Say, you want to pass some information to a 'Function'.
For example, if you want a 'Function' to add two numbers. You need to pass those two numbers to the 'Function'.
Say, in the below example, you want the 'add' Function to add two numbers for you. And you have passed those numbers in between the brackets '( )' of the 'add' Function(i.e. 'first_number' and 'second_number').
And those elements inside the brackets '( )' of the 'add' Function is called as 'Parameters' and the actual value passed to them is called as 'Argument'.
def add(first_number, second_number): result = first_number + second_number return result first_num = 5 second_num = 4 value = add(first_num, second_num) print("The added result is ",value)
So, the variables inside the 'add' Function is called as 'Parameter'.
And the actual values (i.e. '5' and '4') to be sent to the 'Function' are the 'Arguments'.
Throughout the tutorial we will be calling the 'Parameters' as 'Arguments' to avoid unnecessary confusion.
Now, what if, you have defined a 'Function' with two 'Argument' but made the Function call with one argument.
Now, what if, you have defined a 'Function' with two 'Argument' but made the Function call with one argument.
def add(first_number, second_number, third_number): result = first_number + second_number + third_number return result first_num = 5 second_num = 4 value = add(first_num, second_num) print("The added result is ",value)
So, in the above code we have the 'add' Function with three 'Arguments'.
But when the 'add' Function is called, it is called with just two 'Arguments'.
And there is a mismatch of 'Arguments'. And we end up with the below error.
Let us see in the next example, how to fix it.
To solve the problem of Argument mismatch, we can just add an asterisk '*' in front of the 'Argument'.
Let us see in the below example.
def add(*number): result = number[0] + number[1] return result first_num = 5 second_num = 4 value = add(first_num, second_num) print("The added result is ",value)
So, to avoid the 'Argument' mismatch problem. We have replaced the arguments 'first_number' and 'second_number' with '*number'.
And no matter how many 'Arguments' you pass, '*number' will make an intelligent guess and create a 'Tuple' out of it.
Now, if you see the complete execution, we have initialised the first and second number in the variables, 'first_num' and 'second_num'.
And called the 'add' Function, passing above two values(i.e. 'first_num' and 'second_num') as 'Arguments'.
And the execution of 'add' Function begins.
def add(*number): result = number[0] + number[1] return result
So, the arguments 'first_num' and 'second_num' gets assigned to '*number'.
So, the arguments 'first_num' and 'second_num' gets assigned to '*number'.
And the variable 'number' is a tuple holding the values '5' and '4'. And we know a Tuple can be accessed using indexes.
So, 'number[0]' would access the first element of the Tuple (i.e. '5') and 'number[1]' would access the second element of the Tuple (i.e. '4').
And the added value is returned to the caller.
We have seen that if we want to pass arguments to a Function, the order of the arguments needs to be maintained.
i.e. If 'def add(first_number, second_number):' is the function definition and add(first_num, second_num) is the function call. The argument 'first_num' will get assigned to 'first_number' and 'second_num' will get assigned to 'second_number'.
Now, let us say, we don't want to maintain this argument order. And 'Keyword Arguments' helps us to achieve it.
Let us see with the below example.
def add(third_num, first_num, second_num): print("The first number is ",first_num) print("The second number is ",second_num) print("The third number is ",third_num) x = 5 y = 4 z = 3 value = add(first_num = x, second_num = y, third_num = z)
So, in the above code we have declared three variables, 'x', 'y' and 'z' and assigned '5', '4' and '3' to it.
Then we have called the 'add' Function passing 'x', 'y', 'z' as argument assigning them to the 'Keyword', 'first_num', 'second_num', 'third_num'.
Now, 'first_num', 'second_num' and 'third_num' contains the values of 'x', 'y' and 'z'.
Now, the values of 'first_num', 'second_num' and 'third_num' doesn't change, when the Function execution starts.
And the execution of the 'add' Function begins.
def add(third_num, first_num, second_num): print("The first number is ",first_num) print("The second number is ",second_num) print("The third number is ",third_num
Even though the order of the Arguments in the Function definition are not the same, still 'first_num', 'second_num' and 'third_num' won't change.
And the print statement, prints their respective values.
print("The first number is ",first_num) print("The second number is ",second_num) print("The third number is ",third_num)
When you do not want to specify all the 'Arguments' in the Function definition. In that case you define the Function with one 'Argument' and put double Asterisk '**' before that 'Argument'.
def add(**number): print("The first number is ",number["first_num"]) print("The second number is ",number["second_num"]) print("The third number is ",number["third_num"]) x = 5 y = 4 z = 3 value = add(first_num = x, second_num = y, third_num = z)
Similarly, in the above code we have declared three variables, 'x', 'y' and 'z' and assigned '5', '4' and '3' to it.
Then we have called the 'add' Function passing 'x', 'y', 'z' as argument assigning them to the 'Keyword', 'first_num', 'second_num', 'third_num'.
Now, 'first_num', 'second_num' and 'third_num' contains the values of 'x', 'y' and 'z'.
Now, the values of 'first_num', 'second_num' and 'third_num' doesn't change, when the Function execution starts.
Now, we have declared the Function with one 'Argument' and put double Asterisk '**' before that 'Argument'(i.e. '**number').
def add(**number): print("The first number is ",number["first_num"]) print("The second number is ",number["second_num"]) print("The third number is ",number["third_num"])
Now to access, 'first_num', 'second_num' and 'third_num', we have used number["first_num"],number["second_num"] and number["third_num"].
And the print statement, prints their respective values.
print("The first number is ",number["first_num"]) print("The second number is ",number["second_num"]) print("The third number is ",number["third_num"])
Say, you want to pass two 'Arguments' to add two numbers. But what if you don't pass any 'Argument' while calling the Function. You would end up with an error.
Let us see in the below example, how can we avoid that error with 'Default Argument'.
def add(first_number = 0, second_number = 0): result = first_number + second_number return result first_num = 5 second_num = 4 value = add(first_num, second_num) print("The added result is ",value) value = add() print("The added result is ",value)
So, in the above code, you have initialised the variables, 'first_num' and 'second_num' with '5' and '4'.
And called the 'add' Function passing, 'first_num' and 'second_num' as Arguments.
And the 'add' Function executes, adding 'first_number' and 'second_number' returning the result.
def add(first_number = 0, second_number = 0): result = first_number + second_number return result
And the added value is printed using the print statement.
We can see that the arguments, 'first_number' and 'second_number' is initialised with '0',i.e. first_number = 0, second_number = 0. Which is called 'Default Arguments'. It is ignored if 'Arguments' are passed to it. But comes into effect when no values are passed.
Now, in the next Line, we have called the 'add' Function.
Without passing any 'Arguments'.
Now, since the 'Default Arguments' is there in place (i.e. first_number = 0, second_number = 0).
Even though no 'Arguments' are passed, the default values 'first_number = 0' and 'second_number = 0' are initialised and returned.
And the added value is '0' now.
And '0' is returned,
Yes, pass a List/Tuple/Set as Argument.
Let us see with the below example, passing List as Argument.
def add(number_list): result = number_list[0] + number_list[1] + number_list[2] return result numbers = [5, 7, 8] value = add(numbers) print("The added result is ",value)
So, we have created a List with three values,
And we are calling the 'add' Function passing the List as argument.
And the 'add' Function executes, adding all the elements of the List, returning the result.