Scope and argument types
Scope and argument types
Scope of Variables
All variables in a program may not be accessible at all locations in that program. This
depends on where you have declared a variable.
The scope of a variable determines the portion of the program where you can access a
particular variable/identifier. There are two basic types of scopes of variables in Python
:
Global variables
Local variables
def out( ):
a=20 # Here sum is local variable.
print ("Inside the function local a : ",a)
return
Ex.3
Live Demo
sum = 0 # This is global variable.
Def out( a, b ):
sum = a+b # Here sum is local variable.
Print (“Inside the function local sum : “, sum)
return sum
You can call a function by using the following types of formal arguments –
Required arguments
Keyword arguments
Default arguments
Variable-length arguments
Required Arguments
Required arguments are the arguments passed to a function in correct positional order.
Here, the number of arguments in the function call should match exactly with the
function definition
.
Ex.1. To call the function fun(), you need to pass one argument, otherwise it gives a
syntax error like−
def fun( a ):
print (a)
fun()
Live
When the above code is executed, it gives the error as follows –
def out(st):
print(st)
return
out(st=”My school”)
Answer:__________
The following example gives you more clearer picture. Note that the order of
parameters does not matter.
Ex.5Live Demo
def out( name, roll ):
print (“Name: “, name)
print (“Roll: “, roll)
return
out(roll= 50, name = “rajni” )
Ex.6Live Demo
for i in var:
print (i)
return
out( 100 )
out( 200, 300, 400 )
*****