Teacher Note: A procedure is not executed when it is defined¶
Note: this misconception applies both to procedures and functions
Misconception Some students may think that the indented lines of code below a procedure or function definition (called the body), are executed when the procedure or function is defined, rather than when the procedure or function is called.
Correct understanding A procedure is not executed when it is defined. The def
keyword merely instructs the computer to remember that the function name corresponds to the indented lines of code below it. Stated another way, def
instructs the computer to match the procedure or function name with the procedure or function body.
It is only when the procedure is called (the name is used without the def keyword) that the procedure body is executed. Here’s an example of a procedure definition followed by a procedure call:
Before you keep reading...
Making great stuff takes time and $$. If you appreciate the book you are reading now and want to keep quality materials free for other students please consider a donation to Runestone Academy. We ask that you consider a $10 donation, but if you can give more thats great, if $10 is too much for your budget we would be happy with whatever you can afford as a show of support.
How to tell if a student has this misconception Show the student a procedure definition with some print statements in the procedure body, but omit the code that calls the procedure. Ask the student to describe the output of the code. A student that has this misconception s/he will predict that the print statements will be executed, even if there is no call to the procedure yet.