Module 4 · Functions · Lesson 4 of 4
Your first function
Time to write one. A function definition has four parts: the keyword def, a name, a pair of parentheses, and a colon — then the indented body underneath. Defining it doesn't run it; the body only runs when you call the function by its name.
def greet(): # ← the body, indented print("Hello!")
Calling it is the part worth watching closely. When Python reaches greet(), it jumps into the function, runs every line of the body, then returns to the exact spot it left and carries on.
Run this and read the output order: “Start”, then everything inside greet, then “End”. The call is a detour, not a fork.
One rule to remember: a function must be defined before it's called, so its def has to appear above the call in your file. And defining a function never runs it on its own — it just teaches Python the steps, ready for whenever you call.
That's a real function: defined, called, and back again. From here functions only get more powerful — passing in values, and handing results back out.