imagine that you're jumping back to your function definition
…and then executing the body of your function
once it's done, execution goes back to where you called your function (printed done for clarity)
Just the Definitions, Please!
Let's look at the same program, but without the function call. What do you think the output of this program will be? →
(That's supposed to be nothing! A function definition alone won't execute the code in a function.)
Just the Calls, Please!
What about this version of the program? What do you think the output will be? →
We get a NameError!
This may be obvious, but a function has to be defined before it can be used.
Multiple Function Definitions
You can define more than one function in your program!
Functions in Functions
In fact, you can use a function that you've already defined in another function that you're creating. What do you think the output of this program will be? →
Functions in Functions Continued
Notice that in the previous code…
we defined a function called say_moo
we defined another function called main that uses say_moo
"Local" Variables
variables defined in your function are available only to that function
the variables in your function are local to that function
they cannot be used in another function or outside of the current function definition
"Local" Variables Continued
The variables a and b are local to my_first_function and my_second_function respectively.
"Local" Variables Continued Some More
Because variables defined within a function are local
using the same variable name in two different functions doesn't cause any errors
nothing gets overwritten
(calling both functions consecutively prints out 1, then 200)
Defining a Main Function
it is common practice to have the entirity of your program in a single function called main
main is usually defined at the end of the file
with other additional functions defined outside as well
the examples in Starting Out with Python use this pattern often