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.
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.
They tend to forget the pair of parentheses after the function name. This is particularly common in the function that do not require parameters. Because the hello function defined above does not require parameters, it’s easy to forget the parentheses. This is less common, but still possible, when trying to call functions that require parameters.
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.
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.