python


Python Function Arguments


Argument vs Parameters

Types of arguments

In Python , a function can have 4 types of arguments:

  1. Positional Argument
  2. Keyword Argument
  3. Default Argument
  4. Variable Length Argument


Positional Arguments

These are the arguments passed to a function in correct positional order.
Here the number of arguments in the call must exactly match with number of parameters in the function definition
If the number of arguments in call do not match with the number of parameters in function then we get TypeError.
Example


def grocery(name,price):
    print("Item is",name,"It's price is",price)
    
grocery("Bread",20)
grocery(150,"Butter")

    
Output:

Item is Bread It's price is 20
Item is 150 It's price is Butter
    

The Problem With Positional Arguments

The problem with positional arguments is that they always bind to the position of parameters.
So 1st argument will be copied to 1st parameter , 2nd argument will be copied to 2nd parameter and so on.
Due to this in the previous example the value 150 was copied to name and “Butter” was copied to price
To solve this problem , Python provides us the concept of keyword arguments


Keyword Arguments

Keyword arguments are arguments that identify parameters with their names
With keyword arguments in Python, we can change the order of passing the arguments without any consequences
Syntax:


function_name(paramname1=value,paramname2=value)
Example:

def grocery(name,price):
    print("Item is",name,"It's price is",price)
    
grocery(name="Bread",price=20)
grocery(price=150,name="Butter")

Output:

Item is Bread It's price is 20
Item is Butter It's price is 150
Point to remember!!
A positional argument can never follow a keyword argument i.e. the keyword argument should always appear after positional argument


Default Arguments

For some functions, we may want to make some parameters optional and use default values in case the user does not want to provide values for them.
This is done with the help of default argument values.
We can specify default argument values for parameters by appending to the parameter name in the function definition the assignment operator (=) followed by the default value.
Syntax:


def  function_name(paramname1=value,paramname2=value):
    # function body
Example:

def greet(name,msg="Good Morning"):
    print("Hello",name,msg)
    
greet("Chitranshi")
greet("Aditi","How are you?")

Output:

Hello Chitranshi Good Morning
Hello Aditi How are you?
Point to remember!
A function can have any number of default arguments but once we have a default argument, all the arguments to it’s right must also have default values.
This means to say, non-default arguments cannot follow default arguments.
If a function has default arguments , set then while calling it if we are skipping an argument then we must skip all the arguments after it also.


Variable Length Arguments

In this technique , we define the function in such a way that it can accept any number of arguments from 0 to infinite Syntax of Variable Length Argument:


def <function_name>(* >arg_name<):
    Function body
As we can observe , to create a function with variable length arguments we simply prefix the argument name with an asterisk.

def addnos(*a):    
The function addnos() can now be called with as many number of arguments as we want and all the arguments will be stored inside the argument a which will be internally treated as tuple
Example:

def addnos(*a):
    sum =0
    for x in a:
        sum=sum+x
    return sum

print(addnos(10,20))
print(addnos(10,20,30)) 

Output:

30
60
Point yo remember!!
A function cannot have 2 variable length arguments. So the following is wrong:

def addnos(*a,*b):
If we have any other argument along with variable length argument , then it should be set before the variable length argument
If we set the other argument used with variable length argument , after the variable length argument then:
While calling it we must pass it as keyword argument
OR
Either it should be set as default argument