0% found this document useful (0 votes)
15 views7 pages

Types of Arguements

Uploaded by

hod.it
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
15 views7 pages

Types of Arguements

Uploaded by

hod.it
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 7

Functions

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

Invalid Default Argument:


def greet_msg1(name="Geetu",msg="Good Morning"):
print("Hello",name,"Good Morning")

def greet_msg2(name,msg="Good Morning"):


print(name,msg)

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

Invalid Keyword Argument:


def greet_msg(name,msg):
print("Hello",name,msg)

#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.

Advantages of writing functions with keyword arguments:


a) Using the function with keyword arguments is easier as we do
not need to remember the order of the arguments.
b) We can specify values of only those parameters which we want
to, as other parameters have default argument values.

One more example


def fun(a,b=1,c=5):
print("a is",a,"b is", b,"c is",c)

# 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

4. Variable length arguments:


As the name suggests , in certain situations , we can pass variable
number of arguments to a function. Such type of arguments are
called variable length arguments.
Variable length arguments are declared with *(asterisk)
Example:

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

A global variable can be used anywhere in the code.


In the example below we define a global variable z
The global variable z can be used all throughout the program,
inside functions or outside.
A global variable can modified inside a function and change for the
entire program.
After calling afunction(), the global variable is changed for the entire
program.

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

You might also like