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 + 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 runs none of it. Defining a function isn't running it.

1 / 9

Press 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 runs none of it. Defining a function isn't 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?

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.

6 of 6 still to sort.

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?

Computer Science · Return values

Where did the answer go?

This calculator should print 21. It doesn't. Which line is to blame?

A classmate's program — which line goes wrong?

Computer Science · Variables inside functions

Can the main program see inside a function?

Here's a 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 reckon 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 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

1Same number of arguments as parameters. The names don't have to match.
2A function can have no parameters, one, or several.
3A def only defines. A call runs.
4return ends the function on the spot.
5Inside and outside variables are different variables, even with the same 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'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?
A function returns a value to the main program. A procedure runs its code when it's called but doesn't return a value.
How does Python's syntax tell a function from a procedure?
It doesn't. Both are defined with 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's treated as a local variable.
What is an argument?
A value held in the brackets of a subprogram call. It's passed into the subprogram through a parameter.
calculate(num1, num2) is called, and its parameters are a and b. Where does each argument go?
num1 is passed into a, and num2 is passed into b.
Must argument names match parameter names?
No, the names can differ. The number can't: there must be the same number of arguments as parameters.
Can a function have zero parameters?
Yes. A function can have no parameters, one, or several.
Does writing the def line run the subprogram?
No. It only defines it. To run it, call it: its name followed by the arguments in brackets.
Which keyword shares a value from a function with the rest of the program?
return, followed by the value being returned.
What happens to lines after a return line inside a function?
They never run. Executing return ends the function.
How do you keep a returned value for later?
Assign the call to a variable, for example answer = add(num1, num2). Then the program that called the function can use it later.
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: name is not defined.
A variable inside a function shares its name with one outside. Same variable?
No, they're totally different variables. Still, avoid the same names: 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 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.

Helpful guides for parents and students