In this tutorial, we will discuss those function which takes variables as arguments and perform tasks with the help of passed arguments. so let’s discuss Python Functions Arguments.
Python Functions Arguments
Arguments are the variables name or values which are pass along the function name inside the parenthesis when we call or declare a function.
Syntax of Argument in python:
def function_name(argument1, argument2): # declaring a function with 2 arguments function body function_name(value1 , value2) #function calling
Let’s understand it with an example:
def say(arg1,arg2): print(arg1) print(arg2) h = "hello" w = "world" say(h,w)
Behind the Code
In the above example you can see that first we have defined a function say(arg1,arg2) and inside the function, we print out the values of arg1 and arg2. But this time when we are calling the function say() we have passed 2 variables h and w along the function say().
Always remember when you call a function always pass the same number of variables, that many arguments have been declared. In the above example, we have declared 2 arguments arg1 and arg2 and when we called the function, we pass the same number of variables.
Until now we have seen that when we define a function, we pass arguments along with it and those arguments get their value when we call the function with the variables. We also discuss that the number of variables or value at function calling should be equal to the number of arguments assigned to the function definition.
With the help of default argument concept, it’s not necessary that you have to pass the same number of variables as arguments. In default arguments, we assign some value to the arguments when we define the function. So, it’s on the user whether to provide the same number of variables while calling the function or not. If the user does not provide the variable during function calling the default value assigned to the arguments, what if the user passes the variable during the function calling, in that case, the values or variable provided during function calling will overwrite the default values to the arguments.
Let’s understand it with examples:
- Default argument with no variables at function calling:
def say(arg1 ="saying" , arg2 ="hello" ): # Default arguments print(arg1 , arg2) say() #no variables with function calling
- Default arguments with variables at function calling:
def add(num_1 = 1, num_2 = 1 ): return num_1 + num_2 result = add(4,7) print(result)
Behind the code
In the above example 1 we have declared a function say() with 2 arguments arg1 and arg2 and also give some value to those arguments and those values are default values to the arguments, and when we call the function say() we did not pass any value along it and the programme run error free because arguments got their values by default.
In example 2 we declared a function add() with two arguments num_1 and num_2 and like example 2 both the arguments got some default value but in this example when we call the function with 2 values those values at function calling replace the default values of arguments, and we got output 11 instead of 2.
In Arbitrary argument, we can pass as many as arguments we want with a single argument name and asterisk (*) sign before the argument.
In some function, we do not know, how many variables to pass when we call a function. So, when we define a function, we use the Arbitrary Arguments that can accept more as many variables as an argument.
There are two types of Arbitrary Arguments
the name args and kwargs are conventional the main deal is the asterisk (*) mark. With argrs we are using only one (*) sign that means that the variables pass at the function calling, will be treated as tuples and with kwargs (**) sing they will treat as a dictionary.
Let’s understand it with an example:
- Arbitrary Arguments with *args
def add(*args): sum = 0 print("args type is",type(args)) for i in args: sum+=i print(sum) add(1,3,4,5,6,7,8,9,10)
args type is <class 'tuple'> 53
- Arbitrary Arguments with **kwargs
def add(**kwrgs): sum = 0 print("kwrgs type is",type(kwrgs)) for i in kwrgs.values(): sum+=i print(sum)
kwrgs type is <class 'dict'> 25