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

Types of Arguments,Scope of Variables-Notes

The document covers the concepts of user-defined functions in Python, focusing on types of arguments (positional, default, keyword, and variable-length) and the scope of variables (local and global). It provides examples and explanations of how to use these arguments and the differences between local and global variables. Additionally, it includes activities for practical understanding and application of the concepts discussed.

Uploaded by

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

Types of Arguments,Scope of Variables-Notes

The document covers the concepts of user-defined functions in Python, focusing on types of arguments (positional, default, keyword, and variable-length) and the scope of variables (local and global). It provides examples and explanations of how to use these arguments and the differences between local and global variables. Additionally, it includes activities for practical understanding and application of the concepts discussed.

Uploaded by

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

TYPES OF ARGUMENTS,

SCOPE OF VARIABLES
PRE-TEST
• Write a user defined function countVowels() that accept a
string as a parameter. The function should count and display
the total number of vowels present in the given string.
Learning Objective
• To explore the concepts of different types of arguments and
scope of variables in a user defined functions.
Learning Outcome
• After the completion of the topic, students will be able to
execute python programs with user defined functions that
accepts various types of arguments.
• Also, students will be able to identify the differences between
local variable & global variables in python programs.
Types of Arguments- Introduction
Example,
def f1(x,y):
statements
f1(10,20)
• In the above example, 10,20 are actual arguments whereas, x & y are formal arguments.
• In Python, there are only 4 types of actual arguments:
*Positional Arguments
*Default Arguments
*Keyword Arguments
*Variable length Arguments
Positional Arguments
• These are arguments passed to the actual arguments of a function in correct positional order i.e.,
in the same order as in the function header.
• The number and position of arguments must be matched.
• If we change their order, the result will change.
• If we change the number of arguments ,it will result in an error.
• Example:
def subtract(a,b):
print(a-b)
subtract(100,200) gives result is -100
subtract(200,100) gives result 100
subtract(200,100,300) gives an error
Default Arguments
• It is an argument that can assign a default value if a value is not provided in the function call for that
argument.
• The value is assigned at the time of function definition by using ‘=‘ operator.
• Positional arguments must appear before the default arguments, otherwise it results in Syntax error.
• Example
def interest(principal, rate, time=15):
def interest(principal, rate=8.5, time=15): Valid Function Header

def interest(principal, rate=8.5, time): Invalid Function Header


• If we are not passing any value to argument, then only default arguments will be
considered.
• But if a value is provided in the actual argument, it will overwrite the default value.
• Example:

def fn1(name=“Anu”):
print(“Hello”,name,”Good Morning”)
fn1(“Reshma”) Hello Reshma Good Morning
fn1() Hello Anu Good Morning
Keyword(Name) Arguments
• In a function call, we can specify values for some parameters using their name instead of the
position/order. These are called Keyword Arguments or Named Arguments .

• This allows calling function with arguments in any order using name of arguments.
• Example:
def fn1(name,msg):
print(“Hello”,name,msg)
fn1(name=“Anu”, msg=“Good Morning”) Hello Anu Good Morning
fn1(msg=“Good Morning”, name=“Reshma”) Hello Reshma Good Morning
• We can use both positional & keyword arguments simultaneously.
• But first we have to specify the positional argument and then keyword argument, otherwise,
it will generate Syntax Error “Positional Argument follows Keyword
Argument.
Example:
def fn1(name,msg):
print(“Hello”,name,msg)
fn1(name=“Anu”, msg=“Good Morning”)
fn1(msg=“Good Morning”, name=“Reshma”) # Valid

fn1(name=“Ram”,”Good Morning”) # Invalid


Example
def fun(a, b=1, c=5):
print(“a is”, a, “b is”, b, “c is”, c)
fun(3)
Output : a is 3 b is 1 c is 5
fun(3, 7, 10)
Output: a is 3 b is 7 c is 10
fun(25, c=20)
Output: a is 25 b is 1 c is 20
fun(c=20, a=10) # Order doesn’t matter since we use keyword arguments
Output: a is 10 b is 1 c is 20
Points to Remember while using Keyword arguments:

• An argument list must have any positional arguments followed by keyword


arguments.
• Keywords in argument list should be from the list of parameters name only.
• No parameter should receive values more than once.
• Parameter names corresponding to positional arguments cannot be used as
keywords in the same calls.
Example
def Average(n1, n2, n3=1000):
return (n1+n2+n3)/3
FUNCTION CALL LEGAL/ILLEGAL REASON

Average(n1=20, n2=40, n3=80) Legal Non-default values provided as named


argument.
Average(n3=10, n2=7, n1=100) Legal Keyword arguments can be in any order.

Average(100, n2=10, n3=15) Legal Positional argument before Keyword argument.

Average(n3=70, n1=90, 100) Illegal Keyword argument before positional argument.

Average(100, n1=23, n2=1) Illegal Multiple values provided for n1.

Average(20, num=9, n3=11) Illegal Undefined argument num.


Variable length Arguments
• We can pass variable number of arguments to a function using ‘*’ (asterik) symbol preceded with a
variable identifier in the parameter list.
• We can call this function by passing any number of arguments including Zero.
• Example:
def sum(*n):
total=0
for i in n:
total =total+i
print(“Sum is”,total)
sum() Sum is 0
sum(20) Sum is 20
sum(20,30) Sum is 50
sum(10,20,30,40) Sum is 100
Activity
1. Which line in the given code will not work and why?
def Interest(P, R, T=7):
I=(P*R*T)
print(I)
print(Interest(20000, 08, 15)) # Line 1
print(Interest(T=10, 20000, 0.75)) # Line 2
print(Interest(5000, .07)) # Line 3
print(Interest(P=10000, R=.06, Time=8)) # Line 4
print(Interest(80000, T=10) # Line 5
Activity
2. Find the output for the following code:
def interest(prnc, time=2, rate=0.10):
return (prnc * time * rate)
print(interest(6100,1))
print(interest(5000, rate=0.05))
print(interest(5000, 3, 0.12))
print(interest(time=4, prnc=5000))
Scope of a Variable
• Refers to the part of the program where variable is recognised.
• Part of the program in which variable can be used.
• Two types of scope of variable: Local Scope & Global Scope
Local Scope:
• Variables declared inside the function definition.
• Scope is only inside that function.
• Outside function, this variable cannot be accessed.
• Example:
def fun(): Output
x=20 # Local Variable Inside Function 20
print(“Inside Function”,x) Error:name ‘x’ is not defined
fun()
print(“Outside Function”,x)
Global Scope:
• Variables declared outside the function definition are global variables.
• Scope is in the whole program.
• Global variables are accessible inside as well as outside the function.
• Example:
A=25 # Global Variable Output
def fn(): A inside Function: 25
print(“A inside Function:”,A) A outside function :25
fn()
print(“A outside Function:”,A)
• If a same variable is declared inside and outside the function, Priority will be given to local variable.
• Example:
A=30 # global variable
def fn():
A=20 # Local Variable
print(A)
print(“A inside Function”,A) # inside function, local variable is accessed.
fn()
print(“A outside Function”,A) # outside function, global variable is accessed.
Output
20
A inside Function 20
A outside Function 30
• Values of the global variable can be modified using global keyword.
• If we declare a global variable as global in function definition and then modify its value, then that
variable is assigned modified value outside the function definition.
• i.e., Change in global variable inside the function definition will reflect in outside function definition.
• Example:
A=20 #Global Variable
def fun():
global A
A=A+30
print(“A inside Function:”,A) Output
fun() A inside Function:50
print(“A outside Function:”,A) A outside Function:50
Example
B=30 # Global Variable
def fun():
global B # global keyword used
B=20
print(“B inside Function:”,B)
print(“B before Function call:”,B)
fun()
print(“B after Function call:”,B)
Output
B before Function call:30
B inside Function call:20
B after Function call:20
Difference between Local & Global Variable

S.No Local Variable Global Variable

1 It is a variable which is declared within a It is a variable which is declared outside


function or within a block. all the functions or in a global space.

2 It cannot be accessed outside the function It is accessible throughout the program in


but only within a function/block of program. which it is declared.
Activity
1. What is the output of the following code?
x=60
def f1():
global x
print(“x is :”,x)
x=10
print(“Changed global x is:”,x)
x+=5
f1()
print(“Value of x is”,x)
Activity
2. Find the output and write the local and global variables for the following code:
value=50
def display(N):
global value
value=25
if N%7==0:
value=value+N
else:
value=value-N
print(value,end=‘#’)
display(20)
print(value)
LET’S RECAP…..
• Types of Arguments in User defined functions.
• Local Scope
• Global Scope

You might also like