Chapter 5: Functions

Program Source Code 5.1, page no: 134

In [1]:
def product(x,y):    #function product
    return x*y

a=input('Please input a number: ')           #user input
b=input('Please input another number: ')
c=product(a,b)
print 'the product of ',a,'and',b,'is',c
Please input a number: 4
Please input another number: 5
the product of  4 and 5 is 20

Program Source Code 5.2, page no: 135

In [1]:
def product(x,y):         #function product
    return x*y

a=input('Please input a floating point number: ')           #user input
b=input('Please input another floating point number: ')
c=product(a,b)          #function call
print 'the product of ',a,'and',b,'is',c
Please input a floating point number: 1.2
Please input another floating point number: 1.3
the product of  1.2 and 1.3 is 1.56

Program Source Code 5.3, page no: 136

In [5]:
def printProduct(x,y):  #function product
    c=0
    c=a*b
    print 'The product of ',a,'and',b,'is',c
    return

a=input('Please input an intger: ')     #user input
b=input('Please input another integer: ')
c=printProduct(a,b)
Please input an intger: 3
Please input another integer: 4
The product of  3 and 4 is 12

Program Source Code 5.4, page no: 138

In [2]:
def product(x,y): #function product
    return x*y

a=input('Please input a number: ')           #user input
b=input('Please input another number: ')
c=product(a,b)
print 'the product of ',a,'and',b,'is',c
Please input a number: 4
Please input another number: 5
the product of  4 and 5 is 20

Program Source Code 5.5, page no: 140

In [1]:
from ctypes import c_int,pointer


i =c_int(4) 
j=i
pi=pointer(i)
value=pi[0]
pj=pi[0]
print 'Before incrementing *pi'
print 'i= ',i.value,'j= ',j.value,'*pi= ',pi[0],'*pj= ',pj
pi[0]+=1         #no prefix and postfix in python
pj=pi[0]
print 'After incrementing *pi'
print 'i= ',i.value,'j= ',j.value,'*pi= ',pi[0],'*pj= ',pj
Before incrementing *pi
i=  4 j=  4 *pi=  4 *pj=  4
After incrementing *pi
i=  5 j=  5 *pi=  5 *pj=  5

Program Source Code 5.6, page no: 142

In [4]:
def Product(x,y):         #function product
    return x*y

print 'Please input two integer: ' #user input
a=input()
b=input()
c=Product(a,b)  #call function product
print 'The product of ',a,'and',b,'is',c

print 'Please input two floating point numbers: '
f=input()
g=input()
h=Product(f,g)         #call function product
print 'The product of ',f,'and',g,'is',h
Please input two integer: 
4
5
The product of  4 and 5 is 20
Please input two floating point numbers: 
1.2
2.3
The product of  1.2 and 2.3 is 2.76

Program Source Code 5.7, page no: 143

In [2]:
def fracval(numerator =0, denominator =1):       #function declaration with default parameters
    val=(numerator/denominator)
    return val

print 'please input numerator and denominator of a fraction: '
a=input()
b=input()
f=fracval(a,b)        #function call

print 'The fraction value ',a,'/',b,'=',f

print 'please input the numerator (denominator assumed to be 1: )'
a=input()
f=fracval(a)
print 'The fraction value ',a,'/1','=',f

print 'And the defaukt fraction value is (num=0, denom=1): '
f=fracval()
print 'the fraction value 0/1=',f
please input numerator and denominator of a fraction: 
3
4
The fraction value  3 / 4 = 0
please input the numerator (denominator assumed to be 1: )
3
The fraction value  3 /1 = 3
And the defaukt fraction value is (num=0, denom=1): 
the fraction value 0/1= 0

Program Source Code 5.8, page no: 145

In [3]:
def swap(a,b):
    temp=a
    a=b
    b=temp

    
#variable declaration
x=3
y=4
print 'before swap: x= ',x,'y= ',y
swap(x,y)
print 'After swap: x= ',x,'y= ',y
before swap: x=  3 y=  4
After swap: x=  3 y=  4

Program Source Code 5.9, page no: 146

In [3]:
def swap(a,b):        #function swap
        temp = a
        a = b
        b = temp
        return a,b
    
#variable declaration
x=3
y=4
print 'before swap: x= ',x,'y= ',y
x,y=swap(x,y)
print 'After swap: x= ',x,'y= ',y       
before swap: x=  3 y=  4
After swap: x=  4 y=  3

Program Source Code 5.10, page no: 147

In [4]:
def swap(a,b):  #function swap
        temp = a
        a = b
        b = temp
        return a,b
    
#variable declaration
x=3
y=4
print 'before swap: x= ',x,'y= ',y
x,y=swap(x,y)
print 'After swap: x= ',x,'y= ',y       
before swap: x=  3 y=  4
After swap: x=  4 y=  3

Program Source Code 5.11, page no: 147

In [10]:
def PrintCharManyTimes(ch,n):                    #function declarator
    for j in range(n):                #function body
        print ch,
    print
    
PrintCharManyTimes('-',40)                       #call to function
print 'Data type   Range'
PrintCharManyTimes('=',18)                       #call to function
print 'Char        -128 to 127'
print 'int         -32,768 to 32,767'
print 'double      -2,147,483,648 to 2,147,483,647'
PrintCharManyTimes('-',40)                       #call to function
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Data type   Range
= = = = = = = = = = = = = = = = = =
Char        -128 to 127
int         -32,768 to 32,767
double      -2,147,483,648 to 2,147,483,647
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

Program Source Code 5.12, page no: 149

In [4]:
def factfunc(a):               #calls itself to calculate factorials
    if a>1:
        return a * factfunc(a-1)
    else:
        return 1
    
n = input("Please input a number: ")             #get number from user
fact = factfunc(n)                          #function call for factorial
print 'Factorial of',n,'! is',fact,'\n'       #dsplay factorial
Please input a number: 4
Factorial of 4 ! is 24 

Program Source Code 5.13, page no: 152

In [5]:
gi=1       #global variable declared
si = 1         #static variable declared

def func():
    global si
    i=1
    i+=1
    si+=1
    global gi
    gi+=1
    
    print 'i= ',i,'si= ',si,'gi= ',gi
    
for i in range(10):
    print '(',i,')Call to func(): ',
    gi+=1
    func()
( 0 )Call to func():  i=  2 si=  2 gi=  3
( 1 )Call to func():  i=  2 si=  3 gi=  5
( 2 )Call to func():  i=  2 si=  4 gi=  7
( 3 )Call to func():  i=  2 si=  5 gi=  9
( 4 )Call to func():  i=  2 si=  6 gi=  11
( 5 )Call to func():  i=  2 si=  7 gi=  13
( 6 )Call to func():  i=  2 si=  8 gi=  15
( 7 )Call to func():  i=  2 si=  9 gi=  17
( 8 )Call to func():  i=  2 si=  10 gi=  19
( 9 )Call to func():  i=  2 si=  11 gi=  21

Program Source Code 5.14, page no: 154

In [24]:
def max(x,y): #function defined
    if x>y:
        return x
    else:
        return y    

a=5
b=6
c=max(a,b)

if c==a:               # no return by refernce function so declared a simple case
    c=8
    a=c
elif c==b:
    c=8
    b=c
    

print 'a=',a,'b=',b,'c=',c
a= 5 b= 8 c= 8

Program Source Code 5.15, page no: 157

In [9]:
def op1(v):    #for displaying numbers
    for j in range(len(v)):
        print v[j],
        
def remove_if(v,x): #for removing elements
    for a in v:
        if not a%x==0:
            v.remove(a)
    return v
nos=[10,12,15,20,25,27]
print '(before): ',;op1(nos)
x=5
print ''
nos=remove_if(nos,5)
print '(after): ',;op1(nos)
(before):  10 12 15 20 25 27 
(after):  10 15 20 25

Program Source Code 5.16, page no: 158

In [8]:
def sort(number):      #for sorting
    size=len(number)
    for i in range(size-1 ):
        if int(number[i])>int(number[i+1]):
            temp=number[i]
            number[i]=number[i+1]
            number[i+1]=temp
            return number
        
def op1(v):      #for displaying numbers
    for j in range(len(v)):
        print v[j],
        
number=["1","12","23","2"]

print '(original): ',;op1(number)

print ''
number=sort(number)                        # 1st sort
print '(after 1st sort): ',;op1(number)
print ''

number=sort(number)
print '(after 2nd sort): ',;op1(number)       # 2nd sort
print ''
(original):  1 12 23 2 
(after 1st sort):  1 12 2 23 
(after 2nd sort):  1 2 12 23 
In [ ]: