Learnerslesson
   JAVA   
  SPRING  
  SPRINGBOOT  
 HIBERNATE 
  HADOOP  
   HIVE   
   ALGORITHMS   
   PYTHON   
   GO   
   KOTLIN   
   C#   
   RUBY   
   C++   




PYTHON - FUNCTION ARGUMENTS


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'.


Example :


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)


Output :



  The added result is 9

So, the variables inside the 'add' Function is called as 'Parameter'.


java_Collections

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.


What if there is an Argument mismatch?


Now, what if, you have defined a 'Function' with two 'Argument' but made the Function call with one argument.


Example :


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)



Output :



  value = add(first_num, second_num)
TypeError: add( ) missing 1 required positional argument: 'third_number'

So, in the above code we have the 'add' Function with three 'Arguments'.


java_Collections

But when the 'add' Function is called, it is called with just two 'Arguments'.


java_Collections

And there is a mismatch of 'Arguments'. And we end up with the below error.



 value = add(first_num, second_num)
TypeError: add( ) missing 1 required positional argument: 'third_number'

Let us see in the next example, how to fix it.


Fixing Argument mismatch with *args


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.


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)


Output :



  The added result is 9

So, to avoid the 'Argument' mismatch problem. We have replaced the arguments 'first_number' and 'second_number' with '*number'.


'def add(*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'.


first_num = 5
second_num = 4

java_Collections

And called the 'add' Function, passing above two values(i.e. 'first_num' and 'second_num') as 'Arguments'.


value = add(first_num, second_num)

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'.


java_Collections

So, the arguments 'first_num' and 'second_num' gets assigned to '*number'.


java_Collections

And the variable 'number' is a tuple holding the values '5' and '4'. And we know a Tuple can be accessed using indexes.


result = number[0] + number[1]

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.


return result

Keyword Arguments in Python


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'.


java_Collections

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.


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)


Output :



  The first number is 5
  The second number is 4
  The third number is 3

So, in the above code we have declared three variables, 'x', 'y' and 'z' and assigned '5', '4' and '3' to it.



x = 5
y = 4
z = 3

java_Collections

Then we have called the 'add' Function passing 'x', 'y', 'z' as argument assigning them to the 'Keyword', 'first_num', 'second_num', 'third_num'.


value = add(first_num = x, second_num = y, third_num = z)

Now, 'first_num', 'second_num' and 'third_num' contains the values of 'x', 'y' and 'z'.


java_Collections

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.


java_Collections

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)
    

Note : There is not 'return' statement in the above program, as we have no values to return.

Arbitrary Keyword Arguments '**'


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'.


Example :


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)


Output :



  The first number is 5
  The second number is 4
  The third number is 3

Similarly, in the above code we have declared three variables, 'x', 'y' and 'z' and assigned '5', '4' and '3' to it.



x = 5
y = 4
z = 3

Then we have called the 'add' Function passing 'x', 'y', 'z' as argument assigning them to the 'Keyword', 'first_num', 'second_num', 'third_num'.


value = add(first_num = x, second_num = y, third_num = z)

Now, 'first_num', 'second_num' and 'third_num' contains the values of 'x', 'y' and 'z'.


java_Collections

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"].


java_Collections

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"])
    

Default Argument


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'.


Example :


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)


Output :



  The added result is 9
  The added result is 0

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.


value = add(first_num, second_num)

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.


print("The added result is ",value)

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.


value = add( )

Without passing any 'Arguments'.


Now, since the 'Default Arguments' is there in place (i.e. first_number = 0, second_number = 0).


def add(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.


java_Collections

And the added value is '0' now.


result = first_number + second_number

And '0' is returned,


return result

Can we pass a List/Tuple/Set as Argument ?


Yes, pass a List/Tuple/Set as Argument.


Let us see with the below example, passing List as Argument.


Example :


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)


Output :



  The added result is 20

So, we have created a List with three values,


numbers = [5, 7, 8]

And we are calling the 'add' Function passing the List as argument.


value = add(numbers)

And the 'add' Function executes, adding all the elements of the List, returning the result.


return result