Types of Arguements
Types of Arguements
Types of arguments
1. Positional arguments
2. Default arguments
3. Keyword arguments
4. Variable length arguments
1. Positional Arguments:
Positional arguments are arguments passed to a function in correct
positional order
Example:
def add(a,b):
return a+b
#main
a=10
add(a)
After execution
Traceback (most recent call last):
File "C:/Meena classes/All/arguments in functions/pos.py", line 7, in
<module>
add(a)
TypeError: add() missing 1 required positional argument: 'b'
2. Default Arguments
Default arguments are arguments that assumes default value if a
value is not provided in the function call for that argument.
Example:
def greet_msg(name="geetu"):
print("Hello",name,"Good Morning")
pg. 1
greet_msg("vinay")
greet_msg()
After execution:
Hello vinay Good Morning
Hello geetu Good Morning
def greet_msg3(name='abc',msg):
print(name,msg)
#Main
greet_msg1()
greet_msg2("Vinay")
greet_msg3("Good Morning") # Invalid
When we debug:
---------------------------
SyntaxError
---------------------------
non-default argument follows default argument
---------------------------
OK
pg. 2
3. Keyword Arguments:
If there is a function with many parameters and we want to specify
only some of them in the function call, then the value of such
parameters can be provided by using name instead of position.
These are called keyword arguments or named arguments.
Example:
def greet_msg(name,msg):
print("Hello",name,msg)
#Main
greet_msg(name='vinay',msg='Good Morning')
greet_msg(msg='Good Morning',name='Sonia')
After Execution:
Hello vinay Good Morning
Hello Sonia Good Morning
#Main
greet_msg(name='vinay',msg='Good Morning')
greet_msg(msg='Good Morning',name='Sonia')
greet_msg(name="Radhika","Good Morning") #Invalid
After debug:
---------------------------
SyntaxError
---------------------------
positional argument follows keyword argument
---------------------------
OK
pg. 3
Definition Keyword Arguments:
Keyword arguments are named arguments with assigned values
being passed in the function call statement.
# Main
fun(3)
fun(3,7,10)
fun(25,c=20)
fun(c=20,a=10) # Order does not matter
After Execution:
a is 3 b is 1 c is 5
a is 3 b is 7 c is 10
a is 25 b is 1 c is 20
a is 10 b is 1 c is 20
pg. 4
def sum(*n):
total=0
for i in n:
total=total+i
print("The sum =",total)
sum()
sum(20)
sum(20,30)
sum(10,20,30,40)
After Execution:
The sum = 0
The sum = 20
The sum = 50
The sum = 100
Scope of variables:
Scope of variables refers to the part of the program where it is
visible.Area where you can refer it.
Global Scope:
Names assigned at the top level of a module, or directly in the
interpreter.
Name declared global in the function
Local Scope:
Names assigned inside a function definition or loop.
Example:
def f(x): # x is function parameter it is local
y=x+a # y is assigned inside function, it is local
pg. 5
return y
#Main
a=2 # a is assigned in the interpreter it is global
p=f(5)
print(p)
After execution:
7
Global variables
Example:
z = 10 # Global declaration
def afunction():
global z # Will replace interpreter declaration.
z=9
print(z)
#Main
print(z)
afunction()
print(z)
pg. 6
After Execution:
10
9
9
One More Example
z = 10
def func1():
global z
z=3
def func2(x,y):
global z
return x+y+z
#Main
print(z)
func1()
total = func2(4,5)
print(total)
After Execution:
10
12
pg. 7