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:
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.