In this article, MarsDevs illustrates functions in Python, the function argument, and the keyword argument. We use various examples with code to understand the concept better.
A function is a block of code that performs certain defined operations which can be reused by calling that function. So, a function has a high degree of code reuse capacity and better modularity.
When a function is created, we define the sequence of steps that should be performed and return its resultant or performed value to the caller. We call this function by the name of the function.
Python has already built-in functions like print(), sqrt(), etc. We can also create our own functions, so created functions by users are called user-defined functions.
User-defined function and built-in function
We can create a function by defining the required functionality into it. We need to remember some rules before creating functions using Python. These rules are as follows
Syntax of function
There should be positional behavior to return in the same order as they defined.
The function definition consists of name, parameters required and required step in which manner function should go, then return values, if any.
We call a function by its name with parentheses () and if there are any parameters then we put those parameters inside these parentheses ().
There are different types of techniques to pass arguments to a function. We will discuss 2 argument passing techniques here: pass-by-value and pass-by-reference.
In pass-by-reference argument passing technique, when we change parameters that are referenced from the caller function within a called-function, the change also reflects back in the calling / original function. This is because addresses of those parameters are passed instead of values to the called-function. Python uses this argument passing technique.
In the pass-by-value argument passing technique, when we change parameters referenced from caller function within a called-function, the change does not reflect back in the calling / original function. This is because only the copies of those values of that argument are passed instead of addresses.
Now, let’s try to understand arguments that are passed to a function. There are various types of functions arguments
These are explained as following below in brief
These arguments should be passed in the correct positional order, so they should match a number of arguments in the function call with the function definition. They should be defined in an ordered manner.
The above mess() function requires one argument (name) to pass in function, there will be an error raised without it, modified example is given as following below
When you pass these arguments, then the caller identifies the arguments by parameter name. These are not mandatory to pass the arguments or place them in order like required/positional arguments. Python interpreters can identify and match the values with parameters.
Take this example
If you want to label them in first_name and last_name in the calling-function, then you can label as below
Here first_name and last_name are keyword arguments
When you do not provide any value for an argument then it assumes a default value (you can set) in the function call for that argument. Take the following example
Note that when you do not know how many a number of arguments should be passed, then we use two asterisks **name, to map arguments to a single parameter. Consider the following example
In this, there could be less number of parameters in the function definition than the number of arguments passed. These parameters are not named in the function definition and are known as variable-length arguments. An example is given below
Recursive function calls, again and again, the same function till the base condition is satisfied or stack overflows / memory overflows.
Consider the following recursive function to calculate the factorial of natural number n.
These functions are defined without a name and use the “lambda” keyword instead of “def”, so these are also called as a lambda function. They return only one value but can have 0 or more arguments.
Syntax is given below
If we were to write above logic using normal function, then it would be
There are 2 types of variables, based on their scope - Global variable and Local variable.
These variables are declared outside of the functions and can be accessed globally by any function defined in the main function or program. These can be accessed inside or outside of the function, i.e everywhere.
An example is given as follows below
These variables are declared inside the function and can be accessed within that function only. These can not be accessed globally.
An example is given as following below
It will print 100 for inside x because the value of x = 100 is defined inside the fun, so no error.
It will not print for the outside x and will raise a NameError: that name ‘x’ is not defined.
Note that when you define both x inside and outside of the function, then it will prioritize local x over global. It means the local variable has a higher priority to access than the global variable if both local and global exist. An example is given below.
A function can not be empty, to avoid any errors we use ‘pass’ statement. Can be used as a placeholder as well.