Chapter 4: Functions

Example 4.1, page no. 103

In [1]:
def aax1():
# code for function body 
    return (1.0)

r = aax1();
print r
1.0

Example 4.2 & 4.3, page no. 106-108

In [1]:
'''
This won't generate any output
Examples 4.2 and 4.3 are same for python language.
'''


def pmax(a1,a2):
    biggest = 0
    if(a1 > a2):
        biggest = a1
    else:
        biggest = a2

#print "larger of %d and %d is %d\n" %(a1, a2, biggest);



for i in range(11):
    for j in range(-10,11):
       pmax(i,j)
        
# It shows that all variables scope is locally only. They can not be accessed from outside its scope.        

Example 4.4, page no. 109

In [3]:
def sq_root(x):
    DELTA = 0.0001
    curr_appx = 0
    last_appx = 0
    diff = 0
    last_appx = x
    diff = DELTA+1
    while(diff > DELTA):
        curr_appx = 0.5*(last_appx + x/last_appx)
        diff = curr_appx - last_appx
        if(diff < 0):
            diff = -diff
            last_appx = curr_appx
    return(curr_appx)
 


for i in range(1,101):
    print "root of %d is %f\n" %( i, sq_root(i))
root of 1 is 1.000000

root of 2 is 1.414214

root of 3 is 1.732051

root of 4 is 2.000000

root of 5 is 2.236068

root of 6 is 2.449490

root of 7 is 2.645751

root of 8 is 2.828427

root of 9 is 3.000000

root of 10 is 3.162278

root of 11 is 3.316625

root of 12 is 3.464102

root of 13 is 3.605551

root of 14 is 3.741657

root of 15 is 3.872983

root of 16 is 4.000000

root of 17 is 4.123106

root of 18 is 4.242641

root of 19 is 4.358899

root of 20 is 4.472136

root of 21 is 4.582576

root of 22 is 4.690416

root of 23 is 4.795832

root of 24 is 4.898979

root of 25 is 5.000000

root of 26 is 5.099020

root of 27 is 5.196152

root of 28 is 5.291503

root of 29 is 5.385165

root of 30 is 5.477226

root of 31 is 5.567764

root of 32 is 5.656854

root of 33 is 5.744563

root of 34 is 5.830952

root of 35 is 5.916080

root of 36 is 6.000000

root of 37 is 6.082763

root of 38 is 6.164414

root of 39 is 6.244998

root of 40 is 6.324555

root of 41 is 6.403124

root of 42 is 6.480741

root of 43 is 6.557439

root of 44 is 6.633250

root of 45 is 6.708204

root of 46 is 6.782330

root of 47 is 6.855655

root of 48 is 6.928203

root of 49 is 7.000000

root of 50 is 7.071068

root of 51 is 7.141428

root of 52 is 7.211103

root of 53 is 7.280110

root of 54 is 7.348469

root of 55 is 7.416198

root of 56 is 7.483315

root of 57 is 7.549834

root of 58 is 7.615773

root of 59 is 7.681146

root of 60 is 7.745967

root of 61 is 7.810250

root of 62 is 7.874008

root of 63 is 7.937254

root of 64 is 8.000000

root of 65 is 8.062258

root of 66 is 8.124038

root of 67 is 8.185353

root of 68 is 8.246211

root of 69 is 8.306624

root of 70 is 8.366600

root of 71 is 8.426150

root of 72 is 8.485281

root of 73 is 8.544004

root of 74 is 8.602325

root of 75 is 8.660254

root of 76 is 8.717798

root of 77 is 8.774964

root of 78 is 8.831761

root of 79 is 8.888194

root of 80 is 8.944272

root of 81 is 9.000000

root of 82 is 9.055385

root of 83 is 9.110434

root of 84 is 9.165151

root of 85 is 9.219544

root of 86 is 9.273618

root of 87 is 9.327379

root of 88 is 9.380832

root of 89 is 9.433981

root of 90 is 9.486833

root of 91 is 9.539392

root of 92 is 9.591663

root of 93 is 9.643651

root of 94 is 9.695360

root of 95 is 9.746794

root of 96 is 9.797959

root of 97 is 9.848858

root of 98 is 9.899495

root of 99 is 9.949874

root of 100 is 10.000000

Example 4.6, page no. 115

In [4]:
def called_func(a,b):
    temp = a * b
    print temp

called_func(1,2*3.5)
7.0

Example 4.7, page no. 116

In [5]:
def changer(x):
    while(x!=0):
        print "changer: x=%d\n" %(x)
        x-=1

i = 5
print "before i=%d\n" %( i);
changer(i);
print "after i=%d\n" %( i);
before i=5

changer: x=5

changer: x=4

changer: x=3

changer: x=2

changer: x=1

after i=5

Example 4.8, page no. 120

In [ ]:
'''
Do not try to run....
It's infinite looping program..

This program shows how a function calls itself.
'''

def expr():
    val = 0
    ch_in = 0
    val = mul_exp()
    while True:
        ch_in = raw_input("Enter + or - ")
        if ch_in =='+':
            val = val + mul_exp();
        else:
            if ch_in == '-':
                val = val - mul_exp()
            else:
                return val

    return val

def mul_exp():
    val = 0
    ch_in = 0
    val = unary_exp()
    while True:
        ch_in = raw_input("Enter * or / or % ")
        if ch_in =='*':
            val = val * unary_exp();
        else:
            if ch_in == '/':
                val = val - mul_exp()
            else:
                if ch_in =='%':
                    val = val % unary_exp()
                else:
                    return val
    return val

def unary_exp():
    val=0
    ch_in = raw_input("Enter + or - ")
    if ch_in =='+':
        val = unary_exp()
    else:
        if ch_in == '-':
            val = -unary_exp()
        else:
            val = primary()
            return val

    return val

def primary():
    val = 0
    ch_in = raw_input('Enter value')
    if (ch_in >= '0' and ch_in <= '9'):
        val = ch_in - '0'
        return val
    if ch_in == '(':
        val = expr()
    print "error: primary read %d\n" %ch_in
    return val


val = 0
while True:
    print "expression: "
    val = expr();
    if(raw_input()!= '\n'):
        print "error\n"
        while(raw_input() != '\n'):
            pass
        
    else:
        print "result is %d\n" %val

Example 4.9, page no. 123

In [ ]:
#No output will be produced

from second import * #importing all content of second.py file


i=0
print f_in_other_place(i) # declaration 

Example 4.10, page no. 124

In [ ]:
'''
Example 4.10
/* example library module */
/* only 'callable' is visible outside */
'''


__buf=[]
__length = 0

def __fillup__():
    while (length <100):
        buf[length] = 0
                length += 1

def callable ():
    if (length ==0):
        fillup ()
                a = buf[length]
                length-=1
        return (a)

Example 4.11, page no. 126

In [ ]:
'''
python doesn't have static variable 
we can use global variable for counting 
this demonstrates how it works...
'''

count = 0

def small_val ():
    count+=1
    return (count % 16)

Example 4.12, page no. 126

In [ ]:
'''
Example 4.12
python doesn't have static variables.
'''


depth=0
def  r_func():
    depth+=1
    if (depth > 200):
        print ("excessive recursion\n")
        sys.exit(0)	
    else:
        '''/* do usual thing,
        * not shown here.
        * This last action
        * occasionally results in another
        * call on r_func()
        */
        '''
        x_func()

    depth-=1