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 + b
3 return result
4 num1 = 6
5 num2 = 4
6 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.

1 / 9

Use Next to move one step at a time. Every value shown was written into the lesson; the page isn't running any Python.

Step 1 of 9: 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..

Watch out: Writing the def lines doesn't run add. Nothing inside it happens until the call, total = add(num1, num2).

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.

6 of 6 still to sort.

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?

Computer Science · Return values

Where did the answer go?

This calculator should print 21, but it doesn't. Which line is the problem?

A student's program — which line goes wrong?

Computer Science · Variables inside functions

Can the main program look inside a function?

Look at this program. The function add has two lines in its body: result = a + b, then return result. The main program runs total = add(6, 4), and its very last line is print(result).

What do you think that last line does?
How sure are you?

Computer Science · Modular programs

Build the calculator

Finish this calculator. It has a function for each operation, and each one returns its result. The main program calls them with two numbers. Choose the missing lines.

  1. def add(a, b):
  2. missing step
Which line is step 2?

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

1Same number of arguments as parameters, but the names don't have to match.
2A function can have no parameters, one parameter or several.
3A def only defines. A call runs.
4return ends the function straight away.
5Inside and outside variables are different variables, even if they share a name. Avoid the same names, because it makes testing and debugging difficult.

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?
A function returns a value to the main program. A procedure runs its code when called but does not return a value.
How does Python tell a function from a procedure when you define one?
It doesn't. Both use the keyword def followed by a meaningful name.
What is a parameter?
A name in the brackets of the def line that lets a value be passed into the subprogram. Inside it, it is treated as a local variable.
What is an argument?
A value held in the brackets of a subprogram call. It is passed into the subprogram through a parameter.
In calculate(num1, num2), with parameters a and b, where does each argument go?
num1 is passed into a and num2 is passed into b.
Do argument names have to match parameter names?
No. But there must be the same number of arguments as parameters.
Can a function have no parameters?
Yes. A function can have none, one or several parameters.
Does writing a def line run the subprogram?
No, it only defines it. To run it, call it by writing its name followed by the arguments in brackets.
Which keyword sends a value out of a Python function?
return, followed by the value being returned.
What happens to the lines after a return line inside a function?
They don't run, because executing return ends the function.
Why must a returned value be stored?
To use it later in the program that called the function, assign it to a variable, for example answer = add(num1, num2).
Can the main program use a variable that was made inside a function?
No. It can only be seen inside the function. Referring to it from the main program gives a name error: the name is not defined.
A variable inside a function and one outside share a name. Are they the same variable?
No, they are totally different variables. Avoid the same names, because it can make testing and debugging difficult.

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 posted

More KS3 Computer Science topics

See the full KS3 Computer Science curriculum →

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.