Section 4.3 Function Invocation
Defining a new function does not make the function run. To execute the function, we need a
function call. This is also known as a
function invocation.
The way to invoke a function is to refer to it by name, followed by parentheses. Since there are no parameters for the function hello, we wonโt need to put anything inside the parentheses when we call it.
Once weโve defined a function, we can call it as often as we like and its statements will be executed each time we call it.
Letโs take a closer look at what happens when you define a function and when you execute the function. Try stepping through the code above.
First, note that in Step 1, when it executes line 1, it does
not execute lines 2 and 3. Instead, as you can see in blue โGlobal variablesโ area, it creates a variable named hello whose value is a Python object of the โfunctionโ class. In the diagram that object is labeled hello() with a notation above it that it is a function.
At Step 2, the next line of code to execute is line 5. Just to emphasize that hello is a variable like any other, and that functions are Python objects like any other, just of a particular type, line 5 prints out the type of the object referred to by the variable hello. Itโs type is officially โfunctionโ.
Line 6 is just there to remind you of the difference between referring to the variable name (function name) hello and referring to the string โhelloโ. When we print a stringโs type, we see it is of the โstrโ class.
At Step 4 we get to line 8, which has an
invocation of the function. When a function is
called (or
invoked by a line of code, the lines of code inside the functionโs body are executed in the usual way,
but at the end of the functionโs code block, the programโs execution jumps back to line of code just after the function invocation.
You can see that by following the next few steps. At Step 5, the green arrow indicates the โline that just executedโ, aka the functionโs invocation, and the red arrow shows us the โnext line to executeโ which has moved to the function. We say that
control has passed from the top-level program (not โtopโ as in the โtop of the codeโ but as in top-hierarchial level) to the function hello. After Steps 5 through 7 print out two lines, at Step 8, control will be passed back to the point after where the invocation was started. By Step 9, that has happened.
The same process of invocation occurs again on line 10, with the functionโs body (lines 2 and 3) getting executed a second time.
Checkpoint 4.3.3.
What is a function in Python?
A named sequence of statements.
Yes, a function is a named sequence of statements.
Any sequence of statements.
While functions contain sequences of statements, not all sequences of statements are considered functions.
A mathematical expression that calculates a value.
While some functions do calculate values, the python idea of a function is slightly different from the mathematical idea of a function in that not all functions calculate values. Consider, for example, the turtle functions in this section. They made the turtle draw a specific shape, rather than calculating a value.
A statement of the form x = 5 + 4.
This statement is called an assignment statement. It assigns the value on the right (9), to the name on the left (x).
Checkpoint 4.3.4.
What is one main purpose of a function?
To improve the speed of execution
Functions have little effect on how fast the program runs.
To help the programmer organize programs into chunks that match how they think about the solution to the problem.
While functions are not required, they help the programmer better think about the solution by organizing pieces of the solution into logical chunks that can be reused.
All Python programs must be written using functions
In the first several chapters, you have seen many examples of Python programs written without the use of functions. While writing and using functions is desirable and essential for good programming style as your programs get longer, it is not required.
Not all functions calculate values.
Checkpoint 4.3.5.
How many lines will be output by executing this code?
def hello() -> None:
print("Hello")
print("Glad to meet you")
The code only defines the function. Nothing prints until the function is called.
Check again.
When the function is invoked, it will print two lines, but it has only been defined, not invoked.
Checkpoint 4.3.6.
How many lines will be output by executing this code?
def hello() -> None:
print("Hello")
print("Glad to meet you")
hello()
print("It works")
hello()
hello()
Here the the function is invoked and there is also a separate print statement.
There is only one print statement outside the funciton, but the invocations of hello also cause lines to print.
There are three print statements, but the function is invoked more than once.
Each time the function is invoked, it will print two lines, not one.
Three invocations generate two lines each, plus the line "It works".
You have attempted
of
activities on this page.