When you trace a program you follow it line by line, in the order the computer actually runs the lines, and write that order down. It is the quickest way to prove to yourself that you understand what a program does, and it is where sub programs stop being mysterious: the jump from a call into a sub program and back is something you can see once you have the line numbers in front of you.
Here is a program with its lines numbered. Blank lines get a number too, so that the numbering matches what you would see in an editor.
1 def say_hi():
2 print("Why hello there!")
3
4 def offer_drink():
5 print("Would you care for a spot of tea?")
6
7 def offer_food():
8 print("Biscuit?")
9
10 def say_bye():
11 print("Cheerio then.")
12
13 print("Welcome to the hospitality program!")
14 say_hi()
15 print("What's your name?")
16 offer_drink()
17 print("Oh, lovely")
18 offer_food()
Write down the line numbers, in the order they will execute when the program is run.
Do it on paper, before you run anything. Take it slowly and say each step out loud. Two things are worth deciding as you go:
def line? Does the indented line underneath it
run straight away, or later, or not at all?Once you have your list, run the program below and check it against the output.
The order of the printed lines is the order the print lines ran in, so you can
read your trace back out of the output.
def say_hi():
print("Why hello there!")
def offer_drink():
print("Would you care for a spot of tea?")
def offer_food():
print("Biscuit?")
def say_bye():
print("Cheerio then.")
print("Welcome to the hospitality program!")
say_hi()
print("What's your name?")
offer_drink()
print("Oh, lovely")
offer_food()
1, 4, 7, 10, 13, 14, 2, 15, 16, 5, 17, 18, 8
Thirteen steps. Reading them in order:
def lines and creates four
sub programs, one after the other. That is all these lines do. The indented
lines underneath them, 2, 5, 8 and 11, do not run yet.Welcome to the hospitality program!say_hi.say_hi runs and prints Why hello there!, and the
sub program ends there, so the program jumps back to the line after the call.What's your name?.offer_drink.Would you care for a spot of tea?, then jumps back.Oh, lovely.offer_food.Biscuit?, then jumps back to the line after the call. There is
no line after it, so the program ends.Welcome to the hospitality program!
Why hello there!
What's your name?
Would you care for a spot of tea?
Oh, lovely
Biscuit?
Six lines, from six of the seven print statements in the file. Three of the six
are in the main program and three are inside sub programs, and they alternate,
because the program keeps jumping out and coming back.
Line 11, print("Cheerio then."), never runs. Line 10 defines say_bye, and
the main program never calls it, so its body is never reached. Nothing goes
wrong. A sub program that is defined but never called simply does nothing.
Lines 3, 6, 9 and 12 are blank. They are there to separate the definitions and make the program readable, and Python skips straight over them.
Every other line runs exactly once. That is worth noticing too: the lines that run out of order here run out of order because of the calls, not because any line runs twice. A sub program called from two different places would run twice, and tracing it would take you to the same line number twice.
Tracing gets much easier once you have watched it happen. Paste this program into Python Tutor, press Visualize Execution and step through it one line at a time: the arrow that jumps up into a sub program and back down again is exactly the thing you have just written down.