# Chapter 9: Functions¶

## Example average.c, Page 185¶

In [3]:
def average(a,b): #function to calculate average
return (a+b)/2
nums = raw_input("Enter three numbers: ") #input numbers from user
list1 = map(float, nums.split())
x=list1[0]
y=list1[1]
z=list1[2]
print "Average of %.1f and %.1f: %.2f" % (x,y,average(x,y)) #print average using function
print "Average of %.1f and %.1f: %.2f" % (y,z,average(y,z))
print "Average of %.1f and %.1f: %.2f" % (x,z,average(x,z))

Enter three numbers: 3.5 9.6 10.2
Average of 3.5 and 9.6: 6.55
Average of 9.6 and 10.2: 9.90
Average of 3.5 and 10.2: 6.85


## Example countdown.c, Page 186¶

In [5]:
def print_count(n): #function definition
print "T minus %d and counting" % n
for i in range (10,0,-1):
print_count(i) #print using function


T minus 10 and counting
T minus 9 and counting
T minus 8 and counting
T minus 7 and counting
T minus 6 and counting
T minus 5 and counting
T minus 4 and counting
T minus 3 and counting
T minus 2 and counting
T minus 1 and counting


## Example pun2.c, Page 187¶

In [6]:
def print_pun(): #function definition
print "To C, or not to C: that is the question."
print_pun() #function call

To C, or not to C: that is the question.


## Example prime.c, Page 190¶

In [7]:
def is_prime(n): #function to check for prime number
if (n<=1):
return False
divisor=2
while(divisor*divisor<=n):
if(n%divisor==0):
return False
return True
n=int(raw_input("Enter a number: ")) #input number
if(is_prime(n)): #check if prime using function
print "Prime" #print result of check
else:
print "Not prime"

Enter a number: 34
Not prime


## Example on Page 192¶

In [2]:
def main():
nums = raw_input("Enter three numbers: ") #input numbers from user
list1 = map(float, nums.split())
x=list1[0]
y=list1[1]
z=list1[2]

#function usage before definition

print "Average of %.1f and %.1f: %.2f" % (x,y,average(x,y)) #print average using function
print "Average of %.1f and %.1f: %.2f" % (y,z,average(y,z))
print "Average of %.1f and %.1f: %.2f" % (x,z,average(x,z))

if __name__=="__main__":
main()

def average(a,b): #function to calculate average
return (a+b)/2

Enter three numbers: 3.5 9.6 10.2
Average of 3.5 and 9.6: 6.55
Average of 9.6 and 10.2: 9.90
Average of 3.5 and 10.2: 6.85


## Example on Page 195¶

In [3]:
x=3.0
print "Square: %d" % square(x) #gives error since function prototype doesnt exist before
def square(n):
return n*n

"""
def main():
x=3.0
print "Square: %d" % square(x)

#Now wouldn't give an error since writing the line below loads up all functions before starting main()

if __name__=="__main__":
main()

def square(n):
return n*n
"""

---------------------------------------------------------------------------
NameError                                 Traceback (most recent call last)
<ipython-input-3-2c761040ea35> in <module>()
1 x=3.0
----> 2 print "Square: %d" % square(x) #gives error since function prototype doesnt exist before
3 def square(n):
4     return n*n

NameError: name 'square' is not defined

## Example on Page 197¶

In [7]:
def store_zeroes(a,n):
for i in range(0,n):
a[i]=0
b=[None]*200
store_zeroes(b,100) #First 100 elements of b are now 100.
print b

[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None]


## Example qsort.c, Page 207¶

In [1]:
N=10 #number of elements to sort
def split(a,low,high): #function to split list while sorting
part_element=a[low]
while(1):
while(low<high and part_element<=a[high]):
high=high-1
if(low>=high):
break
a[low+1]=a[high]

while(low<high and a[low]<=part_element):
low=low+1
if(low>=high):
break
a[high-1]=a[low]

a[high]=part_element
return high

def quicksort(a,low,high): #recursive function for quicksort
if(low>=high):
return
middle=split(a,low,high)
quicksort(a,low,(middle-1))
quicksort(a,(middle+1),high)

nums = raw_input("Enter %d numbers to be sorted: "%N) #input numbers to sort
a = map(int, nums.split())
a.sort()
quicksort(a,0,(N-1))       #call quicksort function
print "In sorted order: ", #print sorted result
for i in a:
print i,

Enter 10 numbers to be sorted: 9 16 47 82 4 66 12 3 25 51
In sorted order:  3 4 9 12 16 25 47 51 66 82

In [ ]: