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 + bonus3 return score4 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.
Count the lines that actually run. One line in the function is never reached.
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.
One short program: def area(width, height): return width * height size = area(4, 6)
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
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?
How does a function differ from a procedure?
Which Python keyword starts the definition of a subprogram, and what follows it?
Why does Python use the same syntax for functions and procedures?
What is a parameter?
How many parameters can a function have?
What is an argument?
How do you call a function?
What is a return value?
What happens when a function's return line is executed?
What must you do with a return value if you want to use it later?
Can the main program use a variable declared inside a function?
A variable called count exists inside a function and another called count exists outside. 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 OCR GCSE Computer Science topics
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.