Python functions are a powerful tool for writing efficient and reusable code. They allow you to break down complex tasks into smaller, more manageable pieces of code. One key feature of Python functions is the ability to pass arguments, or values, into the function. To learn more about Functions, click here.
There are several types of arguments that can be used in Python functions, each with its own syntax and behavior. In this article, we’ll explore the different types of arguments in Python functions and how they can be used to write more flexible and versatile code. Whether you’re new to Python or an experienced programmer, understanding these argument types is essential for taking your Python skills to the next level
If we want to communicate between functions, then, we need to use parameters/arguments with it and the value(s) will be returned from the function. A function should return a user specific or NONE value.
Parameters vs Arguments :
A parameter is the variable listed inside the parentheses in the function definition/function header. An argument is the value that are sent to the function when it is called. Let’s understand with an example :
So, in above example, we define function calculate at line no 1, and receive a value called “a” inside paratheses, this value is called Parameter or Formal Parameter but we call this function from line no 6 and pass a value called “n”, this value is called Argument or Actual Argument.
Types of Parameters :
Positional Parameters :
- It is also called as Required Arguments or Mandatory Arguments.
- As the name suggest, in this call, we need to match the number of arguments with the number of parameters required.
- When the function is called by the call statement, it must match the number and order of argument as defined in the function definition, this is called the positional argument matching.
- The argument should have one to one mapping with parameters present in a function header.
- If there is any mismatching between the argument and parameters, then it will result in an error.
- For example :
As per above example, we cleared about positional argument, but what if, we mismatch the number of arguments and parameters? Look into the below example :
If we miss any one of the positional argument then Python will display the above (red box) error.
Keywords Parameters :
- It is very similar to Positional Parameters except that we can’t remember the order of the arguments.
- We can call a function and pass on values irrespective of their positions provided we use the name of the parameter and assign them values while calling the function.
- In this, we can skip the default argument, but all the keyword arguments should match the parameters in the function definitions.
- Example
In the above example, we saw that at the time of function calling i.e. at line no 5 we have not follow the order of the Parameter as well as we skip the default argument, however when we call same function in line no 6 then we have not skip the default argument as well as the order of the parameter. This is the key feature of Keyword argument where user doesn’t have to remember the order of the parameter.
Default Parameters:
- It is also called Optional Parameter.
- If we initialize some value to parameter at function header, it’s called default value of that parameter.
- When a default value is set to a function then it is not mandatory to pass its value while calling a function.
- If we provide value to the default arguments during function calls, it overrides the default value, i.e. it accepts new value instead of old initialised value.
- The function can have any number of default arguments.
- Default arguments should follow non-default arguments i.e if we want to combine default and non-default arguments then always take default arguments at last after non-default arguments.
- Example
In the above example, in function add (line no 3), “c” is default parameter, i.e. when we call this function at line 6 without passing third argument, then at the time of execution, it will take it as 0, and when we call this function at line 7 with passing third argument as 9, then at the time of execution it will override (change) the value of c old value 0 to new value 9. So, it’s very simple and we can easily manipulate the code without changing the existing code, like in above code if someone write code for addition 2 numbers and we want to write code for addition 3 number then we simply pass one optional parameter. Last, but not the least, what kind of error we will face if by mistake we will write default parameters before the non-default parameters. Let’s look:
Variable Length Argument :
- In Python, variable-length arguments allow a function to accept an arbitrary number of arguments. This is useful when a function needs to be able to handle a variable number of input arguments.
- There are two types of variable-length arguments in Python: *args and **kwargs.
- *args is used to pass a variable number of non-keyword arguments to a function. The arguments are passed as a tuple and can be accessed by the name given to *args. For example:
In this example, the function my_function()
can accept any number of arguments, and those arguments will be printed out one by one.
**kwargs is used to pass a variable number of keyword arguments to a function. The arguments are passed as a dictionary and can be accessed by the name given to **kwargs. For example:
In this example, the function my_function()
can accept any number of keyword arguments, and those arguments will be printed out one by one.
Variable-length arguments can be used in combination with other types of arguments, such as positional and keyword arguments, to create more powerful and flexible functions. However, it is important to use them appropriately and with care, as they can make code harder to read and understand if used excessively or inappropriately.
FUNCTIONS and RETURN VALUE :
- What if we want something in return from function? So, for this, we need to use “return” keyword inside the function definition.
- A function can return a value in the form of : Literals like return 25, Expression like return a+c ,Variable like return a
- After return statement, no statement inside the function body will be executed, i.e. the return statement ends a functions execution even if it is in the middle of the function.
Function can be return values in two ways i.e.
Returning Single Value :
In this, function can return a single value in any of the form like literals, expression or variable. We can assign this value to other variable, print this value or also use it with relational operator. For example:
Returning Multiple Values :
We can return multiple values by simply returning them separated by commas, like :return value1, value2, value3…….
In Python, comma-separated values are considered tuples without parentheses, except where required by syntax. For this reason, In the above example, the function “squared”, returns a tuple with each value as an element. So, we will receive the returned values in form of a tuple variable, like :
If we don’t want to receive the returned values as tuple then we can also unpack the received values of tuple by specifying the same number of variables on the left hand side of the assignment in function call, like:
If function returned more than 3 values then we must take more than 3 variables at left hand side at the function call statements.
In conclusion, understanding the types of arguments in Python functions is essential for writing effective and efficient code. By knowing the different types of arguments, developers can create functions that are more flexible, reusable, and scalable. It is important to use the appropriate type of argument for the specific use case in order to create functions that are easy to use and maintain. Additionally, using a combination of these argument types can create more powerful and versatile functions.
1 comment
Nice explanation about arguments in Python Function.