KS3 · Computer Science
Parameters and return values
A Python function is a sealed machine: values go in one slot, an answer comes out the other. Use it straight away or catch it in a variable.
Computer Science · Python subprograms
Follow one call of add
A function is 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.
Take it 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 runs none of it. Defining a function isn't running it.
Press 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?
Every one of these starts with a def line. Ignore that. Look at what the body does, then sort it.
Still to sort
Function (0)
Returns a value to the main program
Where the line is: return is the deciding feature. Python uses def for both, and the number of parameters makes no difference.
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.
They all start with def. They don't all take the same number of parameters. So what actually decides it?
Predict, then check
One surprising rule about return. Pick your answer before you peek.
A function called double has two lines in its body: first return n * 2, then print('Finished!'). The main program runs x = double(4) and then print(x). What shows up on screen?
WHAT YOU'VE LEARNED
A quick recap of today's lesson.
What you need to know
- A function hands a value back to the main program. A procedure just runs its code and returns nothing. Python defines both with def.
Have a goYour mate is certain: "show_score prints the score on screen, so it's obviously a function." show_score has no return line. Are they right?
No. It's a procedure.
Printing isn't returning. Showing something on screen hands nothing back to the main program, and without a return it stays a procedure.
- After def comes an identifier: a meaningful name for the subprogram, such as add or area.
- Parameters sit in the def line's brackets and let values into the subprogram. Inside, they act as local variables.
- A function can have no parameters, one, or several. More parameters means you can pass in more values.
- An argument is the value in the brackets of the call. It's passed into the subprogram through a parameter.
- In calculate(num1, num2) with parameters a and b, num1 goes into a and num2 into b. Names can differ; the count must match.
Have a godef power(base, exp) is called as power(2). What's wrong with that call?
It's one argument short: two parameters need two arguments.
The count must match. It's tempting to think the missing parameter just stays empty, but the call needs the same number of arguments as parameters.
- Writing def lines runs nothing. To run a subprogram, call it: its name, then the arguments in brackets.
Have a goA program contains the def lines for add and multiply, and nothing else. You press run. What appears on screen?
Nothing at all.
Defining isn't running. With no call, neither function ever starts, so the program isn't broken, it was just never asked to do anything.
- return, then a value, sends that value back. The moment return runs, the function ends, so lines after it never run.
- Want to use the returned value later? Store it in a variable, like answer = add(num1, num2).
- A variable made inside a function can only be seen inside it. Ask for it from the main program and you get a name error.
The big picture
A subprogram is a function if it returns a value to the main program, and a procedure if it just runs its code. Parameters let values in, arguments are the values you send, and return sends a result back out. Use that result straight away or store it in a variable for later. And variables made inside a function stay inside it.
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's returned, and it only sticks around 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 you'd lose if the call were written as add(num1, num2) on its own.
Flashcards
(13)Function or procedure: what's the one difference?
How does Python's syntax tell a function from a procedure?
What is a parameter?
What is an argument?
calculate(num1, num2) is called, and its parameters are a and b. Where does each argument go?
Must argument names match parameter names?
Can a function have zero parameters?
Does writing the def line run the subprogram?
Which keyword shares a value from a function with the rest of the program?
What happens to lines after a return line inside a function?
How do you keep a returned value for later?
Can the main program use a variable that was made inside a function?
A variable inside a function shares its name with one outside. 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
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 26 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.