Functions
A function is a recipe with a name.
You write the steps once.
Later you call the name whenever you need those steps.
Functions keep programs tidy and stop copy-paste.
Defining and calling
Start with def, then a name, parentheses, and a colon.
Indent the body.
Call the function by writing its name with parentheses.
Parameters are inputs
Parameters are empty bowls waiting for ingredients.
When you call the function, you pass values into those bowls.
return sends a result back
return hands a value back to the caller.
You can save that value in a variable.
If you skip return, the function gives back None.
Default arguments
You can give a parameter a starter value.
Callers can skip it, or override it.
Multiple returns
A function can return several values.
Python packs them as a tuple.
You can unpack them into separate names.
Why functions help
One place to fix a bug
Clear names that say what happens
Small pieces you can test alone
Less repeated code
Tip: Name functions after what they do. Return results when you need the answer later.
Test yourself
Three quick questions made just for this lesson. Earn 10 XP per correct answer.