Loops and Functions
Additional Topics
CAPT Jeff Hyink
OA 2801
1
A Short Discussion of a Few Other
Constructs with Functions and Loops
OA 2801
2
Nested Functions
Functions as Arguments
• We’ve seen some
“nested” functions
already
int(raw_input(“gimme a number”))
Nested
deg_to_radians(dms_to_deg(d,m,s))
• In general, a function
can be one of the
arguments passed to
another function…
OA 2801
Nested
4
Function Reference
• You can pass a “generic reference” to another
function as an argument and use it in another
function
def apply_function(f, an_argument):
temp = f(an_argument)
print temp
A Script
>>>apply_function(int, 55.456)
55
“int” is a function in the
namespace and it is a reference
to a function
OA 2801
5
Function Reference
• Similarly, you can reference your “own functions”
A Script
def cube(x):
return x**3
def apply_function(f, an_argument):
temp = f(an_argument)
This line is then
print temp
cube(an_agrument) or
cube(3)
>>> apply_function(cube,3)
27
In the namespace, cube is a reference to a
function…. just like “x” might be a
reference to a value. So, a function as a
parameter of another function allows you
to pass in an arbitrary function
OA 2801
6
Function Reference
• Of course, you need to match parameter types…
A Script
def cube(x):
return x**3
def apply_function(f, an_argument):
temp = f(an_argument)
print temp
If it’s expecting a function
(“callable” item) and you give it
something else…
>>> apply_function(15,3)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "nested_functions.py", line 5, in
apply_function
print f(an_argument)
TypeError: 'int' object is not callable
>>>
OA 2801
7
Loops of Loops
aka Nested Loops
Nested Loops
• Occasionally, we need to perform a “loop within
another loop”
• This is known as a “nested loop”
• When are the likely occurrences?
• 2-dimensional problems….think:
– Matrix… every item in a column in every row
– “x and y” functions
– “for every item in every group…”
3 4
0
7
OA 2801
1
0
0
4
1
0
2
0
9
2-Dimensional Problems
• We haven’t discussed ways to store these types of
data yet, but…
• 2-dimensional problems are all around:
– Fuel consumption for a ship as a fn of weight and speed
– Average elevation in an area where height = f(x, y)
• In general,
anything of the form f(x,y)
OA 2801
10
2D General Format
i = 0
while (i < row_lim):
j = 0
while (j < col_lim):
# do something
j = j + 1
i = i + 1
set up the inner loop
update the inner counter
update the outer counter
Think of it like an odometer…
The small wheel turns a bunch
of times, then the bigger wheel
turns over
OA 2801
11
Iteration Through a 1-D Problem
• We’ve done this…
A Script
sum = 0
students = 5
count = 0
while (count < students):
new_score = int(raw_input("enter new score: "))
sum = sum + new_score
count += 1
print float(sum)/students
enter new score: 4
enter new score: 5
enter new score: 12
enter new score: 3
enter new score: 11
7.0
OA 2801
12
2-D Example
• Every Student has 2 tests…
for student 0
enter new score: 15
enter new score: 18
for student 1
enter new score: 12
enter new score: 11
for student 2
enter new score: 19
enter new score: 20
15.8333333333
sum = 0
students = 3
tests = 2
student_num = 0
while (student_num < students):
test_num = 0
print "for student %d" % student_num
while (test_num < tests):
new_score = int(raw_input("enter new score: "))
sum = sum + new_score
test_num += 1
student_num += 1
The inner loop is looping over
print float(sum)/students
tests while the outer is looping
over the students…
OA 2801
13
Recap
• Functions can be parameters of other functions
• This increases the utility and re-usability of functions
• Loops can contain other loops
• Standard for 2-d problems…
OA 2801
14
Board Example
• Let’s evaluate the function
2
f (x, y) = x + 2y
• Over the grid of points:
x, y 2 {1, 2, 3}
OA 2801
15