A recipe card you can reuse
Day 22 of 149
π Full deep-dive with code examples
The Recipe Card
Your mom has a recipe card for chocolate chip cookies.
Every time she wants cookies:
- She doesn't reinvent the recipe
- She just follows the card
- Same steps, same delicious cookies!
Functions are recipe cards for code!
In Code
Instead of writing the same thing over and over:
# The recipe (function)
def make_greeting(name):
return "Hello, " + name + "!"
# Use it anytime!
make_greeting("Alex") # "Hello, Alex!"
make_greeting("Sam") # "Hello, Sam!"
Write once, use many times!
The Magic
Functions can:
- Take inputs (ingredients)
- Do something (the recipe steps)
- Give output (the finished dish)
Input: name
Process: add "Hello" before it
Output: greeting
In One Sentence
Functions are reusable blocks of code that take inputs, do something, and return outputs.
π Enjoying these? Follow for daily ELI5 explanations!
Making complex tech concepts simple, one day at a time.
Top comments (0)