# Using a function as an abbreviation
# In python, the user defined functions have to defined before making a call to them
def chorus() :
print "%60s"%"With a knick-knack-paddy-whack,"
print "%60s"%"Give the dog a bone,"
print "%60s\n"%"This old man went rolling home."
print "This old man, he played two,"
print "He played knick-knack on my thumb,"
chorus()
print "This old man, he played one,"
print "He played knick-knack on my shoe,"
chorus()
print "This old man, he played three,"
print "He played knick-knack on my knee,"
chorus()
# Demonstration of one use of a function
def get_an_int() :
"""RETURNS AN INTEGER""" # This statement is just a description of the function (specific feature of python)
return 5
print "Enter the length of a rectangle:",
length = get_an_int()
print length
print "Enter the width:",
width = get_an_int()
print width
print "\nThe area is %d"%(length*width)
# An attempt at communication between functions
def modify_i() :
i = 3
i = 1
print "i is now equal to %d"%i
modify_i()
print "i is now equal to %d"%i
# Demonstration of the use of a global variable
# Making variable global
def get_an_int() :
return 5
def calc_area() :
area = length*width
globals() # The command simply makes all the variables declared so far as global variables
print "Enter the length of a rectangle:",
length = get_an_int()
print length
print "Enter the width:",
width = get_an_int()
print width
calc_area()
print "\nThe area is %d"%(length*width)
# Introduction to parameters
def print_an_int(i) :
"""The function prints an integer passed as a parameter"""
print i
n = 3
print "Passing a constant:",
print_an_int(5)
print "\n\nPassing a variable's value:",
print_an_int(n)
print "\n\nPassing the value of an expression:",
print_an_int(n * 4 + 1)
# Demonstration of how a procedure cannot modify the contents of a variable passed as a parameter
def modify(i) :
i = 3
i = 1
print "i is now equal to %d"%i
modify(i)
print "i is now equal to %d"%i
# Hailstones, demonstrating the use of a parameter
def step(i) :
"""This function returns its parameter i divided by 2 if i is even else it returns i multiplied by 3 and incremented by 1"""
if (i % 2) :
return (i * 3 + 1)
else :
return (i / 2)
counter = 0
print "Enter a positive integer",
n = 25
print n
nsave = n
while n > 1 :
n = step(n)
counter += 1
print "Step %4d: n = %4d"%(counter,n)
print "\n\n%d went to 1 in %d steps."%(nsave,counter)
# Square root program demonstrating a non-integer function and parameter
def EPSILON() :
"""replacement of #define constant"""
return 0.0001
def sqrt(n) :
"""returns the square root of the number supplied as parameter using Newton-Raphson method"""
if n > 0.0 :
# The for loop statement given in the example(from the book) is not possible to code in python
# So, breaking it down to while loop
guess = n / 2.0
while (guess * guess - n > EPSILON() or guess * guess - n < -EPSILON()) :
guess = (guess + n/guess) / 2.0
return guess
else :
return (-1.0)
print "Enter the number whose square root you want:",
x = 17.1
print x
print "The square root of %f is %f"%(x,sqrt(x))
# Testing for leap years using a boolean function
def is_leap(year) :
"""A function that returns true if the given year is a leap year else returns false"""
return (year % 4 == 0 and year % 100 != 0 or year % 400 == 0)
print "Please enter a year:",
year = 2000
print year
if (is_leap(year)) :
print "%d is a leap year."%year
else :
print "%d is not a leap year."%year
# Demonstration of the use of multiple parameters
def get_an_int() :
"""returns a random integer"""
return 7
def area(length,width) :
"""returns the area of the rectangle using its parameters length and width"""
return length * width
print "Enter the length of a rectangle:",
length = get_an_int()
print length
print "Enter the width:",
width = get_an_int()
print width
print "\nThe area is %d."%area(length,width)
# Illustration of a 2-parameter function: raising a number to a power
def power(i,x) :
"""returns the values of i to the power of x"""
accum = 1
if x < 0 :
return 0
for index in range (0,x,1) :
accum *= i
return accum
answer = "y"
while answer == "y" or answer == "Y" :
print "\nPlease enter an integer:",
number = 4
print number
print "Please enter a non-negative integral power"
print " to which the first number is to be raised",
exponent = 2
print exponent
while exponent < 0 :
print "***\7Please make that a non-negative integer"
exponent = 2 # randomly making the exponent non-negative
print "%d raised to the power %d is %ld."%(number,exponent,power(number,exponent))
# see if the user wants to keep going
print "\nAnother calculation? (y/n)",
answer = "n"
print answer