GCSE · Computer Science · Eduqas · Spec C500QS

Creating and extending Python 3 functions/methods

A function prints 7 on the screen, but when you ask it for its answer, Python hands you None. Where did the 7 go?

Computer Science · Functions

Predict the call before Python does

Call each marked step before the table shows it. Nothing is run here: every value comes from a program that was checked in real Python 3. The screen row shows what has been printed so far, and it is not a variable.

def say_sum(a, b):
print(a + b)
def take_away(a, b):
return a - b
print("done")
total = say_sum(3, 4)
print(total)
gap = take_away(10, 4)
print(gap)

Variables

say_sum=a function, remembered but not run

Step 1. Worked for you. The def line only writes the recipe down under the name say_sum. Neither body line runs.

Step 1 of 10Called right: 0 of 6

Ask yourself three things: what is printed, what does the variable hold, and where does control go next?

Computer Science · Writing a function

Build stats() from def to call

Write a function called stats that takes two scores and gives back their total and their average. Then call it with 8 and 5 and keep the result.

  1. # stats(): give back the total and the average of two scoresA comment starts with # and Python ignores it. It just says what the function is for.
  2. missing step
Which line is step 2?

Exam line: Header, body, return, call: define the function, give a value back with return, then call it and store what comes back.

Computer Science · Extending a function

Extend greet() without breaking it

greet() used to take just a name. It has been extended with a greeting, and the program below calls it three times. The last call should greet Ben with "Hi". Press Run, read what Python says, then choose how to fix the flagged line.

Python
  1. def greet(name, greeting):
  2. print(greeting + ", " + name)
  3. greet("Sam", "Hi")
  4. greet("Priya")
  5. greet("Hi", "Ben")

Every message and output here is what Python 3.13 gives for these exact programs. Other Python versions word a few messages differently.

Computer Science · Methods

What does each method do to the object?

A method is called with dot notation, like name.upper() or scores.append(7). For each method below, tick every property it has, then check.

upper()
strip()
replace()
append()
sort()
pop()
index()

Computer Science · Writing a method

Where does self come from?

Here is a class with one method, and a call to it: class Pet: def describe(self, prefix): return prefix + ", I am a pet" pet = Pet() print(pet.describe("Hi")) The def line lists two parameters, self and prefix. The call has only "Hi" in its brackets.

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

Computer Science · Why use functions

Write it, then mark it

A program needs the same block of code in several places. Explain why a programmer would put that code into a function. [4 marks]

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

Exam line: Give the benefit, then say what makes it true: a change made once applies everywhere the function is called.

WHAT YOU'VE LEARNED

A quick recap of today's lesson.

Define it, call it, and know exactly what comes back.

What you need to know

  • A function is a named, reusable block of code. You run it by writing its name followed by brackets.
  • To define one in Python 3, write def, the name, brackets holding any parameters, and a colon. The indented lines below are the body.
  • Defining a function does not run it. The body runs only when the function is called, and the def must be executed first.
  • Have a goJo writes def hello(): and print("Hi") indented underneath, runs the program, and says "Why is my screen empty?" What would you tell Jo?

    The function has only been defined. Jo needs to call it by writing hello() on a later line.

    Jo thinks the def line runs the body, but def only writes the function down. The body runs when the function is called.

  • Parameters are the names in the def brackets. Arguments are the values in the call, matched in order unless passed by name.
  • With several parameters, every one without a default needs an argument. Too few arguments, or too many, raises a TypeError.
  • return sends a value back to where the function was called, and ends the function at that line. With no return, you get None.
  • Have a goA function has return 5 on one line and print("hi") on the line below it. Does hi appear when you call the function?

    No. return ends the function at that line.

    It is tempting to think every line in the body runs, but the moment return runs the function is over, so lines below it are skipped.

  • A returned value can be stored, printed, or used in an expression. Commas after return send back several values together, as a tuple.
  • To extend a function, give a new parameter a default value so old calls keep working. Defaults go after parameters without them.
  • A method is a function that belongs to an object, called like name.upper(). String methods return new values; append() and sort() change the list and return None.
  • Have a goPriya writes scores = scores.append(7) and expects the longer list back. What does scores hold afterwards?

    None.

    append() changes the list in place and returns None, so assigning its result to scores replaces the list with None.

  • Write your own method with def inside a class. Its first parameter, usually called self, is the object, and Python passes it in for you.

The big picture

A function is a named block of code that you define with def and run by calling it. Arguments fill the parameters in order, return sends one value back and ends the function, and a function with no return gives back None. A method is a function that belongs to an object and is called with dot notation. Extending a function, for example with a default value, lets it do more without breaking the calls you already wrote.

Key points

1def defines a function; the body runs only when the function is called.
2Parameters are in the definition; arguments are in the call and are matched in order unless passed by name.
3return hands one value back to the call and ends the function; with no return the function gives back None.
4A default value on a parameter keeps existing calls working after a function is extended, and defaults go after non-defaults.
5A method is a function called on an object with dot notation; string methods return new values, while append() and sort() change the list and return None.
6In a method defined inside a class, self is the object the method is called on, and Python passes it in automatically.

Worked example

Problem

What does this program print, and why? def label(name, tag="Guest"): return name + " (" + tag + ")" print(label("Ana")) print(label("Ben", "Admin")) shout = label("Cy").upper() print(shout)

⚠ Watch out

Treating print as if it were return. A function that only prints shows the answer on the screen but gives back None, so total = add(3, 4) leaves total as None. Methods catch people the same way: scores = scores.append(7) leaves scores as None.

🧠

Memory hook

def writes the recipe, the call cooks it, and return serves the dish back to whoever ordered it. print just shouts the answer across the kitchen.

✓

Check yourself

A function does print(word.upper()) and you call answer = shout("hey"). What appears on the screen, what does answer hold, and which one change makes answer hold "HEY"?

Flashcards

(14)
What does the def line do when Python reaches it?
It defines the function and nothing more. The body does not run until the function is called, and the def must be executed before the line that calls it.
In def area(w, h): and the call area(3, 4), which are the parameters and which are the arguments?
w and h are the parameters, the names in the definition. 3 and 4 are the arguments, the values in the call, matched in order.
A function has two parameters and no defaults. What happens if you call it with one argument, or with three?
Python raises a TypeError either way. With too few, a parameter gets no value; with too many, an argument has no parameter to go to.
What are the two jobs of return?
It sends a value back to the place the function was called, and it ends the function at that line.
What does a function with no return statement give back?
None, the special value. Printing inside the function does not change that.
How do you send back more than one value, and what do you get?
Separate the values with commas after return, for example return total, average. They come back together as a tuple.
How do you give a parameter a default value, and why do old calls keep working?
Write it in the def line, for example def greet(name, greeting="Hello"):. If a caller leaves that argument out, the default is used.
Is def f(a=1, b): allowed?
No. Parameters with default values must come after parameters without default values.
What is a keyword argument, and why use one?
An argument passed by name, for example greet("Sam", greeting="Hi"). It makes the purpose of each value clear.
What is a method, and how do you call one?
A function that belongs to an object. You call it with dot notation: object.method(arguments), for example name.upper().
Does word.upper() change word?
No. String methods return a new value and leave the original alone, because strings are immutable.
What do append() and sort() return?
None. They change the list in place, so scores = scores.append(7) leaves scores as None.
In a method written inside a class, what is the first parameter and who fills it?
By convention it is called self. It refers to the object the method is called on, and Python passes it in automatically.
Name the benefits of using functions.
No repeated code; easier to read, test and maintain; and a change made in one place applies everywhere the function is called.

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

See the full Eduqas Computer Science curriculum →

How this lesson was checked. This Eduqas GCSE Computer Science (specification C500QS)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 16 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.