Functions, explained simply
You have already used a function: print(). Someone else wrote it so you could use it with one word. With def, you can invent functions of your own.
Defining a function is like writing a recipe card. You give it a name, write the steps underneath (indented), and put the card away. Nothing happens yet.
The steps run when you call the function by writing its name followed by brackets. Call it once, or a hundred times, and you never have to rewrite the steps.
Example: A delivery helper
def deliver():
print("Pick up the parcel")
print("Carry it to the door")
deliver()
deliver()Output
Pick up the parcel
Carry it to the door
Pick up the parcel
Carry it to the doorTry this
- Write a function called cheer() that prints a short celebration, then call it three times.
- Define a function but forget to call it. What happens when you run the program?
- Call one function from inside another function.
