✦ ✦
Computer Science · Coding Basics

Defining and Calling Functions

A function bundles a set of instructions into a reusable block. Give this example a name so you can call it again.
🧩The jump function
✏️1. DEFINE the function
# make a block called jump
function jump() {
bend knees
push off ground
land softly
}
You write the steps one time and give the block a name: jump().
▶️2. Call the function
# call the same function
# three times
jump() # ⬆ hop 1
jump() # ⬆ hop 2
jump() # ⬆ hop 3
To call a function is to run it. Each call runs the same three jump instructions.
⚙️Parameters, arguments and return values
PARAMETER steps ⬇ input moveForward(steps) moves the given distance RETURN VALUE “moved 5” output ⬇
A parameter is a name in the definition, such as steps. The value supplied in a call is an 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
🪜Dividing a task into smaller parts
🎮 BIG PROBLEM makeGame() 🦘 jump() small + reusable ⭐ scorePoints() small + reusable 🔊 playSound() small + reusable
🤖 Compare a function with a machine. On a vending machine, enter a selection such as code B4 to receive a snack. A TV remote button also requests an action without requiring you to know the circuit inside.
🔑Key Terms
🧩Function A reusable block of code that performs a task. Give it a name, such as jump, to call it from other code.
📥Parameter A name in a function definition that receives a value when the function is called.
🎯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.
⭐ Compare moveForward(5) with moveForward(20). The function stays the same, but its argument changes how far it moves.
✏️ Computer Science Anchor Chart · Functions: Reusable Code Blocks
ClickClass offers hundreds of free printables at clickclassedu.com/printables.