Introduction
A function (also called a procedure, method, or custom block) is a named bundle of instructions you can run whenever you need that action. Instead of copying the same five steps into ten places, you write them once as showVictory(), then call that name when victory happens.
This lesson connects algorithms, variables, and loops. Functions help you organize growing programs—especially your upcoming mini project and block coding stacks. Clean names matter; so does typing them accurately via practice.
Learning Objectives
By the end of this lesson, you will be able to:
- Define and call a simple function in pseudocode
- Pass parameters into a function
- Describe return values at a beginner level
- Refactor repeated steps into a named function
- Explain readability and reuse benefits
Main Lesson
Define vs call
Define means write the recipe and give it a name.
Call means say the name so the recipe runs now.
```text
FUNCTION greet(name)
PRINT "Hello, " + name
END FUNCTION
CALL greet("Sam")
CALL greet("Priya")
```
One definition; many calls. That is reuse.
Why functions exist
| Without functions | With functions |
|---|---|
| Same code pasted everywhere | One trusted definition |
| Fix a bug in 12 places | Fix once, all calls improve |
| Hard to read long scripts | Named actions read like a story |
| Tough teamwork | Teammates own different functions |
Functions are organizational superpowers, not just advanced trivia.
Parameters: inputs to a function
A parameter is a placeholder variable in the function definition. An argument is the actual value you pass when calling.
```text
FUNCTION addPoints(current, bonus)
RETURN current + bonus
END FUNCTION
SET score TO addPoints(10, 5) // score becomes 15
```
Parameters make functions flexible—the same addPoints works for many scores.
Return values: outputs from a function
Some functions do something (print, move a robot, play a sound). Others calculate something and return a result to the caller. Returned values can be stored in variables or used inside conditions.
Beginner mindset:
- Procedures/actions → focus on effects
- Value-returning functions → focus on answers
Many languages blur the words; the idea of “input → work → output” stays useful.
Function design tips
- One job per function —
updateScoreshould not also draw the entire map and send email. - Clear names as verbs —
calculateAverage,resetTimer,drawHeart. - Short bodies — if a function scrolls forever, split it.
- Know your inputs/outputs — comment them in pseudocode for class projects.
| Weak design | Stronger design |
|---|---|
doStuff() | applyDiscount(price, percent) |
Giant playGame() doing everything | startGame(), playRound(), endGame() |
| Magic numbers inside | Parameters for adjustable values |
Scope (gentle intro)
Variables created inside a function are often local—they exist during that call and do not always overwrite outer variables with the same name. Beginners should avoid reusing confusing duplicate names across levels until comfortable. Prefer passing parameters and returning results.
Functions in block coding
In Scratch-like tools, My Blocks / custom blocks are functions. You define a cap block with a name and optional inputs, then snap a call wherever needed. Same skill: define once, reuse often—see Block Coding.
Real-world function thinking
Apps call functions constantly: format date, validate email, shuffle playlist, compute WPM after a typing sample on TYPE10X Practice. You experience the outputs; developers maintain the named units behind them.
Key Definitions
- Function / procedure — A named reusable set of instructions.
- Define — Create the function’s body and name.
- Call / invoke — Run a defined function.
- Parameter — Input placeholder in the definition.
- Argument — Actual value supplied in a call.
- Return value — Result sent back to the caller.
- Refactor — Restructure code (often into functions) without changing intended behavior.
- Abstraction — Hiding detail behind a useful name.
- Local variable (intro) — Variable limited to a function’s context.
- Library / API (intro) — Collections of ready-made functions others wrote for you.
Examples
Example 1: Area helper
FUNCTION area(width, height) RETURN width * height → call for different rectangles.
Example 2: HUD refresh
FUNCTION refreshHUD() updates score text and lives icons—called after each event.
Example 3: Validation
FUNCTION isEmail(text) returns true/false; login form uses it in a condition.
Example 4: Loop + function
For each student name, call printCertificate(name).
Real-World Scenarios
Scenario A — Group game jam
One student owns movePlayer, another owns checkCollisions. Functions become contracts for teamwork.
Scenario B — Bug multiplication
A scoring mistake was pasted into four rooms of a game. Moving score logic into addScore(points) fixed all rooms after one edit.
Scenario C — Teacher toolkit
A classroom template provides empty function stubs (setup, update, draw). Students fill bodies without redesigning the whole structure.
Tips
Warnings
Did You Know
Common Mistakes
- Defining a function but forgetting to call it
- Calling with the wrong number/order of arguments
- Writing mega-functions that do unrelated jobs
- Using vague names (
process,handle,misc) - Confusing return values with printed side effects
Interactive Exercise
Function Factory (15 minutes)
Refactor this repeated plan into functions:
- Show banner
- Ask for player name
- Show banner again
- Ask for team color
- Show banner again
Tasks:
- Write
FUNCTION showBanner() - Write
FUNCTION askText(prompt)that returns the answer (pseudocode) - Rewrite the main algorithm using calls
- List parameters and return values you used
Practice Questions
- What is the difference between defining and calling a function?
- What is a parameter?
- Why do functions reduce bugs?
- Give a good verb-style function name for resetting a timer.
- How are custom blocks related to functions?
Mini Challenge
Design three functions for a virtual pet:
feed(petHunger)→ returns new hungerplay(petEnergy)→ returns new energystatus(name, hunger, energy)→ prints a summary
Write pseudocode and a short main program that calls each at least once.
Summary
Functions package instructions under clear names so you can reuse, share, and repair logic in one place. Parameters bring inputs; returns send outputs; good design keeps each function focused. Together with loops and variables, functions turn scattered scripts into readable systems—ready for sharper conditions and a polished mini project.
Student Checklist
- [ ] I can define vs call a function
- [ ] I can explain parameters and arguments
- [ ] I understand beginner return values
- [ ] I refactored repeated steps into a function
- [ ] I completed Function Factory and the mini challenge
Teacher Notes
- Compare functions to classroom jobs: “pencil sharpener monitor” as a named role.
- Use sticky-note function cards students can “call” by tapping.
- Provide messy duplicated pseudocode for timed refactors.
- Preview conditions by writing
FUNCTION isPassing(score). - Encourage verb naming conventions on a shared word wall.
FAQ
Q: Are functions required for tiny programs?
Not always—but learning them early prevents pain as projects grow.
Q: Can functions call other functions?
Yes. That is normal and powerful when each stays focused.
Q: What if two functions need the same variable?
Pass values as parameters or carefully use shared state with teacher guidance.
Q: Do all functions return values?
No. Some only perform actions. Know which kind you wrote.
Q: What should I learn next?
Continue to Conditions to branch program paths with IF/ELSE.
Related Lessons
Related Blog Posts
- Read practical learning guides on the TYPE10X Blog
- Strengthen keyboard skills at Practice
Next Lesson CTA
You can now package reusable actions as named functions. Next, learn how programs choose different paths—continue to Conditions.