Chapter 5: Functions & Pointers

Message Function, Page number: 159

In [1]:
# function definition
def message():
    print  "Smile, and the world smiles with you..." 

message() #function call
print "Cry, and you stop the monotony!" 
Smile, and the world smiles with you...
Cry, and you stop the monotony!

Calling More Than One Funnction , Page number: 159

In [2]:
# function definitions
def italy():
    print "I am in italy" 
    
def brazil():
    print "I am in brazil" 

def argentina():
    print  "I am in argentina" 
    

print  "I am in main" 
#function calls
italy()
brazil()
argentina()
I am in main
I am in italy
I am in brazil
I am in argentina

Function Calling Function, Page number: 161

In [3]:
# function definitions
def argentina():
    print  "I am in argentina" 

def brazil():
    print "I am in brazil" 
    argentina() #function call

def italy():
    print "I am in italy" 
    brazil() #function call
    print "I am back in italy" 
    
print  "I am in main" 
#function call
italy()
print "I am finally back in main" 
I am in main
I am in italy
I am in brazil
I am in argentina
I am back in italy
I am finally back in main

Calsum Function , Page number: 166

In [5]:
# function definition
def calsum ( x, y, z ):
    d = x + y + z
    return d

#Input from user
#a,b,c = raw_input ("Enter any three numbers: ").split()
print "Enter any three numbers: "
a = 10
b = 20
c = 30
print a,b,c

#function call
s = calsum(a,b,c) 

#Result
print "Sum = ", s 
Enter any three numbers: 
10 20 30
Sum =  60

Return Statement , Page number: 169

In [ ]:
# function definition
def fun():
    #Input from user
    #ch = raw_input ("Enter any alphabet: ")
    print "Enter any alphabet: "
    ch = 'a'
    print ch
    ch = ord(ch)
    if ch >= 65 and ch <= 90:
        return ascii(ch)
    else:
        return ascii( ch + 32 )

#function call
ch = fun()

#Result
print ch

Formal Arguments, Page number: 170

In [9]:
# function definition
def fun(b):
    b = 60
    print b 

a = 30
fun(a) #function call
print a
60
30

Scope Rule of Functions , Page number: 171

In [10]:
# function definition
def display(j):
    k = 35
    print j
    print k

i = 20 
display(i) #function call
20
35

Square Function , Page number: 176

In [11]:
# function definition
def square(x):
    y = x * x
    return y

#Input from user
#a = raw_input("Enter any number: ")
print "Enter any number: "
a = 4.5
print a

b = square(a) #function call

#Result
print "Square of %f is %f" %( a, b )
Enter any number: 
4.5
Square of 4.500000 is 20.250000

Void Function , Page number: 177

In [12]:
# function definition
def gospel(): # void function
    print "Viruses are electronic bandits..." 
    print "who eat nuggets of information..." 
    print "and chunks of bytes..." 
    print "when you least expect..." 

gospel() # function call
Viruses are electronic bandits...
who eat nuggets of information...
and chunks of bytes...
when you least expect...

Address of a Variable , Page number: 180

In [13]:
#Variable declaration
i = 3

#Result
print  "Address of i = " , id(i)  #printing address
print  "Value of i = ", i 
Address of i =  30301288
Value of i =  3

Pointer Relations , Page number: 182

In [14]:
#Variable declaration
i = 3 
j = id(i) # address of variable 'i'

#Result
print  "Address of i = ", id(i)  
print  "Address of i = ", j 
print  "Address of j = ", id(j)
print  "Value of j = ", j  
print  "Value of i = ", i  
print  "Value of i = ", i  
print  "Value of i = ", i 
Address of i =  30301288
Address of i =  30301288
Address of j =  134133200
Value of j =  30301288
Value of i =  3
Value of i =  3
Value of i =  3

Pointer to Another Pointer , Page number: 184

In [15]:
#Variable declaration
i = 3
j = id(i) # address of i
k = id(j) # address of j

#Result
print  "Address of i = ", id(i) 
print  "Address of i = ", j 
print  "Address of i = ", j 
print  "Address of j = ", id(j)  
print  "Address of j = ", k  
print  "Address of k = ", id(k)
print  "Value of j = ", j  
print  "Value of k = ", k 
print  "Value of i = ", i  
print  "Value of i = ", i  
print  "Value of i = ", i  
print  "Value of i = ", i 
Address of i =  30301288
Address of i =  30301288
Address of i =  30301288
Address of j =  134132944
Address of j =  134132944
Address of k =  134133200
Value of j =  30301288
Value of k =  134132944
Value of i =  3
Value of i =  3
Value of i =  3
Value of i =  3

Call By Value , Page number: 186

In [1]:
# function definition
def swapv (x,y):
    x,y=y,x
    print "x = %d y = %d" %( x, y )

#Variable declaration
a = 10
b = 20

swapv ( a, b ) # function call

#Result
print  "a = %d b = %d" %( a, b )
x = 20 y = 10
a = 10 b = 20

Call By Reference , Page number: 187

In [2]:
# function definition
def swapv (a,b):
    a,b=b,a
    return a,b

#Variable declaration
a = 10
b = 20

a,b = swapv ( a, b ) # function call

#Result
print "a = %d b = %d" %( a, b )
a = 20 b = 10

Area And Perimeter of a Circle , Page number: 188

In [18]:
print "Enter radius of a circle: "
radius = 5
print radius

#Function definition
def areaperi ( r ):
    a = 3.14 * r * r
    p = 2 * 3.14 * r 
    return a,p

area,perimeter = areaperi ( radius ) #function call


#Result
print  "Area = ", area 
print  "Perimeter = ", perimeter  
Enter radius of a circle: 
5
Area =  78.5
Perimeter =  31.4

Iterative Factorial Function , Page number: 190

In [19]:
# function definition
def factorial(x):
    f = 1
    for i in range(x,0,-1 ):
        f = f * i
    return f


print "Enter any number: "
a = 6
print a

fact = factorial(a) #function call

#Result
print "Factorial value = ", fact
Enter any number: 
6
Factorial value =  720

Recursive Factorial Function, Page number: 191

In [20]:
# function definition
def rec(x):
    if x == 1:
        return 1
    else:
        f = x * rec ( x - 1 ) # calling the function
    return f


print "Enter any number: "
a = 5
print a

fact = rec(a) #function call

#Result
print "Factorial value = ", fact 
Enter any number: 
5
Factorial value =  120

Recursion Stack , Page number: 195

In [21]:
#Variable declaration
a = 5
b = 2

#Function definition
def add ( i,  j ):
    s = i + j
    return s

c = add ( a, b ) # function call

#Result
print  "sum = ", c 
sum =  7
In [ ]: