You can learn about Functions in Python Programs with Outputs helped you to understand the language better.

Python Programming – Types Of Parameters Or Formal Arguments

You can call a function by using the following types of formal arguments:

  1. Required arguments
  2. Keyword arguments
  3. Default arguments
  4. Variable-length arguments

Required Arguments

Required arguments are the arguments passed to a function in correct positional order. Here the number of arguments in the function call should match exactly with the function definition. To call the function printme ( ), you definitely need to
pass one argument otherwise it would give a syntax error. See Example 5.

Example
Demo of required arguments.

# Function definition is here
def printme(str):
“This prints a passed string into this function”
print(str)
return
# Now you can call printme function
printme(“This function will print passed function”)RUN
>>>
This function will print passed function
>>>

When you do not pass argument to function call, i.e., you write printme() function without argument then, it will give syntax error:
Traceback (most recent call last):
File “test.py”, line 11, in <module> printme();
TypeError: printme() takes exactly 1
argument (0 given)

Using Positional Parameters and Positional Arguments

If you just list out a series of variable names in a function’s header, you create a positional parameter, def birthday1(name, age):
If you call a function with just a series of values, you create a positional parameter: birthday1 (“Sai”,7)
Using positional parameters and positional arguments means that parameters get their value based only on the position of the values sent. The first parameter gets the first value sent, the second parameter gets the second value sent, and so on. With this particular function call, it means the name gets “Sai” and the age gets 7.
If you switch the positions of two arguments, the parameters get different values. So with the calling birthday1(7, “Sai”) i.e., the name gets the first value 7 and age gets the second value “Sai” which is wrong. Thus the values of arguments must match with parameters, position-wise (Positional) and order-wise. And the arguments must be provided for all parameters (Required).

Keyword Arguments

Keyword arguments are the named arguments with assigned values being passed in the function call statement, i.e., when you use keyword arguments in a function call, the caller identifies the arguments by the parameter name.
This allows you to skip arguments or place them out of order because the Python interpreter is able to use the keywords provided to match the values with parameters. You can also make keyword calls to the printme ( ) function in the following ways:

Example
Demo of keyword arguments.

# Function definition
def printme(str):
“This prints a passed string into this function”
print(str)
return
# Now you can call printme function
printme(str = “My string”)RUN
My string

Example
Demo of keyword argument.

# Function definition
def info(name, age ):
“This prints a passed info into this function”
print(“Name: “, name)
print(“Age: “, age)
return
# Now you can call printinfo function
info(age=17, name=”Vivek” )RUN
>>>
Name: Vivek
Age: 17
>>>

Example 7 gives a more clear picture. Note, here the order of the parameter does not matter.

Using Keyword(named) Arguments

You can tell the function to assign certain values to specific parameters, regardless of order, if you use keyword arguments. With keyword argument, you use the actual parameter names from the function header to link a value to a parameter. So by calling the same function birthday1 ( ) with
birthday1(name=”Sai”, age=7)
the name gets “Sai” and age gets 7. The importance of keyword argument is that their order doesn’t matter. It’s the keywords that link values to parameters. So, the call
birthday1(age=7, name=”Sai”)
will produce the same result as above, i.e., the name gets “Sai” and age gets 7, even though the values are listed in the opposite order.

Default arguments

Default arguments are those that take a default value if no argument value is passed during the function call. You can assign this default value with the assignment operator =.
If any of the formal parameters in the function definition are declared with the format “arg = value,” then you will have the option of not specifying a value for those arguments when calling the function. If you do not specify a value, then that param¬eter will have the default value given when the function executes. In the above code, the variable age is not passed into the function however the default value of age is considered in the function.

Example
Demo of Default argument.

# Function definition
def info(name, age = 19 ):
“This prints a passed info into this function”
print(“Name: “, name)
print(“Age: “, age)
return
# Now you can call printinfo function
info(age=17, name=”Vivek” )
info(name=”Vivek” )RUN
>>>
Name: Vivek
Age: 17
Name: Vivek
Age: 19
>>>

Using Default Parameter Values

A parameter having a default value in the function header is known as a default parameter. In case the user does not want to provide values (argument) at the time of call, you can provide default argu¬ment values. Following is an example of a function header with default values:
def birthday2(name= “Raj”, age=5):
This means that if no value is supplied to the name, it gets “Raj”. And if no value is supplied for age, it gets 5. So the call birthday2 ( )
will not generate an error, instead, the default values are assigned to the parameters.
You can override the default values of any or all the parameters. With the call:
birthday2(name=”Dev”)
The default value of the name is overridden, the name gets “Dev”, age still gets the value 5.

Using Multiple Argument Types Together

Python allows you to combine multiple argument types in a function call. You can combine keyword argument and positional argument in a single function. But while using keyword arguments, the following should be kept in mind:

(a) An argument list must first contain positional arguments followed by any keywords arguments.

(b) Keywords in the argument list should be from the list of parameters name only.

(c) You cannot specify a value for an argument more than once.

(d) Parameter names corresponding to positional arguments cannot be used as keywords in the same calls.
Some function arguments can be given a default value so that you may leave out these arguments in the call if desired. A typical function may look as:
>>> def some fun(arg1, arg2, kwargl=4, kwarg2=8):
>>> print (arg1, arg2, kwarg1, kwarg2)
The first two arguments, arg1 and arg2, are ordinary or positional arguments, while the latter two are keyword arguments or named arguments. Each keyword argument has a name (in this example kwargl and kwarg2) and an associated default value. The keyword arguments must always be listed after the positional arguments in the function definition.
When calling some fun ( ), you may leave out some or all of the keyword arguments. Keyword arguments that do not appear in the call get their values from the specified default values. You can demonstrate the effect through some calls has summarized in Table 10.1.

Function Call Statement Legal/illegal Result
somefunc(3,2) Legal 3 24 8
somefunc(10,20,30,40) Legal 10 20 30 40
somefunc(25,50,kwarg2=100) Legal 25 50 4 100
somefunc(kwarg2=2, argl=3,arg2=4 Legal 3442

Notice that 1<sup>st</sup> and 2<sup>nd</sup> call to function is based on default argument value, and the 3rd and 4th call are using keyword arguments. In the first usage, values 3 and 2 are passed on to arg1 and arg2, respectively. Thus, they all work with default values. In the second call, all four parameters get values in the function call statement. In the third usage, variable argl gets the first value 25, arg2 gets the value 50 due to the position of the argument. And parameter kwarg2 gets the value 100 due to naming, i.e., keyword arguments. The parameter kwargl uses the default value, i.e., 4.
In the fourth usage, you use keyword argument for all specified value. Keyword arguments can be used in any order and for argument skipped, there is a default value. Table 10.2 illustrates some illegal function calls.

Function call statement Legal/illegal Result
somefunc ( ) Illegal Required or positional argument missing, i.e., somefunc ( ) takes at least 2 arguments (0 given).
somefunc(kwargl=10,kwarg2=20,30,40) Illegal Keyword arguments are placed before positional arguments.
somefunc(4,5,arg2=6) Illegal Somefunc ( ) got multiple values for keyword argument ‘arg2’.
somefunc(4,5,kwargl=5,kwarg3=6) Illegal Undefined name kwarg3 used.

Let’s Try

def info(name,age=17):
print(“My name is “,name,”and age is “,age)
info(name = “Dev”)

Variable-Length Arguments

You may need to process a function for more arguments than you specified while defining the function. These arguments are called variable-length arguments and are not named in the function definition, unlike required and default arguments. The general syntax for a function with non-keyword variable arguments is as follows:

def functionname([formalargs,] *var_args_tuple ):
“functiondocstring”
function_suite
return [expression]

An asterisk (*) is placed before the variable name that will hold the values of all non-keyword variable arguments. You use *args as a parameter in the function header. It gives us the ability to pass N (variable) number of arguments. You use *args when you aren’t sure how many arguments are going to be passed to a function, or if you want to pass a stored list or tuple of arguments to a function. You can also use the !<‘!<‘kwargs syntax in a Python function declaration. It let us pass N (variable) number of arguments which can be named or key- worded. **kwargs is used when you don’t know how many keyword arguments will be passed to a function.

Example
Python code to demonstrate *args.

# Python code to demonstrate *args
def fn(*argList):
for argx in argList:
print (argx, end=’ ‘)
fn ( ‘ Learning ‘ , ‘ Python ‘ , ‘ is ‘ , ‘ Fun ‘ )RUN
>>>
Learning python is Fun
>>>

Example
Python code to demonstrate **kwargs

# Python code to demonstrate **kwargs
def fn(**kwargs):
for stu, age in kwargs.items( ):
print(stu,”‘s Age is”,age)
fn(Dev=17, Shiv=16, Karan=18)RUN
>>>
Dev’s Age is 17
Shiv’s Age is 16
Karan’s Age is 18
>>>

This tuple remains empty if no additional arguments are specified during the function call. Following is a simple example,

Example
Demo of variable length arguments.

# Function definition
def info(argl, *vartuple ):
“This prints a variable passed arguments”
print(“Output is: “)
print(arg1)
for var in vartuple:
print(var)
return# Now call info function
info(10 )
info(70, 60, 50 )RUN
>>>
Output is:
10
Output is:
70
60
50
>>>

Let’s Try

# Python Function with No Arguments, and No Return Value FUNCTION 1
def Add1 ( ) :
a = 2
b = 3
Sum = a + b
print(“After Calling the Function:”, Sum)
Add1 ( )
# Python Function with Arguments, and No Return Value FUNCTION 2
def Add2(a,b):
Sum = a + b
print(“Result:”, Sum)
Add2(2,3)
# Python Function with Arguments, and Return Value FUNCTION 3
def Add3(a,b):
Sum = a + b
return Sum
Z=Add3(10,20)
print(“Result “,Z)
# Python Function with No Arguments, but with Return Value FUNCTION 4
def Add4 ( ):
a=11
b=20
Sum = a + b
return Sum
Z=Add3(10,12)
print(“Result “, Z)

 

Leave a Reply

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