GCSE · Computer Science · Edexcel · Spec 1CP2

Built-in and user-devised subprograms

You have used subprograms since your first line of Python. print() is one, input() is another, and one of them has quietly been handing you something all along.

Computer Science · Subprograms

Follow the call: what runs, what travels in, what comes back

This program defines a menu procedure and a function called add. Step through it and keep asking: has anything actually run yet?

Press Next one step at a time. Before each press, predict: has the screen output changed?

1 def show_menu():
2 print("1. Play")
3 print("2. Quit")
4 def add(x, y):
5 total = x + y
6 return total
7 print("Done")
8 show_menu()
9 answer = add(10, 15)
10 print(answer)
xytotalanswer

Output

 

Step 1: Python reads the first def line and remembers the name show_menu. It does NOT run the two print lines inside. Look at the output: still empty.

1 / 11

Step through the trace — every value is the lesson’s, not run by the page.

Step 1 of 11: Python reads the first def line and remembers the name show_menu. It does NOT run the two print lines inside. Look at the output: still empty..

Watch out: A def line only defines. The code underneath it just waits until something calls it.

Two questions, not one

Who made it, and does it hand a value back?

For each subprogram, tick every set that fits. Tick neither if it fits neither.

  • A Built-in (already there)
  • B Function (returns a value)
  1. print()
  2. input()
  3. show_menu(), the menu procedure you wrote
  4. add(x, y), the function you wrote

Now you write one

Build a function that averages three numbers

Write a function that takes three numbers and gives back their average. Then call it with 6, 8 and 10 and keep the answer. Choose each missing line.

  1. # Goal: a function that hands back the average of three numbers
  2. missing step
Which line is step 2?

Catch the lost value

Which line loses the value?

This program should add three scores, add the bonus, and display the total with the bonus. It does not work. Which line is the first one that goes wrong?

A student's program — which line goes wrong?

What's inside, stays inside

After add has finished, what can the main program see?

Remember add from the first activity. It has the parameters x and y. Inside it, total = x + y, and it ends with return total. The main program runs answer = add(10, 15), and add finishes.

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

Say it in your own words

Why bother with a menu procedure?

A program shows the same menu several times. The programmer puts the menu's print lines inside a procedure called show_menu() and calls it whenever the menu is needed. Give two advantages of doing this. [2 marks]

0 words · your answer stays on this page and is not sent anywhere.

WHAT YOU'VE LEARNED

A quick recap of today's lesson.

Procedures, functions, and how a call sends values in and gets one back

What you need to know

  • A subprogram is a section of code designed to do one specific task, called by the main program whenever it is needed.
  • A procedure runs its code but returns no value. A function returns a value to the calling program.
  • print() and input() are subprograms you have already used. User-devised subprograms are the ones you create yourself.
  • Defining a subprogram does not run it. The code only runs when the main program calls it.
  • Have a goJo writes def party(): with print("Cake!") inside it, runs the program, and the screen stays empty. Jo says Python is broken. What really happened?

    Jo defined party but never called it.

    Defining a subprogram does not run it. Nothing runs until the main program calls it, for example with party().

  • A call passes control, and any arguments, into the subprogram, where the parameters receive the values.
  • Have a goThe def line is def cheer(team): and the call is cheer("Reds"). Which value ends up inside team, and what is "Reds" called in the call?

    "Reds" ends up in team, and in the call it is an argument.

    Arguments are the values in the call's brackets, and they travel into the subprogram through the parameters. team is the parameter that receives it.

  • Parameters are treated as local variables inside the subprogram. A subprogram can have no, one or more parameters.
  • Arguments and parameters can have different names, but a call needs the same number of arguments as parameters.
  • When return runs, the function ends, so any line after it in the function does not execute.
  • Have a goA function has return 5 on one line and print("hi") on the very next line. You call it once. How many times does hi appear?

    None. The print line is never reached.

    When the return line runs, the function ends at once, so a statement after it in the function does not execute.

  • A returned value must be stored in a variable if the calling program wants to use it later.
  • A variable made inside a subprogram can only be seen inside that subprogram, so the main program cannot see it.

The big picture

A subprogram is a named section of code that does one job and only runs when it is called. A procedure just does the job. A function also hands a value back, and that value is lost unless the calling program stores it. Arguments go in through parameters, return sends a value out, and variables made inside a subprogram stay inside it.

Key points

1Defined is not called: nothing in a subprogram runs until the main program calls it.
2Arguments go in through parameters; a function sends a value out with return.
3return ends the function straight away, and the value is lost unless the call stores it.
4Variables made inside a subprogram, parameters included, stay inside it.

Worked example

Problem

Write a procedure called greet that takes one value, name, and displays Hello followed by the name. Then call it twice, once for Sam and once for Priya.

⚠ Watch out

Mixing up parameters and arguments. Parameters are the names in the def line; arguments are the actual values in the call's brackets. The names do not have to match, but the number of arguments must match the number of parameters.

🧠

Memory hook

Think of a subprogram as a recipe card. Writing the card cooks nothing; the call does the cooking. A function also hands back the dish, and you need a plate (a variable) to keep it.

✓

Check yourself

Pick any subprogram from this lesson without looking back. Say out loud whether it is built-in or user-devised, whether it is a procedure or a function, and what, if anything, comes back from it.

Flashcards

(14)
What is a subprogram?
A section of code designed to do one specific task, which the main program can call whenever it is needed. It is either a procedure or a function.
What is a procedure?
A named block of code that runs a task when it is called but does not return a value to the calling program.
What is a function?
A named block of code that is called to perform a task and returns a value to the calling program.
Does a def line run the subprogram?
No. Defining a subprogram does not run it. The main program has to call it.
What is a call?
An expression that passes control, and any arguments, to a subprogram.
What is a parameter?
A name in the def brackets that receives a value when the subprogram is called. It is treated as a local variable inside the subprogram.
What is an argument?
A value given in the brackets of a call. It is passed into the subprogram via the parameters.
How many parameters can a subprogram have?
None, one or more. A call needs the same number of arguments as parameters, though the names do not have to match.
What does return do in a Python function?
It hands the value back to the calling program and ends the function, so any statement after it in the function does not execute.
How do you keep the value a function returns?
Store it in a variable when you call the function, for example result = my_function(...). Otherwise it cannot be used later.
Can the main program see a variable made inside a subprogram?
No. It can only be seen inside the subprogram. Variables inside and outside can share a name but are different variables.
Which Python keyword defines a subprogram?
def, followed by a meaningful name. Python defines procedures and functions in the same way.
What are built-in and user-devised subprograms?
Built-in ones, like print() and input(), are already available to you. User-devised ones are subprograms you create yourself, such as a menu procedure.
Why is print() a procedure but input() a function?
input() returns the text entered, which is lost unless it is stored. print() puts things on the screen and returns nothing for the program to use.

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 14 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.