Chapter 6 - Functions
Chapter 6 - Functions
sum = 0
for i in range(20, 37):
sum += i
print("Sum from 20 to
37 is", sum)
sum = 0
for i in range(35, 49):
sum += i
© Copyright 2012 by Pearson Education, Inc. All Rights Reserved.
3
print("Sum from 35 to
Problem
sum = 0
for i in range(1, 10):
sum += i
print("Sum from 1 to
10 is", sum)
sum = 0
for i in range(20, 37):
sum += i
print("Sum from 20 to
sum = 0 sum)
37 is",
for i in range(35, 49):
sum += i
print("Sum from 35 to 49 is", sum)
© Copyright 2012 by Pearson Education, Inc. All Rights Reserved.
4
Solution
def sum(i1, i2):
result = 0
for i in range(i1, i2):
result += i
return result
def main():
print("Sum from 1 to 10 is", sum(1, 10))
print("Sum from 20 to 37 is", sum(20, 37))
print("Sum from 35 to 49 is", sum(35, 49))
function z = max(x, y)
def max(num1, num2):
header
return result
return value
function z = max(x, y)
def max(num1, num2):
header
return result
return value
function z = max(x, y)
def max(num1, num2):
header
return result
return value
function z = max(x, y)
def max(num1, num2):
header
return result
return value
function z = max(x, y)
def max(num1, num2):
header
return result
return value
TestMax Run
pass int 5
pass int 2
def main():
i = 5
main() j = 2
k = max(i, j)
prin
pass int 5
pass int 2
def main():
i = 5
main() j = 2
k = max(i, j)
prin
pass int 5
pass int 2
def main():
i = 5
main() j = 2
k = max(i, j)
prin
def main():
i = 5
main() j = 2
k = max(i, j)
prin
def main():
i = 5
main() j = 2
k = max(i, j)
prin
def main():
i = 5
main() j = 2
k = max(i, j)
prin
pass int 5
pass int 2
def main():
i = 5
main() j = 2
k = max(i, j)
prin
pass int 5
pass int 2
def main():
i = 5
main() j = 2
k = max(i, j)
prin
def main():
i = 5
main() j = 2
k = max(i, j)
prin
def main():
i = 5
main() j = 2
k = max(i, j)
prin
pass int 5
pass int 2
def main():
i = 5
main() j = 2
k = max(i, j)
prin
(a) The main function This is a heap for (b) The max This is a heap for (c) The max function
is invoked. storing objects function is invoked. storing objects is being executed.
stack stack
int object
Space required for
2
the main function Stack is
k: now empty
j: 2 int object
i: 5 5
(d) The max function is This is a heap for (e) The main
finished and the return storing objects function is finished.
value is sent to k.
PrintGradeFunction Run
ReturnGradeFunctio Run
n
What is wrong
nPrintln(4, “Computer Science”)
© Copyright 2012 by Pearson Education, Inc. All Rights Reserved.
29
Keyword
def Arguments
nPrintln(message,
for i in range(0,
n):
n):
print(message)
What is wrong
nPrintln(4, “Computer Science”)
Is this OK?
nPrintln(n = 4, message = “Computer Science”)
Increment Run
© Copyright 2012 by Pearson Education, Inc. All Rights Reserved.
31
Modularizing Code
Functions can be used to reduce redundant
coding and enable code reuse. Functions can also
be used to modularize code and improve the
quality of the program.
GCDFunction TestGCDFunction
Run
PrimeNumberFunction
Run
Decimal2HexConversi Run
Ru
on n
DefaultArgumentDem Run
o
MultipleReturnValueDem
o
Run
RandomCharacter
TestRandomCharact
er
Run
Function Header
Black Box
Function Body
PrintCalendar Run
readInput printMonth
printMonthTitle printMonthBody
getMonthName getStartDay
getTotalNumOfDays
getNumOfDaysInMonth
isLeapYear
© Copyright 2012 by Pearson Education, Inc. All Rights Reserved.
47
Design Diagram
printCalendar
(main)
readInput printMonth
printMonthTitle printMonthBody
getMonthName getStartDay
getTotalNumOfDays
getNumOfDaysInMonth
isLeapYear
readInput printMonth
printMonthTitle printMonthBody
getMonthName getStartDay
getTotalNumOfDays
getNumOfDaysInMonth
isLeapYear
readInput printMonth
printMonthTitle printMonthBody
getMonthName getStartDay
getTotalNumOfDays
getNumOfDaysInMonth
isLeapYear
readInput printMonth
printMonthTitle printMonthBody
getMonthName getStartDay
getTotalNumOfDays
getNumOfDaysInMonth
isLeapYear
readInput printMonth
printMonthTitle printMonthBody
getMonthName getStartDay
getTotalNumOfDays
getNumOfDaysInMonth
isLeapYear
A Skeleton for
printCalendar
© Copyright 2012 by Pearson Education, Inc. All Rights Reserved.
53
Implementation: Bottom-Up
Bottom-up approach is to implement one function in the
structure chart at a time from the bottom to the top. For
each function implemented, write a test program to test it.
Both top-down and bottom-up functions are fine. Both
approaches implement the functions incrementally and
help to isolate programming errors and makes debugging
easy. Sometimes, they can be used together.
UseCustomTurtleFunctio Run
ns © Copyright 2012 by Pearson Education, Inc. All Rights Reserved.
55