After reading this Python function topic, you will able to use it in Python, and you will understand the function structure also you will know the advantages.
In real life, instead of doing all task alone, we generally get our work through others i.e. we divide our work. That’s why Python provides us a subprogram facility called function.
In Python, a large program divides into subprogram for performing the specific task and this subprogram is called function. The function is generally reusable.
Function Structure
When we write function structure generally having two parts as function definition and function call.
Function Definition
The function definition having the statement(s) which executes when function called and after that function returns back to calling function. Here function may or may not return value.
For example:
The function definition in python is given by
def func_name(): val = int(input("Please enter any integer: ")) print('The integer value is', val)
Explanation
- The def keyword represents the beginning of the function definition.
- The func_name represent the function name.
Function Call
The function call really calls the function. When function call, it may or may not pass any argument (parameters).
Example
Program (1): To demonstrate how to define and call function in Python.
#function define def func_name(): val = int(input("Please enter any integer integer: ")) print('The integer value is', val) #function call func_name()
Explanation
- The def keyword represents the beginning of the function definition.
- The func_name represent the function name.
- The statement func_name() call the function.
Output (1)
Please enter any integer integer: 12 The integer value is 12
Function Types:
A function can be classified as:
- Function with argument (parameter) pass and no return values.
- Function with argument pass and return values.
- Function with no argument pass and no return value.
- Function with default argument pass and no return values.
Function with argument pass and no return values
In this type of function, we can only pass data to calling function. The example below will show the way of writing this type of function in Python programming.
Example
Program (1): To demonstrate with example function with argument pass and no return values in Python.
#function to multiply given number by 3 def func_name(n): val = int(input("Please enter any integer integer: ")) x=val*n print('The integer value is',x) #function call with n=3 func_name(3)
Explanation
- The def keyword represents the beginning of the function definition.
- The func_name represent the function name.
- The statement func_name() call the function.
Output (1)
Please enter any integer integer: 12 The integer value is 36
Function with argument pass and return values
In this type of function, we can pass any data to calling function as well as can receive any data from the calling function. The example below will show the way of writing this type of function in Python programming.
Example
Program (1): To demonstrate with example function with argument pass and return values in Python.
def half(n): return 0.5 * n #function call with n=4 and n=8 print(half(4)) print(half(8))
Output (1)
2.0 4.0
Explanation : for Program (1) and Output (1)
- The def keyword represents the beginning of the function definition.
- Here n inside function name (i.e. half(n)) represent argument, value accepts from the user will go to function’s body.
- The half represent the function name.
- The return keyword represents the value that the function returns and in this case the product of its parameter n and 2.
Function with no argument pass and no return values
In this type of function, we cannot pass any data to calling function as well as cannot receive any data from the calling function. The example below will show the way of writing this type of function in Python programming.
Example
Program (1): To demonstrate with example function with no argument pass and no return values in Python.
def func_name(): val = int(input("Please enter any integer integer: ")) print('The integer value is', val) #function call func_name()
Explanation
- The def keyword represents the beginning of the function definition.
- The func_name represent the function name.
- The statement func_name() call the function.
Output (1)
Please enter any integer integer: 12 The integer value is 12
Function with default argument pass and no return values
In this type of function, we will not pass any data to calling function and hence this function, call function with default argument pass. The example below will show the way of writing this type of function in Python programming.
Example
Program (1): To demonstrate with example function with default argument pass and no return values in Python.
def func_name(n=2): val = int(input("Please enter any integer integer: ")) x=val*n print('The integer value is',x) #function call with default value as n=2 func_name() #function call with n=3 func_name(3)
Explanation
- The def keyword represents the beginning of the function definition.
- The func_name represent the function name.
- The statements func_name() and func_name(3) call the function.
Output (1)
Please enter any integer integer: 12 The integer value is 24 Please enter any integer integer: 12 The integer value is 36
Advantages of Function
- Function is easier to understand, debug and test.
- The length of the source program can be reduced using function at a specific position.
- A user-defined function can place into existing library function or the user can define all similar types of the user-defined function under a user define header file.