EMAIL SUPPORT
dclessons@dclessons.comLOCATION
AFFunction() Overview in Python
What is Function
Function are the group of statements, which perform specific task. When a Function is written in python, it can be called using its name in any python program.
When any function is written inside a class, it becomes a method. There are several ways to call a method. Below are two method which can be used to call any Method.
- objectname.methodname ()
- classname.methodname ()
Both Function and Method are same except their placement and the way they are called.
How Function is defined
A function can be defined by using a keyword “def” and then followed by function name and then parentheses () and inside it a set of parameters.
Def function name (para 1, para 2…):
“”” Function docstring”””
Function statements
Example: How to represent a function:
Def minus (a, b):
“”” this function do subtractions of two numbers”””
C= a-b
Print(c)
Here def represents the starting function, with name minus. The parentheses contains the parameter a and b, which will be received by function.
After Function, there is a colon: which represents the beginning of function body. The function body contains the group of statements called suite. In this suite, the first string is called, “docstring”, which gives information about a function and what that function will do and is all about.
Now hen an API documentation file is created, the function name and its “docstring” are stored in that file, providing clear description about the function.
After that “docstring”, the main body of function, which contain logic, which function will perform.
How to call a function
When any function is called, then only it will start work. A function can be called by it name and by passing the values needed to function in parentheses.
Example minus(10,5)
Example 1: Write a Python function to accept two values and find their subtractions.
In the above example, parameters a and b does not know about its type, whatever values will be applied while calling the function, a and b will accept that value type. This is called a dynamic typing.
How to return a values from a function
Return statement actually returns the output from a function. This return statement is written n function body.
Example:
- return a
- return 10
- return x,y,z
Example 2: Write a Python function to find subtractions of two numbers and return the result.
Example3: Write a python function to test whether a number is even or odd.
Example 4: Write a Python Program, to calculate factorial values of numbers
How to return multiple values from Function
In Python, a function can return multiple values. This can be written as below:
Return a, b, c
These a, b, c are return by function as tuple. Now in order to capture the multiple values from function, we need below syntax:
x,y,z = function()
Example 5: Write a Python program to know how function return more than one values.
Function as Object
As soon as function is created, python interpreter internally creates an object. Due to which a function can be passed to another function and it is also possible to return a function from another function.
Lets learn some option based on this concept.
Example 6: Write a python to know how to assign a function to a variable
Example 7: Write a python program to know how to define a function inside another function
LEAVE A COMMENT
Please login here to comment.