Function argument in Python

In this tutorial, you are going to learn about the function argument in Python. You can call a function by passing the following types of formal arguments:

  1. Positional argument
  2. Default argument
  3. Keyword argument
  4. Variable-length argument

Different types of function argument in Python

Here we have described all the types of function arguments below one by one.

Positional Argument (Required argument):

Positional arguments are the arguments passed to a function in a correct position order. The number of arguments in the function call and function definition should be matched.

  1. Define the function with the required argument.
  2. Call the function. The value passed in the calling of the function will match exactly the same as the function definition.
    Ex: add(10,20) # function call
    will give x=10 and y=20 for the function definition.
# function definition
def add(x,y):
    print(x+y)

x,y=15,20

# call to the function
add(x,10)
add(x,x)
add(y,x)

Output:-

25
30
35

 

Default argument:

Default argument takes the default value of the argument provided in the function definition if a function call has no argument at that place.

  1. stu('Apoorva','16/Ibs/013',18)
    This function call is having all the arguments. So, it will work as the normal function
  2. stu('Arpit','18/Ibm/009')
    This function call is having only two arguments and the third argument is missing. Since, the third argument is the default argument and its value is given in the function definition. So, the function call will return the default value at that place.
# function definition
def stu(Name,Id,Age=20):
    print( "Name of the student:",Name)
    print("Id of the student:",Id)
    print("Age of the student:",Age)

# function call 1
print("Details of student 1 is:")
stu('Apoorva','16/Ibs/013',18)
print()

# function call 2
print("Details of student 2 is:")
stu('Arpit','18/Ibm/009')

Output:-

Details of student 1 is:
Name of the student: Apoorva
Id of the student: 16/Ibs/013
Age of the student: 18

Details of student 2 is:
Name of the student: Arpit
Id of the student: 18/Ibm/009
Age of the student: 20

 

Keyword argument (Named argument):

Using the keyword argument, the argument passed in a function call will match with the function defined on the basis of the name of the parameter.
Ex: stu(Age=21,Name=’Arpit’,Id=’18/Ibm/009′)
The function call has the argument with the keyword. The ‘Age’, ‘Name’, ‘Id’ keyword will take the value of the argument of the function call and assign it to the argument of the function definition.

Note: The keywords should match exactly the same as the argument of the function definition.

# Function definition
def stu(Name,Id,Age):
    print( "Name of the student:",Name)
    print("Id of the student:",Id)
    print("Age of the student:",Age)

# Function call 1
print("Details of student 1 is:")
stu(Name='Apoorva',Id='16/Ibs/013',Age=18)
print()

# Function call 2
print("Details of student 2 is:")
stu(Age=21,Name='Arpit',Id='18/Ibm/009')

Output:-

Details of student 1 is:
Name of the student: Apoorva
Id of the student: 16/Ibs/013
Age of the student: 18

Details of student 2 is:
Name of the student: Arpit
Id of the student: 18/Ibm/009
Age of the student: 21

 

Variable length argument:

Sometimes, we need to process a function with more arguments than we have specified in the function definition. These types of arguments are called variable length argument. These are represented as *args  and **kargs.

  1. compare(*cmp) will take any number of arguments.
  2. The function call will take the argument as a tuple.
  3. The for loop will get all the values from the tuple.
# function definition
def compare(*cmp):

    # to get the type of cmp
    print(type(cmp))
    print(cmp)
    for arg in cmp:
        print(arg)

# function call 1
compare( 10,20,'str')   # tuple

# function call 2
compare(10,5.5)

Output:-

<class 'tuple'>
(10, 20, 'str')
10
20
str
<class 'tuple'>
(10, 5.5)
10
5.5

rec(**dat) will take the dictionary as the argument. During the function call a dictionary will pass in the rec() function. The value of the argument can be accessed with the help of for loop.

# Function definition
def rec(**dat):

    # to get the type of dat
    print(type(dat))
    print(dat)

# Function call
rec(Name='Apoorva',city='Noida',phoneno=9199471263)  #dictionary

Output:-

<class 'dict'>
{'Name': 'Apoorva', 'city': 'Noida', 'phoneno': 9199471263}

 

Go and check other tutorials on python:

Remove vowels from a string in Python

Python program to create a class which performs basic calculator operations

Leave a Reply

Your email address will not be published. Required fields are marked *