0% found this document useful (0 votes)
16 views

Lect-02 - Types of Arguments

Uploaded by

YASH WANKHEDE
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
16 views

Lect-02 - Types of Arguments

Uploaded by

YASH WANKHEDE
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 13

Functions in Python

Harish D. Gadade
Parameters and Arguments
● Parameters are used to give inputs to a function. They are specified
with the pair of parentheses in the function definition. Also called
formal parameters

● Arguments are the values actually passed to a function when calling it.
Thus, parameters define what types of arguments a function can accept.
It is also called as actual parameters, i.e. Actual parameters are
called as arguments
def add(a,b): # Parameters
c=a+b
print(c)
add(10,20) # Arguments
2
Prof. Harish D. Gadade, COEP Technological University, Pune
Positional Arguments
#Positional Argument #Positional Argument
def display(name,age): def display(name,age):
print("Name = ",name, "\nAge = ",age) print("Name = ",name, "\nAge = ",age)

display("Ahaan",6) display("Ahaan")

TypeError: display() missing 1 required


#Positional Argument
positional argument: 'age'
def display(name,age):
print("Name = ",name, "\nAge = ",age)

display(6,"Ahaan")
3
Prof. Harish D. Gadade, COEP Technological University, Pune
Keyword Arguments
● An alternative to positional argument is keyword Arguments
● If programmer knows the parameter name used within the function, then
we can explicitly use the parameter name while calling the function.
● A programmer can pass a keyword argument to a function by using its
corresponding parameter name rather than its position.
● This is done by simply typing parameter_name=Value in the function
call.
#Keyword Arguments
def display(name,age):
print("Name = ",name, "\nAge = ",age)

display(age=6,name="Ahaan")
4
Prof. Harish D. Gadade, COEP Technological University, Pune
Parameters with Default Values
● Parameters within a function can have default values. We can provide
default value to a parameter by using the assignment(=) operator..

#Parameters with Default Values def area(pi=3.14,r=1):


def display(name,msg="You are Cool"): a=pi*r*r
print(name,msg) return a
display("Harish","Welcome") a=area(r=2)
print(a)

5
Prof. Harish D. Gadade, COEP Technological University, Pune
Local and Global Scope of a Variable
● Variables and parameters that are initialised within a function
including parameters, are said to exist in that function’s local scope.
Variables that exist in local scope are called Local Variables.
● Variables that are assigned outside functions are said to exist in
global scope. Therefore, variables that exist in global scope are
called global variables

def sample():
b=10
print("The value of Local Variable b : ",b)
print("The value of Global Variable a : ",a)
a=20
sample()
print("The value of Global Variable a : ",a) 6
Prof. Harish D. Gadade, COEP Technological University, Pune
Local and Global Scope of a Variable
● Local Variables cannot be used in Global Space

def sample():
b=10
print("The value of Local Variable b : ",b)

sample()
print("The value of Local Variable b : ",b) #Error

7
Prof. Harish D. Gadade, COEP Technological University, Pune
Local and Global Scope of a Variable
● Reading Global variables from Local Scope

def sample():
print(a)
a="COEP, Pune"
sample()

8
Prof. Harish D. Gadade, COEP Technological University, Pune
Local and Global Scope of a Variable
● Local and Global variable with same name

def sample():
a="COEP, Pune"
print(a)

a="Welcome to COEP" Output:


sample() COEP, Pune
print(a) Welcome to COEP

9
Prof. Harish D. Gadade, COEP Technological University, Pune
Local and Global Scope of a Variable
Global Statement
● Consider a situation where a programmer needs to modify the value
of a global variable within a function.
● In such a situation, we have to make use of global statement

a=10
def display():
a=30
print("The value of a in Function : ",a)
Output:
The value of a in Function : 30
display()
The value of a outside Function : 10
print("The value of a outside Function : ",a)
10
Prof. Harish D. Gadade, COEP Technological University, Pune
Local and Global Scope of a Variable
Global Statement
● Consider a situation where a programmer needs to modify the value
of a global variable within a function.
● In such a situation, we have to make use of global statement

a=10
def display():
global a
a=30
print("The value of a in Function : ",a)
Output:
The value of a in Function : 30
display()
The value of a outside Function : 30
print("The value of a outside Function : ",a)
11
Prof. Harish D. Gadade, COEP Technological University, Pune
Return Statements
● The return statement is used to return a value from the function.
It is also used to return from a function, i.e. break out of the
function
def minimum(a,b):
if a<b:
return a
elif b<a:
return b
else:
return "Both are equal Numbers"

min=minimum(10,20)
print(min)
12
Prof. Harish D. Gadade, COEP Technological University, Pune
Returning Multiple Values
● It is possible to return multiple values in python

def swap(a,b):
a,b=b,a
return a,b

a=10
b=20
a,b=swap(a,b)
print(a,b)

13
Prof. Harish D. Gadade, COEP Technological University, Pune

You might also like