Home | | Problem Solving and Python Programming | Parameters and Arguments - Python

Chapter: Problem Solving and Python Programming : Data, Expressions, Statements

Parameters and Arguments - Python

Parameters are the value(s) provided in the parenthesis when we write function header.

Parameters And Arguments:

 

Parameters:

·                 Parameters are the value(s) provided in the parenthesis when we write function header.

·                 These are the values required by function to work.

·                 If there is more than one value required, all of them will be listed in parameter list separated by comma.

·                 Example: def my_add(a,b):

 

Arguments :

·                 Arguments are the value(s) provided in function call/invoke statement.

·                 List of arguments should be supplied in same way as parameters are listed.

·                 Bounding of parameters to arguments is done 1:1, and so there should be same number and type of arguments as mentioned in parameter list.

·                 Example: my_add(x,y)

 

RETURN STATEMENT:

 

·                 The return statement is used to exit a function and go back to the place from where it was called.

·                 If the return statement has no arguments, then it will not return any values. But exits from function.

 

Syntax:

return[expression]

 

Example:

def my_add(a,b):

c=a+b

return c

x=5

y=4

 

print(my_add(x,y))

 

Output:

9

 

ARGUMENTS TYPES:

 

1.       Required Arguments

2.       Keyword Arguments

3.       Default Arguments

4.       Variable length Arguments

 

1. Required Arguments:

The number of  arguments in the function call should match exactly with the function definition.

def my_details( name, age ):

print("Name: ", name)

print("Age ", age)

return

my_details("george",56)

Output:

Name: george

Age 56

 

2. Keyword Arguments:

Python interpreter is able to use the keywords provided to match the values with parameters even though if they are arranged in out of order.

def my_details( name, age ):

print("Name: ", name)

print("Age ", age)

return

my_details(age=56,name="george")

Output:

Name: george

Age 56

 

3. Default Arguments:

Assumes a default value if a value is not provided in the function call for that argument.

def my_details( name, age=40 ):

print("Name: ", name)

print("Age ", age)

return

my_details(name="george")

Output:

Name: george

Age 40

 

4. Variable length Arguments

 

If we want to specify more arguments than specified while defining the function, variable length arguments are used. It is denoted by * symbol before parameter.

def my_details(*name ):

print(*name)

my_details("rajan","rahul","micheal",

ärjun")

Output:

rajan rahul micheal ärjun

 


Study Material, Lecturing Notes, Assignment, Reference, Wiki description explanation, brief detail
Problem Solving and Python Programming : Data, Expressions, Statements : Parameters and Arguments - Python |

Related Topics

Problem Solving and Python Programming : Data, Expressions, Statements


Privacy Policy, Terms and Conditions, DMCA Policy and Compliant

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