Home | | Computer Science 12th Std | Function Arguments

Python - Function Arguments | 12th Computer Science : Chapter 7 : Core Python : Python Functions

Chapter: 12th Computer Science : Chapter 7 : Core Python : Python Functions

Function Arguments

Arguments are used to call a function and there are primarily 4 types of functions that one can use: Required arguments, Keyword arguments, Default arguments and Variable-length arguments.

Function Arguments

Arguments are used to call a function and there are primarily 4 types of functions that one can use: Required arguments, Keyword arguments, Default arguments and Variable-length arguments.


 

1. 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. You need atleast one parameter to prevent syntax errors to get the required output.

Example :

def printstring(str):

print ("Example - Required arguments ")

print (str)

return

# Now you can call printstring() function

printstring()

When the above code is executed, it produces the following error.

Traceback (most recent call last):

File "Req-arg.py", line 10, in <module>

printstring()

TypeError: printstring() missing 1 required positional argument: 'str'

Instead of printstring() in the above code if we use printstring (“Welcome”) then the output is

Output:

Example - Required arguments

Welcome

 

2. Keyword Arguments

Keyword arguments will invoke the function after the parameters are recognized by their parameter names. The value of the keyword argument is matched with the parameter name and so, one can also put arguments in improper order (not in order).

Example:

def printdata (name):

print (“Example-1 Keyword arguments”)

print (“Name :”,name)

return

# Now you can call printdata() function

printdata(name = “Gshan”)

When the above code is executed, it produces the following output :

Output:

Example-1 Keyword arguments

Name :Gshan

Example:

def printdata (name):

print (“Example-2 Keyword arguments”)

print (“Name :”, name)

return

# Now you can call printdata() function

printdata (name1 = “Gshan”)

When the above code is executed, it produces the following result :

TypeError: printdata() got an unexpected keyword argument 'name1'

Example:

def printdata (name, age):

print ("Example-3 Keyword arguments")

print ("Name :",name)

print ("Age :",age)

return

# Now you can call printdata() function

printdata (age=25, name="Gshan"

When the above code is executed, it produces the following result:

Output:

Example-3 Keyword arguments

Name : Gshan

Age : 25

Note

In the above program the parameters orders are changed

 

3. Default Arguments

In Python the default argument is an argument that takes a default value if no value is provided in the function call. The following example uses default arguments, that prints default salary when no argument is passed.

Example:

def printinfo( name, salary = 3500):

print (“Name: “, name)

print (“Salary: “, salary)

return

printinfo(“Mani”)

When the above code is executed, it produces the following output

Output:

Name: Mani

Salary: 3500

When the above code is changed as print info(“Ram”,2000) it produces the following output:

Output:

Name: Ram

Salary: 2000

In the above code, the value 2000 is passed to the argument salary, the default value already assigned for salary is simply ignored.

 

4. Variable-Length Arguments

In some instances you might need to pass more arguments than have already been specified. Going back to the function to redefine it can be a tedious process. Variable-Length arguments can be used instead. These are not specified in the function’s definition and an asterisk (*) is used to define such arguments.

Lets see what happens when we pass more than 3 arguments in the sum() function.

Example:

def sum(x,y,z):

print("sum of three nos :",x+y+z)

sum(5,10,15,20,25)

When the above code is executed, it produces the following result :

TypeError: sum() takes 3 positional arguments but 5 were given

Syntax - Variable-Length Arguments

def function_name(*args):

function_body

return_statement

Example:

def printnos (*nos):

for n in nos:

print(n)

return

now invoking the printnos() function

 print ('Printing two values')

printnos (1,2)

print ('Printing three values')

printnos (10,20,30)

Output:

Printing two values

1

2

Printing three values

10

20

30

In Variable Length arguments we can pass the arguments using two methods.

1. Non keyword variable arguments

2. Keyword variable arguments

Non-keyword variable arguments are called tuples. You will learn more about tuples in the later chapters. The Program given is an illustration for non keyword variable argument.

 

Tags : Python , 12th Computer Science : Chapter 7 : Core Python : Python Functions
Study Material, Lecturing Notes, Assignment, Reference, Wiki description explanation, brief detail
12th Computer Science : Chapter 7 : Core Python : Python Functions : Function Arguments | Python


Privacy Policy, Terms and Conditions, DMCA Policy and Compliant

Copyright © 2018-2024 BrainKart.com; All Rights Reserved. Developed by Therithal info, Chennai.