Function Arguments in Python with Example
In Python, we can pass a piece of data into a user-defined function definition based on function arguments and parameters.
Parameters are the local variables mentioned within the parentheses in the function definition. We also call them as formal parameters.
Arguments are specific values (or data) passed into the function’s parameters when it is called. We also call them actual parameters.
In Python, arguments are passed by value (also known as call by value). Let’s take an example that will clear you the meaning of function arguments and parameters in Python.
# Function definition with two formal parameters x and y. They are local variables.
def multiply(x, y):
print(x * y)
# Main program.
# Function call 1.
multiply(20, 30) # Here, 20 and 30 are argument values.
a = 50
# Function call 2.
multiply(20, a) # Here, value 20 and variable a are arguments or actual parameters.
b = 80
# Function call 3.
multiply(a,b) # Here, variables a and b are argument values/ actual parameters.
Output: 600 1000 4000
As you can see in the above program, we have defined two parameters x and y in the function definition. Both are local variables. We have passed two argument values to the function’s parameters.
However, we can pass many argument values based on the function’s parameters defined, but they should be separated them by a comma.
Types of Function Arguments in Python
In Python, there are four types of function arguments. We can invoke a function using any of these four types of formal arguments. They are:
- Default arguments
- Required arguments
- Keyword arguments
- Variable-length arguments
Let’s understand each type one by one with the help of various examples.
Default Arguments in Python
Default arguments are the default values in the function header that will be used if we pass no argument values during the function call. We also call the default argument values as implicit values.
Moreover, implicit values should be constant (i.e. immutable) and used if no values passed. We can assign the default value with the help of an assignment operator (=). The general syntax to assign the default value is as follows:
parameter_name = value
Consider the following example program based on the default arguments in Python.
Example 1:
def studentInfo(name, gender = 'Male'):
# This function displays the student's info passed in the function parameters.
print('Name:',name)
print('Gender:',gender)
# Main program.
# Function call 1.
studentInfo('Deepak')
# Function call 2.
studentInfo('Tripti', gender = 'Female')
Output: Name: Deepak Gender: Male Name: Tripti Gender: Female
In this example, we have specified the default value for the formal parameter gender as ‘Male’. During the first function call, we simply pass the argument value ‘Deepak’ to the parameter name. We did not pass argument value to gender. In this case, the default value of gender argument will be used.
During the second function call, we passed all two arguments ‘Tripti’ and ‘Female’ to the both parameters name and gender. Hence, no default argument will use.
Example 2:
def studentInfo(name, rollNo = 20, branch = 'Electrical'):
print('Name:',name,'Roll no:',rollNo,'Branch:',branch)
# Main program.
# Function call 1.
studentInfo(name = 'John')
# Function call 2.
studentInfo(name = 'Bob', rollNo = 10)
# Function call 3.
studentInfo(name = 'Jenny', rollNo = 5, branch = 'Computer Science')
Output: Name: John Roll no: 20 Branch: Electrical Name: Bob Roll no: 10 Branch: Electrical Name: Jenny Roll no: 5 Branch: Computer Science
In this example, we have specified default arguments for the parameters rollNo and branch as 20 and ‘Electrical’, respectively. When we do not pass default values to the parameters rollNo and branch, the default argument values used.
Required Arguments in Python
Required arguments are those arguments that are passed to the function’s parameters in the exact positional order to match the function definition. These arguments are also called positional arguments in Python.
Positional arguments are mandatory arguments to the function. During the function call, if we do not pass argument values in the correct number and order, or we pass more or less arguments than the number defined in the function, we will get a syntax error.
Consider the following example code that is based on the required argument function.
Example 1:
# Program to find the area and perimeter of rectangle using function required arguments.
def rectangle(length, breadth):
areaRec = length * breadth # Area of rectangle.
perRec = 2 * (length + breadth) # Perimeter of rectangle.
print("Area of rectangle = ",areaRec)
print("Perimeter of rectangle = ",perRec)
# Main program.
def main():
ln = int(input("Enter the length of rectangle: "))
br = int(input("Enter the breadth of rectangle: "))
rectangle(ln) # Intentionaly missing one argument value.
main() # function calling.
Output: Enter the length of rectangle: 20 Enter the breadth of rectangle: 10 TypeError: rectangle() missing 1 required positional argument: 'breadth'
In this example program, the length and breadth defined in rectangle() function are formal parameters. The ln and br are actual parameters or arguments.
When we execute the above program code, Python assigns the input value to actual parameters, ln and br. We have called rectangle() function from inside the main() function by passing one argument and another is missing. Therefore, we got syntax error.
Hence, when you call a function that specifies some formal parameters, you must pass the exact required number of arguments to the function.
Keyword Arguments in Python
Keyword arguments are those arguments in which values are associated with parameter names. That’s why these arguments are also called named arguments.
They are especially useful when we call a function with a long parameter list where most of the formal parameters have default values and we only wish to update very few of them.
When we use keyword arguments in the function call statement, the caller identifies arguments by the parameter name. We pass the keyword arguments with assigned values in the function call.
The advantages of using keyword arguments in the function call are as:
- Using keyword arguments, function calling becomes easier since we do not need to worry about the order of arguments.
- We can pass values to those parameters to which we wish to, provided that other parameters have default argument values.
Consider the following example code which prints the name, age, and gender of two persons using required arguments. In this program, we have not used keyword arguments. In this case, we cannot change their order. But, we use keyword arguments, we can.
Example:
# Displaying persons info.
def display(name, age, gender):
print('Name:',name)
print('Age:',age)
print('Gender:',gender)
def main():
# First function call.
display('John', 20, 'M') # function calling from another function.
# Second function call.
display('Jenny', 18, 'F')
main() # function calling.
Output: Name: John Age: 20 Gender: M Name: Jenny Age: 18 Gender: F
In this example program, we have not used keyword arguments. Therefore, the order in which we specify the argument values is important. Otherwise, we will get incorrect outcomes.
Now let’s modify in the above program code with their keywords, name, age, and gender.
# Displaying persons info.
def display(name, age, gender):
print('Name:',name)
print('Age:',age)
print('Gender:',gender)
def main():
# First function call.
display('Herry', age = 20, gender = 'M') # function calling from another function.
# Second function call.
display(age = 18, gender = 'F', name = 'Jimmy')
main() # function calling.
Output: Name: Herry Age: 20 Gender: M Name: Jimmy Age: 18 Gender: F
Now observe the function call statement in the above program: the variable name gets the value of ‘Herry’ due to the order or position of the argument. Then, the formal parameters age and gender gets values 20 and ‘M’ respectively due to named or keyword arguments.
Similarly, in the second function call, we have assigned all the formal parameters through keyword arguments. So, the position of all arguments does not matter. However, it is not recommended to change the order of parameters.
Note:
(a) In Python, we can use a non-keyword argument before the keyword arguments. For example:
display('Deep', gender = 'M', age = 25)
This is a valid function call statement because ‘Deep’ is a non-keyword argument and the rest are keyword arguments.
(b) A non-keyword argument may not follow keyword arguments. That is, we cannot use a non-keyword argument after the keyword arguments. If we do, then we will get syntax error. For example:
display(age = 18, name = 'Sanjana', 'F')
This function call will generate a syntax error because ‘F’ is a non-keyword argument, which is following two named arguments.
Variable length Arguments
In some case, we may need to call a function with more arguments than we specified when defining the function. These arguments are called variable length arguments in Python.
They are useful when we do not know the exact number of arguments that will be passed to the function. We can pass the multiple non keyword argument values with a single parameter name.
The special syntax to define a function with non keyword variable arguments is as follows:
def my_function(*args): # args is the name of variable length parameters.
"function docstring"
body of the function
return [expression]
In the above syntax, my_function is the name of function and *args hold the values of all non-keyword variable arguments. Basically, we use *variable_name in the function definition to pass a variable number of arguments to a function.
Let’s take an example based on the variable length arguments in Python.
Example 1:
# Function definition with variable length parameters.
def my_function(*args): # Here, args is name of formal parameter.
print(args)
print('Calling function with two arguments')
my_function(10, 20)
print('Calling function with three arguments')
my_function(10, 20, 30)
print('Calling function with four arguments')
my_function(20, 30, 40, 50)
Output: Calling function with two arguments (10, 20) Calling function with three arguments (10, 20, 30) Calling function with four arguments (20, 30, 40, 50)
In this example program, you can see that in the first function call, we have passed 2 arguments. In the second function call, we have passed three arguments, whereas we have passed four arguments in the third function call.
Hence, this is a significant feature by Python that allows us to pass n number of non keyword arguments with a single parameter name.
Example 2:
# Function definition with one formal parameter and variable length parameters.
def listDay(arg1, *arg2):
print(arg1, arg2, sep="")
def main():
listDay('Name of days are:', 'Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday')
main() # function calling.
Output: Name of days are:('Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday')
In the above example program, the first argument ‘Name of days are:’ passed to the function and assigned into arg1 formal parameter and passed the rest of the non keyword arguments as a tuple with ‘*arg2’.
Difference between Function Parameters and Arguments
We can use interchangeably both terms parameter and argument. But, there is a slight difference between function parameters and arguments. They are:
Parameters are local variables defined in the function’s definition. Whereas arguments are the actual values or data we pass into the function’s parameters when we call the function.
In this tutorial, you have learned about function parameters and arguments in Python with the help of various examples. I hope that you will have understood all types of arguments and practiced all example programs. Stay tuned with our next tutorial where you will learn Pass by value and pass by reference in Python.
Thanks for reading!!!