Python: Functions
Grouping code together and giving it a name.
The Hook
Think about a TV remote.
You press "Power". You don't need to know the circuit logic; you just press the button and it does the job.
Functions are like buttons. We group instructions, name the "button", and press it whenever we need it!
Worked Example
Defining and Calling a warning routine.
Predict Output
Order matters! What prints first?
print("A")
def beta():
print("B")
beta()
alpha()
Chorus Line
"Change the sing() function to print 'La' three times."
Silent Function
"The engine define but never runs. Call the function at the bottom of the code."
Game Loop
Independent Construction Pattern
- Define a function called
game_over(). - Inside, print
"Game Over"and"Try Again". - Call the function three times at the bottom.