đ A Teacherâs Map to Functions: Teaching with JavaScript and Scratch
Have you ever noticed how repetitive tasks make your learners sigh or lose focus? Imagine a classroom where students write the same lines of code again and againâjust to draw three squares! This, dear teacher, is where functions come in as our superheroes.
Functions are like magic boxes or helper robotsâyou give them instructions once, and they can repeat the task whenever you need, perfectly and instantly. In this blog-lesson, weâll explore functions using JavaScript and Scratch so your learners can see how the same concept works across both text and visual programming worlds.
đ§© Lesson 1: Why We Need Functions
Start with a relatable pain pointârepetition.
In JavaScript:
// Drawing three squares manually (ugh, so repetitive!)
drawSquare();
drawSquare();
drawSquare();
In Scratch:
đ© Blocks Needed:
- âWhen green flag clickedâ
- âRepeatâ
- âMove 100 stepsâ
- âTurn 90 degreesâ
Ask your learners to draw one square, then three squares. Theyâll likely just repeat their code blocks! Thatâs your teaching moment: âWhat if we could just tell Scratch to draw a square whenever we need one?â
đȘ Lesson 2: What is a Function?
A function is like a recipe or vending machine:
- It has a name (
drawSquare
) - Takes inputs (
size
,color
) - Performs a task (draws the shape)
- Produces an output (the drawn shape)
đ§ JavaScript Example
function drawSquare(size, color) {
console.log(`Drawing a ${color} square of size ${size}`);
}
drawSquare(100, "blue");
đ§© Scratch Example
In Scratch, you can make your own block:
Make a Block â drawSquare (size, color) Inside the block:
- Set pen color to
(color)
- Repeat 4 times â move
(size)
â turn 90°
Now youâve built your very own function block!
đ§Ÿ Lesson 3: The Parts of a Function
Letâs break the JavaScript function down like a recipe card:
Part | What It Means | JavaScript Example |
---|---|---|
Return Type | What it gives back | In JS, any type! |
Name | Identifier | drawSquare |
Parameters | Ingredients | (size, color) |
Body | The steps | Inside { } |
Return | The final dish | return something; |
đ Example:
function sum(a, b) {
let x = a + b;
return x;
}
console.log(sum(5, 10)); // Output: 15
In Scratch, this would be your custom reporter block, one that calculates and returns a value!
âïž Lesson 4: Calling a Function
You donât just create a functionâyou call it to make it do its work.
JavaScript
let answer = sum(5, 7); // Calls the sum function
console.log(answer); // Prints 12
Scratch
You drag your custom block (
sum of a and b
) into another script. Thatâs your function call!
đ Lesson 5: Functions That Just Do Things (Void Functions)
Some functions donât give anything backâthey just act.
JavaScript
function sayHello(name) {
console.log("Hello, " + name + "!");
}
sayHello("Ada");
Scratch
Make a Block:
sayHello(name)
Inside:say (join "Hello, " name)
Your robot (Scratch Cat) waves and says âHello, Ada!ââbut it doesnât return anything.
đ Lesson 6: Inputs â Pass by Value vs. Reference
Hereâs how to simplify this for young learners:
- Pass by Value: Giving your friend a photocopyâthey can draw on it without changing yours.
- Pass by Reference: Sharing a Google Docâeveryone sees changes in real time.
JavaScript Example
let score = 10;
function addBonus(x) {
x += 5;
}
addBonus(score);
console.log(score); // Still 10 (because itâs a copy)
Scratch Thought Experiment
If you make a clone of a sprite and change its color, the original stays the same â thatâs pass by value! But if you change a shared variable, all sprites see the new value â pass by reference.
đš Creative Activities
-
Temperature Converter
function convertFtoC(fahrenheit) { return (fahrenheit - 32) * 5 / 9; } console.log(convertFtoC(100));
Scratch version: Make a block that says,
set celsius to ((fahrenheit - 32) * 5 / 9)
-
Circle Area Calculator
function calculateCircleArea(radius) { return Math.PI * radius * radius; }
-
Greeting Function
function createGreeting(name) { return `Hello, ${name}!`; } console.log(createGreeting("Chemba"));
-
Shape Drawer Challenge In Scratch:
- Create blocks:
drawSquare(size, color)
anddrawTriangle(size, color)
- Combine them with loops to draw amazing patterns!
- Create blocks:
đ Conclusion
Functions are the superpower of programming: they save time, reduce mistakes, and make code easier to understand. Whether youâre clicking colorful blocks in Scratch or typing curly braces in JavaScript, the principle is the same â teach your robot once, and it can help you forever!
đ§© Review â Fill in the Gaps
- A function is like a reusable ____ that performs a specific task.
- The inputs of a function are called ____.
- The value that a function gives back is called the ____.
- In JavaScript, functions are created using the keyword ____.
- A function that doesnât return anything is called a ____ function.
- In Scratch, you can create your own function by clicking ____.
- Giving a photocopy to a function is like ____ by value.
- The code inside
{ }
in JavaScript is called the functionâs ____. - A function that just performs an action is also called a ____ function.
- To make your function run, you must ____ it by name.