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 return
2 def power(a, b):
3 answer = a ** b
4 power(2, 3)
5 print(answer)
6 # Run 2: return added, call stored
7 def power(a, b):
8 answer = a ** b
9 return answer
10 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.

1 / 13

Press Next to move one step. Watch the Variables panel and the Output box, and compare step 5 with step 12.

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

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 · Debugging

The total that vanished

The function total_price is correct, but the program cannot use the total on the last line. Click the line where the value is lost.

A pupil's program — which line goes wrong?

Computer Science · Writing subroutines

Build a discounted-total function

Build a function that takes a price and a discount (both made-up example numbers), works out the new price, and lets the main program use it. Choose the missing lines.

  1. # a function that gives back a price after a discountThe numbers in this example are invented for practice.
  2. missing step
Which line is step 2?

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.

6 of 6 still to sort.

Computer Science · Spotting a function

How can you tell it is a function?

A Python subroutine has been defined with def. You want to know whether it is a function.

Which idea is closest to what you think?
How sure are you?

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

1A subroutine is one section of code for one specific task, called when needed. It is either a procedure or a function.
2A function returns a value to the main program; a procedure returns no value. In Python the def line is the same for both.
3A returned value must be stored where the function was called, or it cannot be used again.
4A parameter is a local variable that receives the value of an argument. The number of arguments must match the number of parameters.

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?
A section of code designed to perform one specific task, which can be called whenever the main program requires it.
What is the one thing that makes a subroutine a function and not a procedure?
It returns a value (or values) to the main program.
What does the line return a + b do when it runs?
It sends a + b back to the main program and ends the function, so any statement after it does not run.
How do you keep a value that a function returns?
Store it where you call the function, for example result = add(num1, num2).
How do Visual Basic and Python define procedures and functions?
Visual Basic: Sub for a procedure, Function for a function. Python: def for both.
What does defining a subroutine do?
It does not run it. The subroutine only runs when the main program calls it.
What is the difference between an argument and a parameter?
The argument is the value in the call; the parameter, which acts as a local variable inside the subroutine, receives that value.
How many parameters can a subroutine have?
None, one or more. The call needs the same number of arguments as there are parameters.
Which of print() and input() is a function?
input() is a function. print() is a procedure.
Why can the main program not see a variable created inside a subroutine?
The variable is local to the subroutine. Only a value sent back with return reaches the main program.
What kind of code suits a procedure?
Code that only performs actions, such as printing a greeting message or displaying admission prices.
What happens if the main program uses a local variable of a procedure, such as answer?
It cannot see it, so a NameError results. Adding return answer and storing the result makes it a function that works.

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

See the full Edexcel Computer Science curriculum →

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.