CHAPTER 5: FUNCTIONS AND POINTERS

EXAMPLE ON PAGE:144

In [1]:
 
def message():                                          #function definition
    print "Smile,and the world smiles with you...\n"
message()                                               #function call
print "Cry, and you stop the monotony!\n"
Smile,and the world smiles with you...

Cry, and you stop the monotony!

EXAMPLE ON PAGE:146

In [2]:
 
def italy():
    print "I am in italy\n"
def brazil():
    print "I am in brazil\n"
def argentina():
    print "I am in argentina\n"
print "I am in main\n"
italy();                                                #italy() will be called
brazil();                                               #brazil() will be called
argentina();                                            #argentina() will be called
I am in main

I am in italy

I am in brazil

I am in argentina

EXAMPLE ON PAGE:147

In [3]:
 
def italy():
    print "I am in italy\n"
    brazil();                                                   #it will call brazil()
    print "I am back in italy\n"
def brazil():
    print "I am in brazil\n"
    argentina();                                                #it will call argentina()
def argentina():
    print "I am in argentina\n"
print "I am in main\n"
italy();                                                        #italy() is called
print "I am finally back in main\n"
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

EXAMPLE ON PAGE:148

In [ ]:
 
def message():
    print "Can't imagine life without C\n"
    main()                                               #it will call back main()
def main():
    message()
main()

EXAMPLE ON PAGE:149

In [4]:
 
def message():
    print "Jewel Thief!!\n"
message()                                             #1st call
message()                                             #2nd call
Jewel Thief!!

Jewel Thief!!

EXAMPLE ON PAGE:149

In [5]:
 
def message2():
    print "But the butter was bitter\n"
def message1():
    print "Mary bought some butter\n"
message1()
message2()
Mary bought some butter

But the butter was bitter

EXAMPLE ON PAGE:151-152

In [6]:
 
def calsum(x,y,z):                                           #parameterized function
    d=x+y+z
    return d
print "Enter any three numbers"
a=eval(raw_input())
b=eval(raw_input())
c=eval(raw_input())
sum=calsum(a,b,c)                                            #passing values as arguments
print "Sum=%d\n" % (sum)
Enter any three numbers
10
20
30
Sum=60

EXAMPLE ON PAGE:153

In [7]:
 
def fun():
    print "Enter any number"
    n=eval(raw_input())
    if (n>=10 and n<=90):
        return n
    else:
        return n+32
fun()
Enter any number
100
Out[7]:
132

EXAMPLE ON PAGE:154

In [8]:
 
def fun(b):
    b=60
    print "%d\n" % (b)                     #prints 60
a=30
fun(a)                                     #prints 30
print "%d\n" % (a)
60

30

EXAMPLE ON PAGE:155

In [9]:
 
def display(j):
    k=35
    print "%d\n" % (j)              #we can't print i directly here because scope of variable is local by default
    print "%d\n" % (k)
i=20
display(i)
20

35

EXAMPLE ON PAGE:158-159

In [10]:
 
def square(x):
    y=x*x
    return y
print "Enter any number"
a=eval(raw_input())
b=square(a)
print "Square of %f is %f\n" % (a,b)
Enter any number
2.5
Square of 2.500000 is 6.250000

EXAMPLE ON PAGE:161

In [12]:
 
i=3
print "Address of i=%u\n" % (id(i))                           #id() will return the loaction of a variable
print "Value of i=%d\n" % (i)
Address of i=20688560

Value of i=3

EXAMPLE ON PAGE:161

In [13]:
 
i=3
print "Address of i=%u\n" % (id(i))
print "Value of i=%d\n" % (i)
print "Value of i=%d\n" % (i)
Address of i=20688560

Value of i=3

Value of i=3

EXAMPLE ON PAGE:162

In [14]:
 
i=3
j=id(i)
print "Address of i=%u\n" % (id(i))                 #print address of i
print "Address of i=%u\n" % (j)                     #print address of i
print "Address of j=%u\n" % (id(j))                 #print address of j
print "Value of j=%u\n" % (j)                       #print value of j
print "Value of i=%d\n" % (i)
print "Value of i=%d\n" % (i)
print "Value of i=%d\n" % (i)
Address of i=20688560

Address of i=20688560

Address of j=88986780

Value of j=20688560

Value of i=3

Value of i=3

Value of i=3

EXAMPLE ON PAGE:164

In [15]:
 
i=3
j=id(i)
k=id(j)
print "Address of i=%u\n" % (id(i))                    #print address of i
print "Address of i=%u\n" % (j)                        #print address of i
print "Address of i=%u\n" % (j)                        #print address of i
print "Address of j=%u\n" % (id(j))                    #print address of i
print "Address of j=%u\n" % (k)                        #print address of j
print "Address of k=%u\n" % (id(k))                    #print address of k
print "Value of j=%u\n" % (j)                          #print value of j
print "Value of k=%u\n" % (k)                          #print value of k
print "Value of i=%u\n" % (i)                          #print value of i
print "Value of i=%u\n" % (i)
print "Value of i=%u\n" % (i)
print "Value of i=%u\n" % (i)
Address of i=20688560

Address of i=20688560

Address of i=20688560

Address of j=88986672

Address of j=88986672

Address of k=88986708

Value of j=20688560

Value of k=88986672

Value of i=3

Value of i=3

Value of i=3

Value of i=3

EXAMPLE ON PAGE:166

In [1]:
 
def swapv(x,y):
    x,y=y,x
    print "x=%d y=%d\n" % (x,y)
a=10
b=20
swapv(a,b)
print "a=%d b=%d\n" % (a,b)
x=20 y=10

a=10 b=20

EXAMPLE ON PAGE-167

In [1]:
 
def swapr():
    global a,b                                           #global declaration
    a,b=b,a
a=10
b=20
swapr()
print "a=%d b=%d\n" % (a,b)
a=20 b=10

EXAMPLE ON PAGE:167-168

In [22]:
 
def areaperi(r,a,p):
    a=3.14*r*r                                         #formula of area
    p=2*3.14*r                                         #formula of perimeter
    print "Area=%f\n" % (a)
    print "Perimeter=%f\n" % (p)
area=0
perimeter=0
print "Enter radius of a circle"
radius=eval(raw_input())
areaperi(radius,area,perimeter)
Enter radius of a circle
5
Area=78.500000

Perimeter=31.400000

EXAMPLE ON PAGE:169

In [23]:
 
def factorial(x):
    f=1
    for i in range(x,1,-1):
        f=f*i
    return f
print "Enter any number"
a=eval(raw_input())
fact=factorial(a)
print "Factorial value=%d\n" % (fact)
Enter any number
3
Factorial value=6

EXAMPLE ON PAGE:170

In [24]:
 
def rec(x):
    if x==1:
        return 1
    else:
        f=x*rec(x-1)                        
    return f                                      #will call back the rec() function
print "Enter any number"
a=eval(raw_input())
fact=rec(a)
print "Factorial value=%d\n" % (fact)
Enter any number
5
Factorial value=120

EXAMPLE ON PAGE:173

In [25]:
 
def add(i,j):
    sum=i+j
    return sum
a=5
b=2
c=add(a,b)                                          #Transfers control to add()
print "sum=%d\n" % (c)
sum=7

EXAMPLE ON PAGE:175-176

In [3]:
def factorial(num):
    f=1
    for i in range(1,num+1,1):
        f=f*i;
    return f
f=factorial(5)
print "%d\n" % f
120