Modular programs: functions, testing and debugging
Long programs are not harder because they are long. They are harder because everything touches everything. Functions cut those links.
Lesson overview
What you'll learn in this lesson
Design and develop modular programs that use procedures or functions, and test and correct them systematically
Key learning points
- • Decomposition
- • Parameters and return values
- • Choosing test cases
- • Systematic debugging
This lesson at a glance
- 30 minutes
- 24 parts to scroll through
- 4 quick checks
- Marked quiz at the end
- Gentle pace: short sittings with pauses
Words to know
Scroll down — the lesson carries on below
Watch & discover
Part 1 of 24
Visual introductionPicture this
Modular programs: functions, testing and debugging
Long programs are not harder because they are long. They are harder because everything touches everything. Functions cut those links.
In a nutshell
Design and develop modular programs that use procedures or functions, and test and correct them systematically
What you already know
Part 2 of 24
Before we start
What you already know
You should already know sequence, selection and iteration, and how to write a loop in a text language.
Key words
Part 3 of 24
Key words
Words you'll need today
- Function
- A named block of code that does one job and can return a value.
- Parameter
- An input a function is given when it is called.
- Return value
- The result a function hands back.
- Test case
- An input paired with the output you expect.
- Trace table
- A table recording variable values line by line to find a bug.
Reset break
Part 4 of 24
Pause
That's sitting 1 of 6 done
Stretch, get a drink, look out of the window. There is no timer and nothing is counting down — your place is saved, so you can come back in five minutes or tomorrow.
Learning cycle
Part 5 of 24
Learning cycle 1 of 2
Breaking a problem into functions
A short piece of teaching, then a check to make sure it has landed.
Explore the idea
Part 6 of 24
Learn
Decomposition
Start from the job, not the code. A quiz program needs to load questions, ask one, mark an answer and report a score. Each of those becomes a function with a clear name and one responsibility, so a bug in marking cannot break loading.
Explore the idea
Part 7 of 24
Learn
Parameters and return values
A function that reaches out to global variables is only pretending to be separate. Pass what it needs as parameters and give back a result: mark(answer, correct) returning True or False can be tested on its own, at any time, without running the whole program.
Reset break
Part 8 of 24
Pause
That's sitting 2 of 6 done
Stretch, get a drink, look out of the window. There is no timer and nothing is counting down — your place is saved, so you can come back in five minutes or tomorrow.
Quick check
Part 9 of 24
Quick check
Part 10 of 24
Learning cycle
Part 11 of 24
Learning cycle 2 of 2
Testing until it earns trust
A short piece of teaching, then a check to make sure it has landed.
Reset break
Part 12 of 24
Pause
That's sitting 3 of 6 done
Stretch, get a drink, look out of the window. There is no timer and nothing is counting down — your place is saved, so you can come back in five minutes or tomorrow.
Explore the idea
Part 13 of 24
Learn
Choosing test cases
Test normal data, boundary data and erroneous data. For a mark-out-of-ten checker that means 7, then 0 and 10, then -1, 11 and the text 'seven'. Bugs cluster at boundaries, which is exactly where casual testing never looks.
Explore the idea
Part 14 of 24
Learn
Systematic debugging
Read the error message first — it usually names the line and the type of fault. Then trace the variables by hand, or print them, and compare with what you expected. Change one thing at a time; two simultaneous fixes hide which one worked.
Quick check
Part 15 of 24
Reset break
Part 16 of 24
Pause
That's sitting 4 of 6 done
Stretch, get a drink, look out of the window. There is no timer and nothing is counting down — your place is saved, so you can come back in five minutes or tomorrow.
Quick check
Part 17 of 24
Common mix-ups
Part 18 of 24
Common mix-ups
Things people often get wrong
- A syntax error stops the program running; a logic error runs happily and gives the wrong answer.
- Printing a value inside a function is not the same as returning it.
- Testing only with data you expect proves very little.
Challenge round
Part 19 of 24
Game · Sort it
Which of these are true?
Drag each card into the right column. Tap a card first if dragging is fiddly.
True
Not true
Reset break
Part 20 of 24
Pause
That's sitting 5 of 6 done
Stretch, get a drink, look out of the window. There is no timer and nothing is counting down — your place is saved, so you can come back in five minutes or tomorrow.
Challenge round
Part 21 of 24
Game · Recall cards
A function should ideally...
Card 1 of 4
Exit quiz
Part 22 of 24
Exit quiz
Show what you've learned
6 questions, marked together at the end. Nothing is timed.
1. Parameters are...
2. Decomposition means...
3. Erroneous test data is data that...
4. When debugging you should change...
5. A return value lets you...
6. Reusable code is easier when functions avoid...
Mastery quiz
Part 23 of 24
Marked quiz
End of lesson quiz: Modular programs: functions, testing and debugging
4 questions, marked with the reasoning shown. No timer.
1. A function should ideally...
2. Boundary testing for a 1–10 input means trying...
3. A logic error is one that...
4. A trace table is used to...
Lesson round-up
Part 24 of 24
Lesson round-up
Ready when you are
Quiz score
Not sat
Games
Not played
Points this lesson
0
Best run
0 in a row
Luna: 0 out of 4 on the practice checks. Only if you feel up to it — one more?
Ask LunaPart 1 of 24 · Watch & discover
