Naming and Reusing Code Blocks
Give a group of code steps a name so you can use that group again and again without rewriting it.
Reading is good — doing is better. Practice Naming and Reusing Code Blocks as an interactive lesson.
Try the lessonDefinition
A code block is a set of instructions that work together to do one job. When you give that group a name — like 'dance' or 'drawSquare' — you create something called a function or procedure. After it has a name, you can run all those steps just by calling the name, instead of typing every step over and over.
Remember the rule
Define once, call many — write the steps ONE time, give them a name, then use that name as many times as you need.
Key words
- Code Block
- A chunk of instructions grouped together to do one task.
- Function
- A named code block you can call by its name whenever you need it.
- Procedure
- Another word for a function — a named set of steps you can reuse.
- Define
- To write out and name a function for the first time, like writing a recipe on a card.
- Call
- To use a function by saying its name, so the computer runs all the steps inside it.
- Reuse
- Using the same named block more than once instead of rewriting the steps.
- Parameter
- A changeable value you can hand to a function so it can do the job slightly differently each time.
- Bug
- A mistake in your code that makes it do the wrong thing.
Worked examples
You want a sprite to jump three times. Without a function you write: jump up, jump down, jump up, jump down, jump up, jump down. How can you make this shorter?
→ Define a function called 'jump': { move up 20, move down 20 }. Then call 'jump' three times: jump, jump, jump. That is 2 lines inside the function plus 3 call lines — much less repeated code. · If you later want the jump to go higher, you only fix it in one place inside the function.
You are drawing four squares on screen. Each square needs: move right 50, turn 90, move right 50, turn 90, move right 50, turn 90, move right 50, turn 90. What should you do?
→ Define a function called 'drawSquare' with those 8 steps inside it. Then call 'drawSquare' four times, once for each square. You wrote the 8 steps once instead of 32 times. · Reusing the function also means all four squares are identical — no copy-paste typos.
A function called 'celebrate' does: play sound, spin, flash lights. Your game should celebrate when a player scores AND when the game ends. Where do you call celebrate?
→ Call 'celebrate' in two places: once in the 'player scores' section and once in the 'game over' section. The computer runs all three steps each time without you rewriting them. · This is the main power of reuse — the same behavior in two spots, fixed in one spot.
You have a function 'greet' that says 'Hello, friend!' You want it to use a player's real name instead. How do you improve it?
→ Add a parameter called 'name' to the function: greet(name) { say 'Hello, ' + name }. Call it as greet('Maria') or greet('Leo') and it will say the right name each time. · Parameters let one function handle many slightly different situations.
You wrote 'drawSquare' but accidentally called it 'DrawSquare' (capital D) later in your code. The computer says it cannot find the function. What went wrong?
→ The name you defined ('drawSquare') and the name you called ('DrawSquare') do not match exactly. Fix the call to use the exact same spelling and capitalization: drawSquare. · Computers read names exactly as written — one wrong letter or capital is a bug.
Common mistakes
- Defining a function but forgetting to call it — the steps never run because you never used the name.
- Calling a function before defining it — in many languages the definition must come first.
- Misspelling the function name when calling it, so the computer cannot find it.
- Putting too many different jobs inside one function — a function works best when it does just one clear thing.
- Changing the steps inside a function and forgetting that every place that calls it will now behave differently too.
FAQs
What is the difference between defining a function and calling a function?
Defining is writing the recipe — you list all the steps and give them a name. Calling is using the recipe — you say the name and the computer follows all those steps right then.
Do I have to use functions, or can I just repeat the steps?
You can repeat steps, but it causes problems. If you repeat 20 lines in 5 places and need to fix one thing, you must fix it in 5 places. With a function you fix it in one place.
Can a function call another function?
Yes! A function called 'celebrate' could call 'playSound' and 'spinSprite', which are also functions. Building big things out of small named pieces is great coding style.
How do I pick a good name for a function?
Use a short word or phrase that describes exactly what the function does, like 'drawCircle' or 'moveLeft'. Avoid names like 'stuff' or 'myThing' that do not explain the job.
What happens if I define a function but never call it?
Nothing happens. The steps sit there waiting but never run. The computer only follows the steps when you actually call the function by name.
Is a function the same thing in Scratch, Python, and other languages?
The idea is the same everywhere — name a block of steps, then call the name. In Scratch it is called a 'My Block.' In Python you write 'def name():'. The word changes but the idea is identical.
Want the whole picture for your child?
Every K–6 subject, an AI tutor that teaches step by step, unlimited practice, and a reward world.
Start a 3-day free trial