← ACARA Digital Technologies Years 7–8

Using a loop inside a function

A function that loops up to its parameter n, adds each number to a total declared inside it, then returns that total after the loop ends so the caller gets the finished answer.

Understanding Using a loop inside a function in ACARA Digital Technologies Years 7–8

What it does: The return statement sits outside the closing brace of the loop, so all the repeats finish before the total is sent back. The condition i <= n makes the loop stop at the number passed into the function, so sumTo(5) repeats 5 times. With i < n the loop stops before n, so sumTo(4) would give 6 instead of 10. Using <= includes n. total is declared inside the function, so code outside can't see it. The caller gets the answer from the returned value of sumTo(5). total += i adds i to the running total. total = i replaces the total each pass, so only the last number is kept.

Common mistakes: The loop uses i < n, so it stops before adding n. sumTo(5) would give 10 instead of 15. It should be i<=n so the last number is included. The function has no parameter, so n is not defined inside the loop condition. The header should be function countEvens(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 611–623) and word search (Modules 611–634).

Take this worksheet further — free with Cloodit

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

Learning
Duo
Which function correctly returns the total of 1 up to n?
function sumTo(n) { let total = 0; for (let i = 1; i <= n; i++) { total += i; return total; } }function sumTo(n) { let total = 0; for (let i = 1; i <= n; i++) { total += i; } return total; }
Matches
function sumTo(n) { let total = 0; for (let i = 1; i <= n; i++) { total += i; return total; } }
↓
The return is inside the loop, so the function stops after the first repeat and returns 1 instead of the full sum.
Applying
Meanings
Term
function sumTo(n)
Meaning
A function that loops up to its parameter n, adds each number to a total declared inside it, then returns that total after the loop ends so the caller gets the finished answer.
Practice
function sumTo(n) {
Testing
Gaps
total = 0;
Check Answer
Spots
2 let total = 0;
3 for (let i=1; i<n; i++)

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

The 'Woolly Mammoth' badge — Tundra 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 function correctly returns the total of 1 up to n?
A. function sumTo(n) { let total = 0; for (let i = 1; i <= n; i++) { total += i; } return total; } ✓
B. function sumTo(n) { let total = 0; for (let i = 1; i <= n; i++) { total += i; return total; } }
The return must come after the loop ends. Inside the loop, it would stop the function after the first pass, so sumTo(4) would give 1 instead of 10.
2. Where should the accumulator let total = 0; be declared in sumTo(n)?
A. Inside the loop, at the top of each pass
B. Inside the function, before the loop starts ✓
If total is declared inside the loop, it resets to 0 on every pass and the running total is lost. Declare it once before the loop.
3. What does sumTo(3) return if the function is written correctly?
A. 6 ✓
B. 3
The loop adds 1 + 2 + 3, which makes 6. The function returns the finished total, not just the last number.
4. Which statement about the loop in sumTo(n) is true?
A. The loop always repeats exactly 10 times, whatever n is
B. The loop uses the parameter n to decide how many times to repeat ✓
The parameter n sets the loop limit (i <= n). Calling sumTo(5) and sumTo(100) makes the loop run a different number of times.
5. A student wrote return total; as the very first line inside the for loop body. What happens?
A. The function ends on the first pass and returns too early ✓
B. The loop finishes all its passes, then the function returns
A return statement ends the function straight away. Any remaining loop passes never happen.

Match the Pairs

function sumTo(n) { let total = 0; for (let i = 1; i <= n; i++) { total += i; } return total; } → A function that loops from 1 up to n, adds each number to a total, then returns the finished total after the loop
let total = 0; → Declares the accumulator inside the function so it starts at 0 every time the function is called
The return must come after the loop ends, so the loop can finish adding every number first → for (let i = 1; i <= n; i++) { total += i; } return total;
The loop uses the parameter n to decide how many times to repeat → for (let i = 1; i <= n; i++)
function sumTo(n) { let total = 0; for (let i = 1; i <= n; i++) { total += i; return total; } } → The return is inside the loop, so the function stops after the first repeat and returns 1 instead of the full sum.
let total = 0; function sumTo(n) { for (let i = 1; i <= n; i++) { total += i; } return total; } → The total is declared outside the function, so it keeps its old value between calls and the second call gives a wrong, bigger answer.

Key Term

function sumTo(n)
A function that loops up to its parameter n, adds each number to a total declared inside it, then returns that total after the loop ends so the caller gets the finished answer.

Fill in the Blanks

1. function sumTo(n) { let total = 0; for (let i = 1; i <= n; i++) {
Hint: Type the keyword that declares the accumulator variable inside the function, so it can be changed in the loop.
2. } return total; }
Hint: Type the keyword that sends the finished total back to the caller. It goes after the loop ends, not inside it.
3. function sumTo(n) { let total = 0; for (let i = 1; i <= n; i++) {
Hint: Type the parameter name that sets the last number the loop counts up to.
4. for (let i = 1; i <= n; i++) { total += i; }
Hint: Type the shorthand operator that adds the loop counter to the running total on each repeat.
5. // 1 + 2 + 3 + 4 gives 10 let result = sumTo(4); console.log(result);
Hint: Type the name of the function that loops up to its parameter and returns the total.

Spot the Error

Question 1
A function sumTo(n) {
B let total = 0;
C for (let i=1; i<n; i++)
D total += i;
E return total;
F }
Line C — The loop uses i < n, so it stops before adding n. sumTo(5) would give 10 instead of 15. It should be i<=n so the last number is included.
Question 2
A function countEvens() {
B let count = 0;
C for (let i=2; i<=n; i+=2)
D count++;
E return count;
F }
Line A — The function has no parameter, so n is not defined inside the loop condition. The header should be function countEvens(n) {.
Question 3
A function stars(n) {
B let row = "";
C for (let i=0; i<n; i--)
D row += "*";
E return row;
F }
Line C — i-- makes the counter go down, so i<n stays true forever and the loop never ends. It should be i++ to count up.
Question 4
A function factorial(n) {
B let result = 1;
C for (let i=1, i<=n, i++)
D result *= i;
E return result;
F }
Line C — The three parts of a for loop header are separated by semicolons, not commas. It should be for (let i=1; i<=n; i++).
Question 5
A function sumSquares(n {
B let total = 0;
C for (let i=1; i<=n; i++)
D total += i * i;
E return total;
F }
Line A — The closing bracket after the parameter is missing. The function header should be function sumSquares(n) {.

Student with a class code? Join your class →