KS3 · Computer Science
Parameters and return values
A Python function is like a sealed machine: values go in through one slot and an answer comes out of the other, but only if you catch it.
Computer Science · Python subprograms
Follow one call of add
Think of a function as a sealed machine with a slot in and a slot out. Step through one call and watch a value travel in, get worked on, and come back out.
Step through slowly. Watch where num1 and num2 go, when add actually starts working, and where the answer ends up.
1 def add(a, b):2 result = a + b3 return result4 num1 = 65 num2 = 46 total = add(num1, num2)7 print(total)
Variables
Output
Step 1: Python reads the def lines first. It learns that add exists and what it should do, but it does not run any of it. Defining a function is not the same as running it.
Use Next to move one step at a time. Every value shown was written into the lesson; the page isn't running any Python.
Computer Science · Subprograms
Function or procedure?
Each subprogram starts with a def line. Look at what its body does, then sort it.
Still to sort
Function (0)
Returns a value to the main program
Where the line is: The deciding feature is return. Python uses def for both, and the number of parameters doesn't matter.
Procedure (0)
Runs its code when called but returns no value
Where the line is: A procedure can have parameters and it can print. What it doesn't do is return a value.
Every one of these is defined with def. So what actually decides which is which?
Predict, then check
One surprising rule about return. Commit to an answer before you look.
A function called double has two lines in its body. First comes return n * 2, and after it comes print('Finished!'). The main program runs x = double(4) and then print(x). What appears on the screen?
WHAT YOU'VE LEARNED
A quick recap of today's lesson.
What you need to know
- A function returns a value to the main program. A procedure runs its code when it is called but returns no value. Python defines both with def and a meaningful name.
- A parameter sits in the def line and lets a value be passed in. An argument is the value in the brackets of the call. In calculate(num1, num2) with parameters a and b, num1 goes into a and num2 goes into b.
- Writing the def lines doesn't run anything. To run a subprogram, call it by writing its name followed by the arguments in brackets.
- return sends a value back to the main program and ends the function, so no line after it inside the function runs. To use the value later, store it in a variable.
- A variable made inside a function can only be seen inside it. Asking for it from the main program gives a name error (name is not defined).
The big picture
A subprogram is a function if it returns a value to the main program, and a procedure if it only runs its code. Parameters let values in, arguments are the values you send, and return sends a result back. The main program has to store that result to use it, because variables made inside a function can't be seen from outside.
Key points
Worked example
Problem
Write a function called area with two parameters, width and height, that returns the area of a rectangle. Call it with 5 and 3, store the answer, and print it.
⚠ Watch out
Expecting the main program to use a function's variable just by writing its name. It can't see inside the function. The answer only gets out if it is returned, and it only stays if the main program stores it.
Memory hook
In through the brackets, out through return, and catch it with a variable.
Check yourself
For total = add(num1, num2), say which argument lands in which parameter. Then say what is lost if the call is written as add(num1, num2) on its own.
Flashcards
(13)What is the difference between a function and a procedure?
How does Python tell a function from a procedure when you define one?
What is a parameter?
What is an argument?
In calculate(num1, num2), with parameters a and b, where does each argument go?
Do argument names have to match parameter names?
Can a function have no parameters?
Does writing a def line run the subprogram?
Which keyword sends a value out of a Python function?
What happens to the lines after a return line inside a function?
Why must a returned value be stored?
Can the main program use a variable that was made inside a function?
A variable inside a function and one outside share a name. Are they the same variable?
Tap any card to flip it, or use Study as deck to go through them one at a time. In the full lesson these run as a spaced-repetition deck — you rate each card Hard, Good or Easy and the tricky ones keep coming back until they stick.
Learning with Lightbulb is opening soon
You can use this lesson now. Join the waitlist and we'll let you know when the full Lightbulb experience is ready.
Keep me postedMore KS3 Computer Science topics
- Abstraction in computational thinking
- Adding binary numbers
- Binary to denary conversion
- Boolean logic: AND, OR, NOT
- Bubble sort
- Building truth tables
- Client-server vs peer-to-peer
- Collecting and recording data
- Comparing sorting algorithms
- Compressing data
- Creating a 3D animation
- Decomposition: splitting problems up
How this lesson was checked. This KS3 Computer Sciencelesson was published through Lightbulb Learning's human-designed editorial process — the educational standards, accuracy rules and publication checks it must pass were authored and approved by Philip Halpin. It passed subject-specific assessment, automated educational checks and technical publication verification before going live (publication checks completed 9 October 2026). Published pages are monitored, human spot-checking is ongoing across the lesson library, and anything found wrong is corrected or withdrawn. How our lessons are made and checked. Spotted a mistake? Email hello@lightbulblearning.co and we'll review it.