Dear learners, in this tutorial we are going to learn Python Function and Arguments. Previously we learned about Python loop. Hope that you will enjoy learning.
Table of Contents
Python Function and Arguments
Basically function is used to do some specific work. We can split a large task to some work distribution and do them separately which will enable us to debug program easily. The basic structure of a Python function is given below:
def function_name( arguments ) : #perform tasks
Python function arguments can be one or more. If it is more than one, they has to be separated by comma. For example, you want to calculate result = x * ( y + z ).
Let, p = y + x
So, result = x * p
First, we have to calculate p, and then, putting the value of p we have to calculate result. To do so, we will create separate functions. The following code will illustrate about Python function.
''' create a function for adding two variables. It will be needed to calculate p=y+z ''' def calculateP( y , z ) : return int( y ) + int( z ) ''' create a function for multiplying two variables. It will be needed to calculate p=y+z ''' def calculateResult( x, p ) : return int( x ) * int( p ) #Now this is the beginning of main program x = input('x: ') y = input('y: ') z = input('z: ') #Now Calculate <strong>p</strong> p = calculateP ( y , z ) #Now calculate <strong>result</strong> result = calculateResult( x , p ) #Print the result print(result)
The output of the following code will be
================== RESTART: /home/imtiaz/Desktop/pyDef.py ================== x: 2 y: 2 z: 3 10 >>>
Passing Variable Number of Arguments
If you don’t know how many arguments you have to pass in the function then you can allow the function to take variable number of arguments. To do this you need to add an asterisk(*) right before your argument name. But there is some condition, that your special argument has to be the last argument of the function and it is allowed to have only one argument of that kind in one function. The following example will help you to understand the variable number of arguments in python function.
def varFunc(name,*args): print("This is the first argument "+str(name)) #This print will make you understand that the args is a list print(args) for item in args: print(item) print("First time:") varFunc("of 1st function call",2, 3, 4, 5) print("Second time:") varFunc("of 2nd function call","asd","Bcd") print("Third time:") varFunc("and only argument of 3rd function call")
The Output will be:
Passing Key-Value pair as Optional Python Function Arguments
You can also pass arguments by keywords, so that the order you send argument does not matter. To do so, you have to add two asterisks(**) right before the special argument when you define a function. The following example will clear your concept.
def varFunc(name, roll, **option): print("Name: "+name) print("Roll: "+str(roll)) if "age" in option : print("Age: "+ str(option.get("age"))) if "gender" in option: print("Gender: "+ str(option.get("gender"))) print("First Person") varFunc("Alice", 234, age=18, gender="female") print("\nSecond Person") #See, the order of argument age and gender is different now varFunc("Bob", 204, gender="male", age=21) print("\nThird Person") #We will not pass age as and argument varFunc("Trudy", 204, gender="male")
Output will be
================== RESTART: /home/imtiaz/Desktop/key_value_arg.py ================== First Person Name: Alice Roll: 234 Age: 18 Gender: female Second Person Name: Bob Roll: 204 Age: 21 Gender: male Third Person Name: Trudy Roll: 204 Gender: male >>>
This is all about Python Function and Arguments. Hope that you understood well. For any query feel free to use the comment section below.
Keep you enthusiasm always up for learning new things!
Reference: Official Documentation