🧩Function
Also known as Subroutine, Procedure
A named block of code that does one job. Write it once, use it many times.
Think of it like
Think of it like a toaster. Bread goes in, you press one button, and toast comes out.
Example
A shop writes one function, add_tax, that adds 20% to a price. Every page that shows a price calls it.
Why it matters
Functions stop you writing the same code twice. Fix a bug in one function and every place that uses it is fixed.
Definition
Example code
python
def greet(name):
return "Hi, " + name + "!"
print(greet("Emma")) # Hi, Emma!Common mistake
Key takeaways
- 1One job. A good function does one clear thing.
- 2Write once. Use it from as many places as you like.
- 3In and out. Values go in; a result comes back.