Introduction to function arguments in python:
In python, Inputs given to the function are known as function arguments or parameters. Depending upon how we have defined the function, There are four basic types of arguments and in this article we are going to learn each the argument type in detail.
If you don’t know the terms python function, argument, return statement etc. then I would request you to please go through the earlier article Python Functions.
Python user defined function:
To start with this article, Let’s first revise what is python user defined function. So the user defined function is a block of python code written by the user to perform a specific task. This block of code is written in such a modular format (Known as defining the function) so that it can be easily reused.
In python there are many built-in functions which comes readily available with python library and framework. However not all the built-in functions suits to our requirement. Considering our requirements we’ll have to write our own user defined function.
Python function arguments:
The time we want to call any function we have to pass the required inputs to the function. So while calling a function we have to provide these function inputs in parenthesis. In python these inputs given to the function are know as arguments or parameters.
However not every user defined function requires the input (argument), it depends upon how the function is defined and what task it is doing.
Types of python arguments:
There are basic four types of python function arguments;
- Required Argument
- Default Arguments
- Keyword Arguments
- Variable length (Arbitrary) Arguments
Let’s see each type of argument in detail.
Python Required Arguments:
As the name suggests, These are the mandatory arguments those needs to be provided while calling the function. Number of arguments provided should exactly match to the number of arguments defined in function definition. If these arguments are not provided while calling the function it will result an error.
Example 1: Python program for required arguments.
#Function definition def addition(num1, num2): """This function adds the num1 and num2 and prints the result""" result = num1 + num2 return print("Addition of both the numbers is:", result)
num1 = int(input("Please Enter the First Number:")) num2 = int(input("Please Enter the Second number:")) #Calling a function addition(num1, num2) #With both the arguments provided. addition(num1) #With only one argument (num1) #Result 1: With both arguments provided. Please Enter the First Number:8 Please Enter the Second number:6 Addition of both the numbers is: 14
#Result 2: With only one argument (num1) TypeError: addition() missing 1 required positional argument: 'num2'
Here in above example, num1 and num2 are the mandatory arguments. In Result 2 we can see that if we don’t provided the required arguments it results into the error.
So in simple words, while calling a function with required arguments the argument type and number should exactly match with the arguments specified in the function definition.
Python default Arguments:
These type of arguments has default value given in function definition itself. It takes the default value if user does not provide the argument value while calling the function.
On the other hand if user provides the value to default type of argument then default value gets over written by the provided value.
Example 2: Python program for default arguments.
#Defining a function area. def area(length, width=2): #Default value of argument width is "2". """This function calculates the area of rectangle.""" total_area = length*width return print("Total area of rectangle is:", total_area, "sq.ft.") #Calling a function area(4) #without width argument. area(4, 4) #With width argument. #Result: Total area of rectangle is: 8 sq.ft. #With default argument value. Total area of rectangle is: 16 sq.ft. #With user provided argument value.
In above example, While defining function “area” we have defined argument “width” with default value as “2”. Now we are calling the function “area” twice in the code.
first time we have not provided the width argument value. So it takes ‘width=2’ as the default argument value. Therefore the area of rectangle is 8 sq.ft.
Second time we are calling the area function again but this time we are providing the width argument value “4”. So here it overrides the default value and takes the user provided input into consideration. So the total area of rectangle calculated is 16 sq. ft.
Python Keyword Arguments:
When we try to call a function with multiple arguments. The values provided in function call (in parenthesis) gets assigned to the arguments according to the order in which they are provided. If we alter the sequence of values then assignment also changes.
However in python it is possible to assign specific value to a specific argument only, No matter in what sequence you are giving the input. In such cases keyword arguments are used. In this type of argument we do specify the argument name along with its value. So the change in sequence doesn’t affect the values assignment.
See the example below to understand python keyword argument.
#Defining a function print_numbers. def print_numbers(a, b, c): """This function prints the values of a, b, c.""" print(a, b, c) return #Calling a function without keyword arguments. print_numbers(12, 15, 25) print_numbers(25, 15, 12) # Calling a function with keyword arguments print_numbers(b=12, c=15, a=25) print_numbers(a=25, c=15, b=12) #Result: 12 15 25 25 15 12 25 12 15 25 12 15
In above example, We have function print_numbers which requires three arguments (a, b, c). We are calling this function multiple times with the same values but with and without keyword arguments.
In the first two function calls i.e without keyword arguments. If we change the order of inputs the result also got changed. In first call values assigned are a=12, b=15, c=25 while after changing the sequence (second call) values became a=25, b=15, c=12.
Whereas in last two calls (with keyword arguments) we have specified the argument name along with its values. Due to that though we have changed the sequence in last two calls the result obtained is same.
Python variable length (Arbitrary) Arguments:
In some situation, While defining a function we can’t predict the number of arguments user is going to pass while calling a function.
So when we are uncertain about the number of argument then such a situation can be handled by using python variable length or arbitrary arguments. We can provide multiple values to an arbitrary arguments while calling a function.
If we want to make any argument arbitrary then while defining the new function we have to mark an asterisk (*) mark in front of argument. In the function definition any argument with asterisk (*) mark before is considered as arbitrary argument.
#Defining a function area. def print_artbitrary_aruments(*x): #Here 'x' is an arbitrary argument. return print(x) #Calling a function print_artbitrary_aruments print_artbitrary_aruments(10, 20, 12, 'happy') print_artbitrary_aruments('Guide', 'python', 'for', 'beginer', 25) #Result: (10, 20, 12, 'happy') ('Guide', 'python', 'for', 'beginer', 25)
In above example, We are calling the function “print_artbitrary_aruments” but every time we are providing different number of arguments.
So during the while calling a function all the arguments are first converted into a python tuple and then they are passed into the function.
- What is python functions.
- What are the python flow control statements.
- Different data types in python.
Also you can get more help on python forum. Click Here