GCSE · Computer Science · Edexcel · Spec 1CP2
Functions vs procedures
Two Python subroutines can look almost identical, and one of them quietly throws its answer away. Which one is it, and which single line decides?
Computer Science · Subroutines
One line apart: a lost answer and a working function
The same subroutine runs twice. Step through both runs and watch where they part company.
Before you press Next: when power works out 2 ** 3, where do you think the answer 8 ends up?
1 # Run 1: no return2 def power(a, b):3 answer = a ** b4 power(2, 3)5 print(answer)6 # Run 2: return added, call stored7 def power(a, b):8 answer = a ** b9 return answer10 result = power(2, 3)11 print(result)
Variables
Output
Step 1: Run 1. Python reads def power(a, b) and remembers it. Nothing inside has run yet: defining a subroutine is not the same as calling it.
Press Next to move one step. Watch the Variables panel and the Output box, and compare step 5 with step 12.
Predict, then check
Commit to an answer before you look.
A function has three lines in this order: def double(n): then return n * 2 then print("Doubling done!"). The main program calls double(5). Does "Doubling done!" appear on the screen?
Computer Science · Choosing a subroutine
Procedure or function?
For each job, decide: does the main program need a value handed back?
Still to sort
Procedure (0)
Only performs actions. Returns no value.
Where the line is: If the main program only needs the job done, not an answer, it suits a procedure.
Function (0)
Returns a value to the main program.
Where the line is: If the main program must receive a value to use, it has to be a function.
WHAT YOU'VE LEARNED
A quick recap of today's lesson.
Two subroutines that look almost the same. Only one hands an answer back.
What you need to know
- A subroutine is a section of code that does one specific task and can be called whenever the main program needs it.
- A function is a subroutine that returns a value (or values) to the main program.
- A procedure runs a block of code when called but returns no value. That is the difference.
Have a goA subroutine works out a pupil's age in years and the main program needs that number afterwards. Function or procedure?
A function.
The main program needs a value back, and only a function returns a value; a procedure would do its job and hand nothing back.
- In Python, return a + b sends the value back and ends the function, so a line after it never runs.
- Store the returned value, for example result = add(num1, num2), or you cannot use it again.
- Python defines both kinds with def. Visual Basic uses Sub for a procedure and Function for a function.
- Defining a subroutine does not run it. The main program has to call it.
Have a goPriya writes def hello(): print('Hi') at the top of her program, runs it, and groans: 'Nothing printed. The computer is broken!' What did she forget?
She never called hello().
Defining a subroutine does not run it. Only a call from the main program makes the code inside run.
- Parameters receive the values of the arguments in the call, in order. The counts must match, and a subroutine can have none, one or more.
Have a goA subroutine has parameters x and y and is called with the arguments 10 and 4. What values do x and y receive?
x is 10 and y is 4.
Parameters receive the values of the arguments in the call, in order, which is why the number of arguments has to match the number of parameters.
- Code that only performs actions suits a procedure; code that must return a value must be a function. print() is a procedure and input() is a function.
The big picture
A subroutine is a section of code that does one specific task and is called when the main program needs it. If it returns a value to the main program it is a function; if it just runs its code and returns nothing it is a procedure. You will watch one subroutine fail and then work, learn that a returned value must be stored, and practise choosing the right kind for a task.
Key points
Worked example
Problem
A program contains: def greet(name): print('Hello ' + name), then the line greet('Sam'). What happens when it runs, and is greet a function or a procedure?
⚠ Watch out
Working something out inside a subroutine, or printing it there, and expecting the main program to see it. A local variable such as answer stays inside the subroutine. A value only comes back through return, and the main program has to store it where it calls the function.
Memory hook
A function comes back holding something. A procedure does its job and comes back empty-handed.
Check yourself
perimeter(w, h) works out 2 * (w + h) but the main program cannot use it. What line must perimeter gain, and what must the call do?
Flashcards
(12)What is a subroutine?
What is the one thing that makes a subroutine a function and not a procedure?
What does the line return a + b do when it runs?
How do you keep a value that a function returns?
How do Visual Basic and Python define procedures and functions?
What does defining a subroutine do?
What is the difference between an argument and a parameter?
How many parameters can a subroutine have?
Which of print() and input() is a function?
Why can the main program not see a variable created inside a subroutine?
What kind of code suits a procedure?
What happens if the main program uses a local variable of a procedure, such as answer?
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 Edexcel GCSE Computer Science topics
- Built-in and user-devised subprograms
- Code readability techniques
- Converting algorithms to programs
- CSV file I/O
- Decomposition and abstraction in programming
- Evaluating program fitness and efficiency
- Global vs local variables
- Identifying and correcting program errors
- Primitive data types
- Programming – condition-controlled repetition
- Programming – count-controlled repetition
- Programming – iteration over data structures
How this lesson was checked. This Edexcel GCSE Computer Science (specification 1CP2)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 21 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.