Chapter 07 : User Defined Functions

Example: 1, page no.5.49

In [2]:
#Program to demonstrate Local and Global Declaration 

#variable declaration globally
i=0                
print "Value of i in main", i

 #define function
def f1():        
    k=0
    i=50
    print "Value of i after function call",i
    
#Function Call
f1()             
Value of i in main 0
Value of i after function call 50

Example: 2, page no: 5.50

In [14]:
#Program to demonstrate usage of local and global variable

for i in range(0,3):
    print "Value of i is:", i
    
i=590
print "The Value of i is:", i    
Value of i is: 0
Value of i is: 1
Value of i is: 2
The Value of i is: 590

Example: 3, Page no.5.52

In [24]:
#Program to call function without parameters
def message():
    print "Main Message"
    
print "Function Message"

message()
Function Message
Main Message

Example: 4, Page No.5.52

In [25]:
#Program to call function with parameter

#Defining function
def add(x,y):
    z=0
    z=x+y
    return z

k=add(10,20)

#Result
print "The Value of k is ", k
The Value of k is  30

Example: 5, page no.5.53

In [3]:
#Program to write prototype of the function

k=0

#Defining in function
def f1(k):
    k=k+100
    return k

#Result
print "The value of i before call: ", k

print "The Value of i after call: ",(f1(k))
The value of i before call:  0
The Value of i after call:  100

Example: 6, page no. 5.54

In [4]:
#Program to write prototype definition in main function

def fun(k):
    k=k+10
    return k

i=0

#Result
print  "The i value before call %d\n"%(i)
print "The i value after call %d\n"%fun(i)
The i value before call 0

The i value after call 10