5 Types of Arguments in Python Function Definition

Learn about different types of arguments used in the python function definition

Photo by Sharon McCutcheon from Pexels

5 Types of Arguments in Python Function Definition:

  1. default arguments
  2. keyword arguments
  3. positional arguments
  4. arbitrary positional arguments
  5. arbitrary keyword arguments

Python Function Definition:

The function definition starts with the keyword def. It must be followed by the function name and the parenthesized list of formal parameters. The statements that form the body of the function start at the next line and must be indented.

Formal parameters are mentioned in function definition .Actual parameters(arguments) are passed during function call.

We can define a function with a variable number of arguments


1.default arguments:

  • default arguments are values that are provided while defining functions.
  • The assignment operator = is used to assign a default value to the argument.
  • default arguments become optional during the function calls.
  • If we provide value to the default arguments during function calls, it overrides the default value.
  • The function can have any number of default arguments
  • Default arguments should follow non-default arguments.

Example:

In the below example, the default value is given to argument b,c

def add(a,b=5,c=10):
return (a+b+c)

This function can be called in 3 ways

  1. Giving only the mandatory argument
print(add(3))
#Output:18

2. Giving one of the optional arguments.
3 is assigned to a, 4 is assigned to b.

print(add(3,4))
#Output:14

3.Giving all the arguments

print(add(2,3,4))
#Output:9

Note: Default values are evaluated only once at the point of the function definition in the defining scope. So, it makes a difference when we pass mutable objects like list,dictionary as default values.


2. Keyword Arguments:

Functions can also be called using keyword arguments of the form kwarg=value.

During function call, values passed through arguments need not be in the order of parameters in the function definition. This can be achieved by keyword arguments. But all the keyword arguments should match the parameters in the function definition.

Example:

def add(a,b=5,c=10):
return (a+b+c)

Calling the function add by giving keyword arguments

  1. All parameters are given as keyword arguments. So no need to maintain the same order.
print (add(b=10,c=15,a=20))
#Output:45

2. During function call, only giving mandatory argument as a keyword argument. Optional default arguments are skipped.

print (add(a=10))
#Output:25

3. Positional Arguments

During function call, values passed through arguments should be in the order of parameters in the function definition. This is called positional arguments.

Keyword arguments should follow positional arguments only.

Example:

def add(a,b,c):
return (a+b+c)

The above function can be called in two ways:

  1. During function call, all arguments are given as positional arguments. Values passed through arguments are passed to parameters by their position. 10 is assigned to a,20 is assigned to b and 30 is assigned to c
print (add(10,20,30))
#Output:60

2. Giving a mix of positional and keyword arguments. keyword arguments should always follow positional arguments

print (add(10,c=30,b=20))
#Output:60

Default vs positional vs keyword arguments:


Important points to remember:

Photo by Author

1.default arguments should follow non-default arguments

def add(a=5,b,c):
return (a+b+c)

#Output:SyntaxError: non-default argument follows default argument

2. keyword arguments should follow positional arguments

def add(a,b,c):
return (a+b+c)

print (add(a=10,3,4))
#Output:SyntaxError: positional argument follows keyword argument

3. All the keyword arguments passed must match one of the arguments accepted by the function and their order is not important.

def add(a,b,c):
return (a+b+c)

print (add(a=10,b1=5,c=12))
#Output:TypeError: add() got an unexpected keyword argument 'b1'

4.No argument should receive a value more than once

def add(a,b,c):
return (a+b+c)

print (add(a=10,b=5,b=10,c=12))
#Output:SyntaxError: keyword argument repeated

5. Default arguments are optional arguments

Example 1: Giving only the mandatory arguments

def add(a,b=5,c=10):
return (a+b+c)

print (add(2))
#Output:17

Example 2: Giving all arguments(optional and mandatory arguments)

def add(a,b=5,c=10):
return (a+b+c)

print (add(2,3,4))
#Output:9

Variable-length arguments

Variable-length arguments are also known as arbitary arguments. If we don’t know the number of arguments needed for the function in advance, we can use arbitary arguments

Two types of arbitrary arguments

  1. arbitary positional arguments
  2. arbitrary keyword arguments

4. arbitrary positional arguments:

For arbitrary positional argument, an asterisk (*) is placed before a parameter in function definition which can hold non-keyword variable-length arguments. These arguments will be wrapped up in a tuple. Before the variable number of arguments, zero or more normal arguments may occur.

def add(*b):
    result=0
for i in b:
         result=result+i
return result

print (add(1,2,3,4,5))
#Output:15
print (add(10,20))
#Output:30

5.arbitrary keyword arguments:

For arbitrary positional argument, a double asterisk (**) is placed before a parameter in function which can hold keyword variable-length arguments.

Example:

def fn(**a):
for i in a.items():
        print (i)
fn(numbers=5,colors="blue",fruits="apple")
'''
Output:
('numbers', 5)
('colors', 'blue')
('fruits', 'apple')
'''

Special Parameters:

As per Python Documentation

By default, arguments may be passed to a Python function either by position or explicitly by keyword. For readability and performance, it makes sense to restrict the way arguments can be passed so that a developer need only look at the function definition to determine if items are passed by position, by position or keyword, or by keyword.

A function definition may look like:

Photo by author

where / and * are optional. If used, these symbols indicate the kind of parameter by how the arguments may be passed to the function: positional-only, positional-or-keyword, and keyword-only.

  1. Positional or keyword arguments
  2. Positional only parameters
  3. keyword-only arguments

1.Positional or keyword arguments

If / and * are not present in the function definition, arguments may be passed to a function by position or by keyword

def add(a,b,c):
return a+b+c

print (add(3,4,5))
#Output:12

print (add(3,c=1,b=2))
#Output:6

2. Positional only parameters

Positional-only parameters are placed before a / (forward-slash)in the function definition. The / is used to logically separate the positional-only parameters from the rest of the parameters. Parameters following the / may be positional-or-keyword or keyword-only.

def add(a,b,/,c,d):
return a+b+c+d

print (add(3,4,5,6))
#Output:12

print (add(3,4,c=1,d=2))
#Output:6

If we specify keyword arguments for positional only arguments, it will raise TypeError.

def add(a,b,/,c,d):
return a+b+c+d

print (add(3,b=4,c=1,d=2))
#Output:TypeError: add() got some positional-only arguments passed as keyword arguments: 'b'

3. Keyword only arguments

To mark parameters as keyword-only, place an * in the arguments list just before the first keyword-only parameter.

def add(a,b,*,c,d):
return a+b+c+d

print (add(3,4,c=1,d=2))
#Output:10

If we specify positional arguments for keyword-only arguments it will raise TypeError.

def add(a,b,*,c,d):
return a+b+c+d

print (add(3,4,1,d=2))
#Output:TypeError: add() takes 2 positional arguments but 3 positional arguments (and 1 keyword-only argument) were given

All 3 calling conventions used in the same function

In the below-given example,function add has all three arguments

a,b — positional only arguments
c-positional or keyword arguments
d-keyword-only arguments

def add(a,b,/,c,*,d):
return a+b+c+d

print (add(3,4,1,d=2))
#Output:10

Important Points to remember:

  1. Use positional-only if you want the name of the parameters to not be available to the user. This is useful when parameter names have no real meaning.
  2. Use positional-only if you want to enforce the order of the arguments when the function is called.
  3. Use keyword-only when names have meaning and the function definition is more understandable by being explicit with names.
  4. Use keyword-only when you want to prevent users from relying on the position of the argument being passed.

My other blog Links

Everything You Ever Wanted to Know About Python Return Statements


Resources(Python Documentation):

Defining functions

default arguments

keyword arguments

special parameters

arbitary argument list


Level Up Coding

Thanks for being a part of our community! Subscribe to our YouTube channel or join the Skilled.dev coding interview course.

https://skilled.dev

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s