In Python, a function is a group of related statements that performs a specific task. A function can be defined as the organized block of reusable code, which can be called whenever required. Functions help break our program into smaller and modular chunks. As our program grows larger and larger, functions make it more organized and manageable.
Python allows us to divide a large program into the basic building blocks known as a function. The function contains the set of programming statements enclosed by {}. A function can be called multiple times to provide reusability and modularity to the Python program. The Function helps to programmer to break the program into the smaller part. It organizes the code very effectively and avoids the repetition of the code. As the program grows, function makes the program more organized.
There are mainly two types of functions.
Advantage of Functions
There are the following advantages of Python functions.
Howt to create function in python?
In Python, def keyword us used to define the function.
Syntax
def my_function(parameters):How to call Function in Python?
In Python, A function must be defined before the function call; otherwise, the Python interpreter gives an error. To call the function, use the function name followed by the parentheses.
return statement in python
The return statement is used at the end of the function and returns the result of the function. It terminates the function execution and transfers the result where the function is called. The return statement cannot be used outside of the function.
It can contain the expression which gets evaluated and value is returned to the caller function. If the return statement has no expression or does not exist itself in the function then it returns the None object.
Arguments in function in python
The arguments are types of information which can be passed into the function. The arguments are specified in the parentheses. We can pass any number of arguments, but they must be separate them with a comma.
Example
Call by reference
In Python, call by reference means passing the actual value as an argument in the function. All the functions are called by reference, i.e., all the changes made to the reference inside the function revert back to the original value referred by the reference.
Example 1 Passing Immutable Object (List)
Example 2 Passing Mutable Object (String)
Types of arguments in Python
There may be several types of arguments which can be passed at the time of function call.
Required Arguments
we can provide the arguments at the time of the function call. As far as the required arguments are concerned, these are the arguments which are required to be passed at the time of function calling with the exact match of their positions in the function call and function definition. If either of the arguments is not provided in the function call, or the position of the arguments is changed, the Python interpreter will show the error.
Default Arguments
Python allows us to initialize the arguments at the function definition. If the value of any of the arguments is not provided at the time of function call, then that argument can be initialized with the value given in the definition even if the argument is not specified at the function call.
Variable-length Arguments (*args)
In large projects, sometimes we may not know the number of arguments to be passed in advance. In such cases, Python provides us the flexibility to offer the comma-separated values which are internally treated as tuples at the function call. By using the variable-length arguments, we can pass any number of arguments.
However, at the function definition, we define the variable-length argument using the *args (star) as *
Keyword arguments(**kwargs)
Python allows us to call the function with the keyword arguments. This kind of function call will enable us to pass the arguments in the random order. The name of the arguments is treated as the keywords and matched in the function calling and definition. If the same match is found, the values of the arguments are copied in the function definition.
Trending Tutorials