← ACARA Digital Technologies Years 7–8

Writing a program that uses functions and control structures

Build a program from small functions that take parameters and return values, then call them from a short main section. Add a comment above each function and test each one separately.

Understanding Writing a program that uses functions and control structures in ACARA Digital Technologies Years 7–8

What it does: The main section only calls the function addUp and prints the result, while the function holds the real steps. The comment sits above the function and describes its job, which helps other people read and fix the program. Brackets with an argument are needed to call a function. Without them, double just refers to the function and does not run it. A returned value can be stored, tested in an if statement or reused. A printed message cannot be reused. Putting a loop inside a function lets you reuse it by calling it, instead of repeating the same code. Testing each function on its own with a known input shows whether that function works before it is used in the whole program.

Common mistakes: The call has a spelling mistake. The function is declared as square, so squar is not defined and causes a ReferenceError. It should be console.log(square(5));. A JavaScript function is declared with the keyword function, not func. The line should be function isEven(n) {.

Class: _____________________  ·  Name: _____________________  ·  Date: _________

Free printable worksheet with full answer key — or add this module to a class and your students practise it interactively.

This module's key terms are also in a printable crossword (Modules 635–666) and word search (Modules 635–695).

Take this worksheet further — free with Cloodit

Each concept is reinforced from every angle — learned, then applied, then tested.

Learning
Duo
Which is the better way to structure a program?
Write small functions, then call them from a short main sectionPut every step in one long block with no functions
Matches
function double(n) { n * 2; } console.log(double(4));
↓
The function has no return statement, so it gives back undefined and the main section prints undefined instead of 8.
Applying
Meanings
Term
function double(n) { return n * 2; }
Meaning
Build a program from small functions that take parameters and return values, then call them from a short main section. Add a comment above each function and test each one separately.
Practice
// Returns the number doubled
Testing
Gaps
double(n) { return n * 2; }
Check Answer
Spots
3}
4console.log(squar(5));

Cloodit turns curriculum-aligned coding content into journeys students work through one biome at a time.

The 'Thorny Devil' badge — Detail Master, one of Cloodit's 106 collectible badges
Students earn collectable badges

Over 100 of them, across seven natural biomes.

Study Buddy
Students build a real study resource

Turns their work into revision material ready for Flash Cards and Exam study.

Diamond Dash — one of Cloodit's live, competitive Sprint games
Students lock in with Sprints

Live, competitive practice for the start of the lesson or a teacher-led lesson finisher.

A Cloodit certificate of achievement, ready to personalise and print
Custom printable certificates

Honour your top performers, or encourage a student who's trying hard — ready to print in seconds.

Quick Questions

1. Which is the better way to structure a program?
A. Write small functions, then call them from a short main section ✓
B. Put every step in one long block with no functions
Small functions each do one job. A short main section calls them in order, so the program is easier to read, test and fix.
2. Which function takes a parameter and returns a value?
A. function double(n) { return n * 2; } ✓
B. function double() { console.log(2); }
The first function takes n as input and gives back n * 2 with return. The second has no parameter and only prints.
3. Where is the best place for a comment that explains a function?
A. Hidden at the very end of the program
B. Directly above the function declaration ✓
A comment above the function tells the reader what the function does before they read its code.
4. Which main section calls the function correctly?
A. let result = double(5); console.log(result); ✓
B. let result = double; console.log(result);
Brackets with an argument are needed to call a function. Without them, double just refers to the function and does not run it.
5. How should you test a function in a larger program?
A. Run the whole program once and hope it all works
B. Call it on its own with known inputs and check the output ✓
Testing each function separately, for example double(4) should give 8, helps you find which function has a bug.

Match the Pairs

function isEven(n) { return n % 2 === 0; } → A small function that takes a parameter n and returns true or false so the main section can use it in an if statement.
console.log(double(4)); // expect 8 → A separate test that calls one function with a known input and compares the output with the expected answer.
The main section is short. It calls the functions and lets them do the work. → let total = addUp(5); console.log(total);
A comment above a function explains what the function does, what it takes in and what it returns. → // Returns the area of a rectangle function area(w, h) { return w * h; }
function double(n) { n * 2; } console.log(double(4)); → The function has no return statement, so it gives back undefined and the main section prints undefined instead of 8.
function double(n) { return n * 2; } let result = double; → The function is never called because the brackets and the argument are missing, so result holds the function and not the answer.

Key Term

function double(n) { return n * 2; }
Build a program from small functions that take parameters and return values, then call them from a short main section. Add a comment above each function and test each one separately.

Fill in the Blanks

1. function double(n) { return n * 2; }
Hint: Type the keyword that declares a new function.
2. // Doubles a number function double(n) { return n * 2; }
Hint: Type the keyword that sends a value back from the function.
3. function double(n) { return n * 2; } let result = double(5); console.log(result);
Hint: Type the name of the function being called from the main section.
4. function isEven(n) { return n % 2 === 0; } console.log(isEven(4));
Hint: Type the operator that checks whether two values are equal.
5. function double(n) { return n * 2; } for (let i = 1; i <= 3; i++) { console.log(double(i)); }
Hint: Type the keyword that starts a counting loop in the main section.

Spot the Error

Question 1
A function square(n) {
B return n * n;
C }
D console.log(squar(5));
Line D — The call has a spelling mistake. The function is declared as square, so squar is not defined and causes a ReferenceError. It should be console.log(square(5));
Question 2
A // Checks if a number is even
B func isEven(n) {
C return n % 2 === 0;
D }
Line B — A JavaScript function is declared with the keyword function, not func. The line should be function isEven(n) {
Question 3
A function greet(name {
B return 'Hi ' + name;
C }
D console.log(greet('Sam'));
Line A — The closing bracket after the parameter name is missing, so the brackets are not paired. The line should be function greet(name) {
Question 4
A function area(w, h) {
B return w * h;
C }
D console.log(area(5));
Line D — area needs two arguments but the call gives only one. h is undefined, so the result is NaN. The call should be area(5, 3).
Question 5
A function sign(n) {
B if (n < 0) {
C retrun 'negative';
D }
E return 'positive';
F }
Line C — The keyword return is spelt wrongly, so JavaScript gives a syntax error. The line should be return 'negative';

Student with a class code? Join your class →