⏺️ Parameters & Arguments

We can also pass inputs in the form of variables to the defined functions. Basically they are of two types.

Function Parameters: The variable names mentioned inside parenthesis while function definition.

Function Arguments: The input values or variables provided to the function during function call.

Note: If function expecting parameters and we dont pass arguments during function call, it will throw TypeError: missing required positional arguments

💻 Example:

Lets define function which accepts two parameters: num1,num2

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
# function definition with parameters
def add_numbers(num1,num2):
    print(f'first number:{num1}')
    print(f'second number:{num2}')

    # calculate addition
    result = num1+num2
    print(f'their addition:{result}')

# lets define two variables
a = 10
b = 40

# function call with arguments
add_numbers(a, b)

✅ Output:

first number:10
second number:40
their addition:50

There are different types of arguments can be passed to the function.

  1. Positional Arguments

  2. Default Arguments

  3. Keyword Arguments

  4. Varaible Length Arguments

Lets learn them one by one.

1) Positional Arguments

  • We can pass the input values through arguments to function parameters by their position.

  • The order or the position of the arguments passed during function call is important.

  • If you change the order of arguments provided then you also need to change the order of parameters to implement correct logic.

💻 Example:

Lets define function which accepts three parameters: name1,name2,name3

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
# function definition with parameters
def attendance(name1,name2,name3):
    print(f'Roll number one:{name1}')
    print(f'Roll number two:{name2}')
    print(f'Roll number three:{name3}')
    print(f'Attendance completed ...\n')

# lets define three name variables
a = 'Raj'
b = 'Pooja'
c = 'Kamal'

# function calls with interchanging positional arguments
attendance(a, b, c)
attendance(c, b, a)
attendance(b, a, c)

✅ Output:

Roll number one:Raj
Roll number two:Pooja
Roll number three:Kamal
Attendance completed ...

Roll number one:Kamal
Roll number two:Pooja
Roll number three:Raj
Attendance completed ...

Roll number one:Pooja
Roll number two:Raj
Roll number three:Kamal
Attendance completed ...

2) Default Arguments

  • The value of these arguments can be set default during function definition.

  • A function can have any number of default arguments.

  • The default arguments always be provided after positional or non default arguments if any.

  • We can make argument default by assigning value to it using assignment operator in function definition.

  • We dont need to pass default argument during function call, but we need to pass positional arguments if any.

💻 Example:

Lets consider previous example only. Lets make second and third student name as default.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
# function definition with default arguments
def attendance(name1,name2='Pooja',name3='Kamal'):
    print(f'Roll number one:{name1}')
    print(f'Roll number two:{name2}')
    print(f'Roll number three:{name3}')
    print(f'Attendance completed ...\n')

# lets define positional argument
a = 'Raj'

# function calls with only positional arguments
attendance(a)

✅ Output:

Roll number one:Raj
Roll number two:Pooja
Roll number three:Kamal
Attendance completed ...
  • We can also override the default value by passing the argument as positional during function call as follows:

💻 Example:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
# function definition with default arguments
def attendance(name1,name2='Pooja',name3='Kamal'):
    print(f'Roll number one:{name1}')
    print(f'Roll number two:{name2}')
    print(f'Roll number three:{name3}')
    print(f'Attendance completed ...\n')

# lets define positional arguments
a = 'Raj'
b = 'John'

# function calls with positional arguments
attendance(a, b)

✅ Output:

Roll number one:Raj
Roll number two:John
Roll number three:Kamal
Attendance completed ...

💻 Example: lets consider another example.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
# function definition with default arguments
def add_nums(num1,num2,num3=100):
    print(f'First Number: {num1}')
    print(f'Second Number: {num2}')
    print(f'Third Number: {num3}')
    result = num1+num2+num3
    print(f'Their addition: {result}')
    print(f'Calculation completed ...\n')

# function calls with positional arguments
add_nums(100, 0)
add_nums(100, 0, 5)

✅ Output:

First Number: 100
Second Number: 0
Third Number: 100
Their addition: 200
Calculation completed ...

First Number: 100
Second Number: 0
Third Number: 5
Their addition: 105
Calculation completed ...

3) Keyword Arguments

  • We can pass arguments as key-value pairs known as keyword arguments.

  • If all the arguments are keyword then we dont need to maintain their order during function call.

  • The keyword arguments should follow the positional arguments if any.

  • If keyword arguments are passed before positional arguments, it will throw SyntaxError.

💻 Example:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
# function definition with default arguments
def add_nums(num1,num2,num3=100):
    print(f'First Number: {num1}')
    print(f'Second Number: {num2}')
    print(f'Third Number: {num3}')
    result = num1+num2+num3
    print(f'Their addition: {result}')
    print(f'Calculation completed ...\n')

# function call with keyword arguments
add_nums(num3=100, num2=55, num1=45)

# you can skip default arguments
add_nums(num2=10, num1=5)

✅ Output:

First Number: 45
Second Number: 55
Third Number: 100
Their addition: 200
Calculation completed ...

First Number: 5
Second Number: 10
Third Number: 100
Their addition: 115
Calculation completed ...
  • Keyword argument sequence with positional arguments

💻 Example: Lets pass arguments with incorrect order.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
# function definition with default and positional arguments
def add_nums(num1,num2,num3=100):
    print(f'First Number: {num1}')
    print(f'Second Number: {num2}')
    print(f'Third Number: {num3}')
    result = num1+num2+num3
    print(f'Their addition: {result}')
    print(f'Calculation completed ...\n')

# lets define positional argument
num1 = 5

# function calls with positional and keyword arguments

add_nums(num2=10, num1)

✅ Output:

SyntaxError: positional argument follows keyword argument

💻 Example: Arguments with correct order.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
# function definition with default and positional arguments
def add_nums(num1,num2,num3=100):
    print(f'First Number: {num1}')
    print(f'Second Number: {num2}')
    print(f'Third Number: {num3}')
    result = num1+num2+num3
    print(f'Their addition: {result}')
    print(f'Calculation completed ...\n')

# lets define positional argument
num1 = 5

# function calls with positional and keyword arguments
add_nums(num1, num2=10)

✅ Output:

First Number: 5
Second Number: 10
Third Number: 100
Their addition: 115
Calculation completed ...

4) Variable Length Arguments

What if there are hundreds of positional and keyword arguments need to be considered for a function ? It will take number of lines of codes to just pass the arguments to the function call as well as to function definition. There is a way using which we can pass any number of positional and keyword arguments. It is as follows.

1) Variable Positional Arguments(*args):

  • We can pass arbitrary number of positional arguments to a function.

  • All positional arguments are stored into a tuple ‘args’ using *args syntax during function definition.

Lets define a function which takes arbitrary numbers as input and returns their sum.

💻 Example:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
# function definition with arbitrary positional arguments
def add_nums(*args):
    print('Positional arguments passed to the function are:')
    print(args)

    # calculate sum of tuple elements
    result = sum(args)

    # return the result
    return result

# function calls with multiple positional arguments
result = add_nums(10,20,30,40)
print(f'The numbers addition: {result}')

✅ Output:

Positional arguments passed to the function are:
(10, 20, 30, 40)
The numbers addition: 100

2) Variable Keyword Arguments(**kwargs):

  • We can pass arbitrary number of keyword arguments to a function.

  • All keyword arguments are stored into a dictionary ‘kwargs’ using **kwargs syntax during function definition.

💻 Example:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
# function definition with arbitrary keyword arguments
def add_nums(**kwargs):
    print('Keyword arguments passed to the function are:')
    print(kwargs)

    # calculate sum of dictionary values
    result = sum(kwargs.values())

    # return the result
    return result

# function calls with multiple positional arguments
result = add_nums(num1=10,num2=20,num3=30,num4=40)
print(f'The numbers addition: {result}')

✅ Output:

Keyword arguments passed to the function are:
{'num1': 10, 'num2': 20, 'num3': 30, 'num4': 40}
Their addition: 100

Using both arbitrary arguments:

  • We can use both arbitrary positional and arbitrary keyword arguments with a single function.]

  • Arbitrary positional arguments to be mentioned first, then you can mention the arbitrary keyword arguments during function call.

💻 Example:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
# function definition with arbitrary positional and keyword arguments
def add_nums(*args, **kwargs):
    print('Positional arguments passed to the function are:')
    print(args)
    print('Keyword arguments passed to the function are:')
    print(kwargs)

    # calculate sum of tuple items
    result1 = sum(args)

    # calculate sum of dictionary values
    result2 = sum(kwargs.values())

    # return the result
    return result1, result2

# lets define positional arguments
pos1 = 1
pos2 = 2
pos3 = 3
pos4 = 4

# function call with arbitrary positional and keyword arguments
result1, result2 = add_nums(pos1, pos2, pos3, pos4, num1=10,num2=20,num3=30,num4=40)
print(f'The positional argument numbers addition: {result1}')
print(f'The keyword argument numbers addition: {result2}')

✅ Output:

Positional arguments passed to the function are:
(1, 2, 3, 4)
Keyword arguments passed to the function are:
{'num1': 10, 'num2': 20, 'num3': 30, 'num4': 40}
The positional argument numbers addition: 10
The keyword argument numbers addition: 100

Be prepared with below questions prior to your interview !

Frequently asked Interview Questions
  1. What are different types of argument can we pass to a function?

  2. Explain variable length positional arguments (*args).

  3. Explain variable length keyword arguments (**kwargs).