What is the difference between function parameters and arguments?

Function Parameters & Arguments in Python-by Sagar Jaybhay

Suppose you written 1 function to print name.

def print_my_name(name):
    print('hi ', name)

And for calling this method you called that method by using below code.

nm = 'sagar'
print_my_name(nm)

in the above example when you written function code passedtheir name it is referred to as a parameter.

When you called that function by using function name andwhatever you pass this is called an argument.

Here the argument is passed by reference what it means that we pass memory address nm to that name or string value.

Positional Parameter:

See the below code of add function

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

In this position of a parameter is first and then b. when we call that function we are passing

add(10, 20)

value of a=10 and b=10 means we are passing parameters in the order they are present in the function.

Default value:

We can make positional parameters are optional by providing the default value to that corresponding parameter in that function.

def add(a, b=100):
    return a + b

in the above code when we call that add function and even ifwe do not provide the value of b then also it executes function because we havea default value for this and

add(10) in this case a=10 and python took b=100 is default value but when we call

add(22,32) in this case a=22 and b=32.

If you want to assign a default value to the parameter you should declare this parameter is the last parameter in function else it gives an error.

Below code will not compile.

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

Keyword or Named arguments:

Take a look to below code

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

and for calling this method we write below code

add(a=10, c=11)

In this when we called a function by giving the parameter names in calling syntax is called keyword or named arguments. In named arguments, you can shuffle the sequence of how the parameter is.

In named argument took below code

add(a=10, 11)

This code is not worked because when you given named argument subsequent argument should be named arguments else it won’t work. This syntax is not valid.

*args:

If the function contains this parameter then it means that this will get any number of parameter or argument. You can not add positional parameter after *args.

def add(a, b, *args):
    c = a + b
    for e in args:
        c = c + e
    print(c)
    return c

add(10, 20, 30, 40, 50)  # output - 150

Positional Arguments with *args:

def add(a, b=10, *args, d):
    c = a + b + d
    for e in args:
        c = c + e
    print(c)     # output - 40

add(10, d=20)

def add(a, b=10, *, d):
    c = a + b + d    
    print(c)     # output - 40

add(10, d=20)

In the above example * it represents the end of positional arguments.

** kwargs (Keyword Only Arguments)

**kwargs: In this no parameter after **kwargs(keyword only argument). ** kwargs this is a dictionary and took parameter as a dictionary.

def add(a, **kwargs):
    print(a ,kwargs)

add(10, b=22, d=20)  # output - 10 {'b': 22, 'd': 20}
def sub(**kwargs):
    c = 0
    for b in kwargs:
        c = c - kwargs[b]
    print(c)

sub(b=22, d=20)  # output - -42
def mul(*args, **kwargs):
    print(args, kwargs)

mul(10, 20, b=22, d=20)  # output - (10, 20) {'b': 22, 'd': 20}

You may also like...

1 Response

  1. June 5, 2019

    […] bound to an object this is called a method. And when you write function outside a class is called function. Below code explains […]

Leave a Reply

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