Chapter 9 - Functions

example 9.1, page no. 179

In [2]:
def main(): #defining a function 
    print "Hello World!"

main()
Hello World!

example 9.2, page no. 180

In [4]:
def printMessage(): # defining function
    print "Hello world!"

printMessage() # calling the function
Hello world!

example 9.3, page no. 181

In [5]:
printMessage();

def printMessage():
    print "Hello world!"
Hello world!

example 9.4, page no. 182

In [7]:
def printMessage():
    print "Hello world!"

printMessage()
Hello world!

example 9.5, page no. 184

In [4]:
def printMessage():
    times = 0
    times += 1
    print "This function called ", times, " times"



choice = '1'
while(choice != 'Q'):
    print "Enter Q to quit, any other character to continue: ",
    choice = raw_input()
    if (choice == 'Q'):
        print "Input stopped"
    else:
        printMessage()
 Enter Q to quit, any other character to continue: e
 This function called  1  times
Enter Q to quit, any other character to continue: e
 This function called  1  times
Enter Q to quit, any other character to continue: q
 This function called  1  times
Enter Q to quit, any other character to continue: Q
 Input stopped

example 9.6, page no. 185

In [9]:
def printMessage():
    times = 0
    times += 1
    print "This function called ", times, " times"


choice = '1'
while(choice != 'Q'):
    print "Enter Q to quit, any other character to continue: ",
    choice = raw_input()
    if (choice == 'Q'):
        print "Input stopped"
    else:
        printMessage()
 Enter Q to quit, any other character to continue: w
 This function called  1  times
Enter Q to quit, any other character to continue: q
 This function called  1  times
Enter Q to quit, any other character to continue: Q
 Input stopped

example 9.7, page no. 186

In [11]:
times = 0
def printMessage():
    global times
    times += 1
    print "This function called ", times, " times"


choice = '1'
while(choice != 'Q'):
    print "Enter Q to quit, any other character to continue: ",
    choice = raw_input()
    if (choice == 'Q'):
        print "Input stopped"
    else:
        printMessage()
Enter Q to quit, any other character to continue: w
 This function called  1  times
Enter Q to quit, any other character to continue: t
 This function called  2  times
Enter Q to quit, any other character to continue: u
 This function called  3  times
Enter Q to quit, any other character to continue: Q
 Input stopped

example 9.8, page no. 189

In [12]:
times = 0
def printMessage():
    global times
    times += 1
    print "This function called ", times, " times"


choice = '1'
while(choice != 'Q'):
    print "Enter Q to quit, any other character to continue: ",
    choice = raw_input()
    if (choice == 'Q'):
        print "Input stopped"
    else:
        printMessage()
Enter Q to quit, any other character to continue: w
 This function called  1  times
Enter Q to quit, any other character to continue: r
 This function called  2  times
Enter Q to quit, any other character to continue: t
 This function called  3  times
Enter Q to quit, any other character to continue: Q
 Input stopped

example 9.9, page no. 190

In [22]:
def printMessage():
    print "You inputted ", str

print "Enter a string: ",
str = raw_input()
printMessage()
Enter a string: Jeff
 You inputted  Jeff

example 9.10, page no. 191

In [21]:
def printMessage(s):
    print "You inputted ", s

print "Enter a string: ",
str = raw_input()
printMessage(str)
Enter a string: Jeff
 You inputted  Jeff

example 9.11, page no. 193

In [20]:
def printMessage(firstName, lastName):
    print "Your name is ",  firstName, " ", lastName

print "Enter first name:",
name1 = raw_input()
print "Enter last name:",
name2 = raw_input()
printMessage(name1, name2)
Enter first name:Jeff
 Enter last name:Kent
 Your name is  Jeff   Kent

example 9.12, page no. 194

In [17]:
def printMessage(thename, theage):
    print "Your name is ",  thename, " and your age is", theage

print "Enter name:",
name = raw_input()
print "Enter age:",
age = int(raw_input())
printMessage(name, age)
printMessage(age, name)
Enter name:Jeff
 Enter age:34
 Your name is  Jeff  and your age is 34
Your name is  34  and your age is Jeff

example 9.13, page no. 195

In [18]:
def doubleIt(x):
    print "The number to be doubled is ", x
    x *= 2
    print "The number doubled in doubleIt is ", x

print "Enter number: ",
num = int(raw_input())
doubleIt(num)
print "The number doubled outside the function is ", num,
Enter number: 34
 The number to be doubled is  34
The number doubled in doubleIt is  68
The number doubled outside the function is  34

example 9.14, page no. 196

In [19]:
li = [0]
def doubleIt():
    print "The number to be doubled is ", li[0]
    li[0] *= 2
    print "The number doubled in doubleIt is ", li[0]
    
print "Enter number: ",
num = int(raw_input())
li[0] = num
doubleIt()
print "The number doubled outside the function is ", li[0]
Enter number: 5
 The number to be doubled is  5
The number doubled in doubleIt is  10
The number doubled outside the function is  10

example 9.15, page no. 197

In [23]:
z = []
def addNumbers (a, b):
    z.append(a + b)


print "Enter first number: ",
firstNum = int(raw_input())
print "Enter second number: ",
secondNum = int(raw_input())
addNumbers(firstNum, secondNum)
print firstNum, " + ", secondNum, " = ", z[0]
Enter first number: 3
 Enter second number: 4
 3  +  4  =  7

example 9.16, page no. 199

In [24]:
def addNumbers(x, y):
    return x + y

print "Enter first number: ",
firstNum = int(raw_input())
print "Enter second number: ",
secondNum = int(raw_input())
sum = addNumbers(firstNum, secondNum)
print firstNum, " + ", secondNum, " = ", sum
Enter first number: 4
 Enter second number: 5
 4  +  5  =  9