✦
✦
Computer Science · Coding Basics
Functions: Reusable Code Blocks
A function bundles a set of instructions into a named, reusable block — write it once, then call it whenever you need it!
🧩Write It Once, Use It Again & Again
✏️1. DEFINE the function
# make a block called jump
function jump() {
bend knees
push off ground
land softly
}
function jump() {
bend knees
push off ground
land softly
}
You write the steps one time and give the block a name: jump().
▶️2. CALL it whenever
# use it as many times
# as you want!
jump() # ⬆ hop 1
jump() # ⬆ hop 2
jump() # ⬆ hop 3
# as you want!
jump() # ⬆ hop 1
jump() # ⬆ hop 2
jump() # ⬆ hop 3
To call a function is to run it. No need to rewrite those 10 lines each time!
⚙️Inputs In, Answer Out
A parameter is the input a function expects. The value you actually hand it is the argument. When it finishes, it can send back a return value.
moveForward(5)
argument is 5 → moves 5 steps
moveForward(20)
argument is 20 → moves 20 steps
🪜Decomposition: Big Job → Small Blocks
🤖 Real-world clue: a vending machine is like a function — give it an input (code B4) and it hands back a snack. Press a TV remote button (a function!) again any time, without knowing how it works inside.
🔑Key Terms
🧩Function A named block of code that does a task and can be reused.
📥Parameter An input the function expects when it runs.
🎯Argument The actual value you pass in for a parameter.
📤Return Value The output a function sends back when it finishes.
▶️Call To run or use a function in your code.
🪜Decomposition Breaking a big problem into smaller, manageable parts.
⭐
Remember: write a function once, then call it as many times as you like — and break big problems into small functions to keep your code tidy!
✏️ Computer Science Anchor Chart · Functions: Reusable Code Blocks