GCSE · Computer Science · OCR · Spec J277

Sub programs: functions

Think of a function as a machine with a slot in and a slot out: values go in through the brackets, and one answer comes back out through return.

Computer Science · Functions

Follow the values in, and back out

Press Next to step through. The highlighted line is the one running, and the panel shows the variables that exist at that moment.

Before you press Next: what number ends up in result, and does "Well played!" ever get printed?

1 def final_score(points, bonus):
2 score = points + bonus
3 return score
4 print("Well played!")
5 result = final_score(40, 15)
6 print("You scored", result)

Variables

Output

 

Step 1: Line 1 defines a function called final_score, with two parameters: points and bonus. Its code only runs when it is called, so the program carries on at line 5.

1 / 7

Count the lines that actually run. One line in the function is never reached.

Step 1 of 7: Line 1 defines a function called final_score, with two parameters: points and bonus. Its code only runs when it is called, so the program carries on at line 5..

Watch out: Line 4 sits inside the function, but it comes after return. Keep an eye on whether it ever lights up.

Computer Science · Functions

Name every part

Pick a part of the program, then choose the job it does.

Still to sort

Identifier (0)

The meaningful name chosen for the function.

Where the line is: It names the function. It isn't a value going in or coming out.

Parameter (0)

A name in the brackets of the def line, ready to receive a value.

Where the line is: Parameter or argument? Parameters live in the definition's brackets. Arguments live in the call's brackets.

Argument (0)

A value in the brackets when the function is called.

Where the line is: Arguments are passed in through the parameters, so each one needs a matching parameter to land in.

Return value (0)

What the function sends back to the program that called it.

Where the line is: It travels out of the function, not in.

6 of 6 still to sort.

One short program: def area(width, height): return width * height size = area(4, 6)

Two kinds of subprogram

FunctionvsProcedure

Read down the rows. Only one row has two different answers that matter.

Focus

What it is

Function

A subprogram: a named block of code that is called to perform a task

Procedure

A subprogram: a named block of code that is called to perform a task

The insight

Identical so far. Both are subprograms, so the difference has to be somewhere else.

Sends a value back?

Function

Yes — it returns a value (or values) to the main program

Procedure

No — it does its task without returning a value

Defined in Python with

Function

def, then an identifier, e.g. def final_score(points, bonus):

Procedure

def, then an identifier — exactly the same syntax

Can take parameters?

Function

Yes: none, one or more

Procedure

Yes: values can be passed in the same way

After the call

Function

Store the returned value, e.g. in a variable, if you need it later

Procedure

Nothing comes back, so there's nothing to store

Computer Science · Functions

Can the main program see inside?

A function called level_up has one parameter, level. Inside it, new_level = level + 1 runs, then return new_level. The main program runs player = level_up(4) and then, on the next line, print(new_level).

Will print(new_level) show 5? Pick the idea closest to what you think right now.
How sure are you?

Computer Science · Functions

Where did the answer go?

This program should print Journey: 180 minutes. The function works fine. One line throws its answer away. Which one?

A student's program — which line goes wrong?

WHAT YOU'VE LEARNED

A quick recap of today's lesson.

Arguments go in through the parameters. One value comes back with return.

What you need to know

  • A subprogram is a named block of code you call to perform a task. It's either a function or a procedure.
  • Here's the difference: a function returns a value (or values) to the main program. A procedure doesn't.
  • Have a goA subprogram works out a player's total and sends that number back to the main program. Function or procedure?

    A function.

    It returns a value to the main program, and that's exactly what makes it a function. A procedure does its task without sending a value back.

  • In Python, you define one with def followed by an identifier — a meaningful name you choose.
  • Python uses exactly the same syntax for functions and procedures, because it doesn't distinguish between them.
  • Parameters sit in the brackets of the def line. They let values be passed in, and work as local variables inside.
  • Functions can have one parameter, several, or none at all.
  • To call a function, write its name, then the arguments in brackets. Each argument goes into its matching parameter.
  • Have a goA function starts def greet(name, age): and is called as greet("Mo", 15). Which parameter receives 15?

    age

    Each argument goes into its matching parameter, so the second argument, 15, lands in the second parameter, age — not in name.

  • When the return line runs, its value goes back to the calling program and the function stops. Lines after it never run.
  • Have a goYour mate swears his function says "Bye!" because he put print("Bye!") on the line straight after return. Is he right?

    No. That print never runs.

    The function stops the moment the return line runs, so anything after it is skipped, however sure your mate is. To print "Bye!", it has to come before return.

  • Store the returned value, for example in a variable, if you need it later.
  • Variables made inside a function stay inside. The main program can't use them unless the value is returned.

The big picture

A function is a subprogram that returns a value to the program that called it; a procedure doesn't. In Python both start with def and a meaningful name. The arguments in a call go into the matching parameters, return sends one value back and ends the function there and then, and the main program has to store that value to use it later. Variables made inside a function stay inside it.

Key points

1Function = a subprogram that returns a value. Procedure = a subprogram that doesn't.
2def + identifier starts both in Python.
3Parameters are in the definition; arguments are the values in the call.
4return sends one value back and ends the function on the spot.
5Catch the returned value in a variable, or it's gone.
6Inside variables are local: same name outside means a different variable.

Worked example

Problem

A cinema charges £12 per adult and £7 per child. Write a Python function called ticket_cost that takes the number of adults and the number of children and returns the total cost. Then use it to work out the cost for 2 adults and 3 children, keeping the answer so it can be printed.

⚠ Watch out

Mixing up parameters and arguments. Parameters are the names in the brackets of the def line. Arguments are the actual values in the brackets when you call the function, and they're passed into the parameters.

🧠

Memory hook

In through the brackets, out through return. After return? Nothing.

✓

Check yourself

In total = add(7, 2), what are the arguments, where does the returned value end up, and what makes add a function, not a procedure? (7 and 2; in total; it returns a value.)

Flashcards

(13)
What is a function?
A named block of code that is called to perform a task and returns a value.
How does a function differ from a procedure?
A function returns a value (or values) to the main program. A procedure doesn't.
Which Python keyword starts the definition of a subprogram, and what follows it?
def, followed by an identifier — a meaningful name for the subprogram.
Why does Python use the same syntax for functions and procedures?
Python doesn't distinguish between them, so both are defined with def.
What is a parameter?
A name in a subprogram's definition that lets a value be passed in. Inside, it's treated as a local variable.
How many parameters can a function have?
One, more than one, or none.
What is an argument?
A value held in the brackets of a subprogram call. It's passed into the subprogram through its parameter.
How do you call a function?
Write its name followed by the arguments in brackets.
What is a return value?
The value a function sends back to the program that called it, using return followed by the value.
What happens when a function's return line is executed?
The function terminates, so no line after it in the function runs.
What must you do with a return value if you want to use it later?
Store it, for example in a variable.
Can the main program use a variable declared inside a function?
No. It can only be seen inside that function — the main program only gets the value if it's returned.
A variable called count exists inside a function and another called count exists outside. Are they the same variable?
No. A variable inside a function is a different variable from one outside, even with the same name.

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 OCR GCSE Computer Science topics

See the full OCR Computer Science curriculum →

How this lesson was checked. This OCR GCSE Computer Science (specification J277)lesson 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 3 November 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.