Consider the following code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
"""
This program will ask the user to enter the side lengths
of a rectangle.
Two functions will be used to compute the area and perimeter
of the rectangle.
The area and perimeter will be printed back to the user.
"""
def perimeter_rectangle(length, width):
"""
Compute the perimeter of a rectangle
Args:
length (int): the length of the rectangle
width (int): the width of the rectangle
Returns:
int: the perimeter of the rectangle
"""
computed_perimieter = (length + width) * 2
return computed_perimieter
def area_rectangle(length, width):
"""
Compute the area of a rectangle
Args:
length (int): the length of the rectangle
width (int): the width of the rectangle
Returns:
int: the area of the rectangle
"""
computed_area = length * width
return computed_area
def main():
print("Let's compute data about a rectangle!")
side_one = int(input("Enter length: "))
side_two = int(input("Enter width: "))
area = area_rectangle(side_one, side_two)
# STOP DRAWING: show the stack before we run the next line
perimeter = perimeter_rectangle(side_one, side_two)
print("The area is %d" % (area))
print("The perimeter is %d" % (perimeter))
main()
Let’s draw the stack:
-
Draw the empty stack and the heap.
-
When a function is called, create a stack frame for the function and put the frame on top of the stack. Usually, the first function called is
main
.-
Allocate parameters, if any, on the stack. The
main
function typically does not have parameters but many other functions do. -
Allocate variables used in the function. NOTE: For very large programs, you may want to skip drawing loop variables because they change frequently and can clutter your diagram. However, you should get in the habit of including them until you are comfortable enough to understand when they can be excluded. A loop variable is:
for <loop_variable> in <sequence>: <statements> ...
-
Execute the function line-by-line.
-
If another function is called, repeat at step 2.
-
If the function has a
return
value, we say that the function evaluates to be that value. Be sure to set the variable in the calling function to the returned value, if applicable.
-
-
When you reach the end of the function, remove it from the stack.
-
-
Continue executing the function that is now on top of the stack. If the stack is empty, the program is complete.
Below are the steps for drawing the stack for the code shown above. Line numbers are included so you can see how each line impacts the drawing. Try drawing the stack yourself and see if you get the same drawing!
Step | Line # | What happens to the stack diagram |
---|---|---|
0 |
Create the stack and the heap. |
|
Figure 1. Stack Diagram after step 0.
|
||
1 |
47 |
Put a frame for |
Figure 2. Stack Diagram after step 1.
|
||
2 |
35 |
Put the variables |
Figure 3. Stack Diagram after step 2.
|
||
3 |
36-37 |
Assume that the user types |
Figure 4. Stack Diagram after step 3.
|
||
4 |
22 |
The |
5 |
22 |
Put parameters |
Figure 5. Stack Diagram after step 5.
|
||
6 |
22 |
We assign the first parameter, |
7 |
22 |
We assign the second parameter, |
Figure 6. Stack Diagram after step 7.
|
||
8 |
31 |
Calculate |
Figure 7. Stack Diagram after step 8.
|
||
9 |
32 |
We reach the |
Figure 8. Stack Diagram after step 9.
|
||
10 |
39 |
Since the function is complete, we remove the stack frame for |
Figure 9. Stack Diagram after step 10.
|
||
11 |
42 |
Can you continue drawing the stack diagram starting on line 42? |