Parameters in Python

In this tutorial, we will learn about parameters in Python.

To pass information to any function, we use some variables or objects known as parameters. The function processes this information and returns the result.

Generally, there are two types of parameters in Python. They are

  • Formal parameters
  • Actual parameters

Syntax of Function Definition and Function call :

def function_name(formal_parameters): #function definition
            #statements

function_name(actual_parameters) #function call

Formal Parameters

The parenthesis of function definition consists of variables known as formal parameters. The information of the actual parameters gets copied into these formal parameters when a function gets called.

Program to understand formal parameters:

def add(a,b):    #function definition
    return a+b

In the above program, a and b are the formal parameters.

Actual Parameters

In python, the actual parameters are also known as Arguments.

The arguments are the data or objects specified during a function call. It passes the data to the function where the formal parameters capture the data and copies into them.

There are 4 types of arguments in Python. They are:

  • Default arguments
  • Required arguments
  • Keyword arguments
  • Arbitrary arguments

Let us understand them clearly in an easy way.

Default Arguments:

In this type of argument, the formal parameters of the function definition are assigned with some default values. As a result, the function uses these default values if we miss actual values in the function call.

For Example:

def add(a,b=5):  #function definition
    return a+b
c=add(2) #function call
print(c)

Output:

7

Note: In the function definition, we need to specify the default arguments only after the non-default arguments. Otherwise, the interpreter raises Syntax Error.

Required Arguments:

Unlike Default arguments, we don’t specify any values to the formal parameters of a function. Instead, we pass all the values to the function through a function call in the order of their position. So, we can also call them positional arguments.

For example:

def add(a,b):  #function definition
    return a+b
c=add(2,5)   #function call
print(c)

Output:

7

Here, the value 2 copies into variable a. Similarly, the value 5 copies into variable b.

Keyword Arguments:

In this type of argument, we use keywords to pass values to the function. Hence, there is no need to maintain the order of their position. Thus, the values get copied into the formal parameters according to the keywords specified.

For example:

def div(a,b):  #function definition
    return a//b
c=div(a=10,b=5)   #function call 1
d=div(b=5,a=10)   #function call 2
print(c)
print(d)

Output:

2
2

Arbitrary arguments:

In this type of argument, we can pass multiple values to the function. We also call them variable-length arguments.

The *args parameter accepts more than one value and stores them as a tuple. Similarly, the **kargs parameter accepts more than one value in the form of key-value pairs and store them as a dictionary.

For example:

Program to demonstrate usage of *args

def fun(*args):
    print("The type of args is:",type(args))
    for i in args: 
        print(i)
fun(1,2,3,4)

Output:

The type of args is: <class 'tuple'>
1 2 3 4

Program to demonstrate usage of **kargs

def fun(**kargs):
    print("The type of kargs is:",type(kargs))
    for i,j in kargs.items():
        print(i,':',j)
fun(a='apple',b='ball',c='cat',d='dog')

Output:

The type of kargs is: <class 'dict'>
a : apple
b : ball
c : cat
d : dog

That’s it! Hope you understood about parameters in python. If you have any doubts feel free to post them below.

Also, do check our other related articles,

Leave a Reply

Your email address will not be published.