x

Revision notes for Edexcel GCSE Computer Science Functions versus procedures. Open the guide for explanations and worked examples. Written against the Edexcel GCSE Computer Science (1CP2) specification, so the content matches what's examinable rather than general Computer Science background.

Functions versus procedures

What you'll learn

  • What a subprogram is, and why programmers split code into smaller parts.
  • The difference between a function and a procedure.
  • How to write Python 3 functions that return values.
  • How to write Python 3 procedures that do not return values.

Why subprograms matter

A subprogram is a named block of code that performs a smaller task inside a larger program.

Instead of writing one long program from top to bottom, programmers split it into subprograms. This makes code easier to:

  • read
  • test
  • reuse
  • fix
  • update

For example, a quiz program might have separate subprograms to:

  • display the menu
  • ask a question
  • calculate the score
  • show the final result
Definition

Subprogram

A subprogram is a named section of code that can be run when it is called by another part of the program.

Calling a subprogram

To call a subprogram means to run it by writing its name, followed by brackets.

In Python:

display_menu()

The brackets are still needed even if no data is being passed in.

Definition

Call

A call is an instruction that tells the program to run a particular subprogram.

Parameters and arguments

Subprograms can be given data to work with.

A parameter is a variable listed in the subprogram definition. It acts like a placeholder for data that will be supplied later.

An argument is the actual value passed into the subprogram when it is called.

def greet_user(name):      # name is a parameter
    print("Hello", name)

greet_user("Amina")        # "Amina" is an argument
Definition

Parameter and argument

A parameter is a variable used in a subprogram definition. An argument is the actual value passed into the subprogram when it is called.

Common Mistake

Mixing up parameter and argument

Students often use these words as if they are the same. In exams, be precise: the parameter is in the subprogram definition; the argument is in the call.

The key difference: functions return, procedures do not

The main difference is about whether the subprogram sends a value back to the part of the program that called it.

A function must return a value.

A procedure does not return a value.

Diagram comparing a function, which returns a value, with a procedure, which does not return a value

Key Idea

Function versus procedure

Use a function when the subprogram needs to produce a result that will be used later. Use a procedure when the subprogram just performs an action, such as printing a message.

Functions

A function is a subprogram that returns a value to the code that called it.

In Python, a function is written using def, just like other subprograms, but it uses return to send a value back.

Definition

Function

A function is a subprogram that may or may not take parameters, but must return a value.

Function with no parameters

A function does not have to receive data.

def get_welcome_message():
    return "Welcome to the quiz!"

message = get_welcome_message()
print(message)

Here:

  • get_welcome_message has no parameters
  • it returns the string "Welcome to the quiz!"
  • the returned value is stored in message

Function with parameters

A function can also take parameters.

def calculate_total_score(score1, score2):
    total = score1 + score2
    return total

final_score = calculate_total_score(8, 7)
print(final_score)

Here:

  • score1 and score2 are parameters
  • 8 and 7 are arguments
  • the function returns the total
  • the returned value is assigned to final_score
Example

Writing a function with parameters

Write a function that takes a length and width, calculates the area of a rectangle, and returns the result.

  1. Decide what data the function needs to receive. It needs length and width, so these become parameters.

  2. Decide what value the function must send back. The result is the area, so the function must return area.

  3. Write the function using return, because the calculated value needs to be used by the calling code.

def calculate_rectangle_area(length, width):
    area = length * width
    return area
  1. Call the function and store the returned value in a variable.
rectangle_area = calculate_rectangle_area(6, 4)
print(rectangle_area)

The return statement

The return statement sends a value back from a function to the line of code that called it.

For example:

def double_number(number):
    return number * 2

answer = double_number(5)

The function call double_number(5) is replaced by the returned value, so answer stores 10.

Tip

How to spot a function

If the subprogram call is used in an assignment, calculation, comparison, or output statement, it is probably a function, because a value is being returned and used.

Common Mistake

Printing instead of returning

If a question asks you to write a function, do not just use print. A function must use return to send a value back.

Procedures

A procedure is a subprogram that performs a task but does not return a value.

In Python, procedures are also written using def, but they do not return a useful value.

Definition

Procedure

A procedure is a subprogram that may or may not take parameters, but does not return a value.

Procedure with no parameters

A procedure can perform an action without receiving any data.

def display_menu():
    print("1. Start quiz")
    print("2. View scores")
    print("3. Quit")

display_menu()

This procedure displays text on the screen. It does not calculate and return a value.

Procedure with parameters

A procedure can take parameters if it needs data to carry out its action.

def display_score(name, score):
    print(name, "scored", score)

display_score("Amina", 15)

Here, the procedure receives name and score, then prints a message.

Example

Writing a procedure with parameters

Write a procedure that takes a username and displays a welcome message.

  1. Decide whether a returned value is needed. The task is only to display a message, so this should be a procedure.

  2. Decide what data the procedure needs. It needs the username, so username becomes a parameter.

  3. Write the procedure so it performs the action using print, with no returned value.

def display_welcome(username):
    print("Welcome,", username)
  1. Call the procedure on its own line, because there is no returned value to store.
display_welcome("Sam")
Common Mistake

Python and None

In Python, a subprogram with no return statement technically returns None. For GCSE, treat this as a procedure because it does not return a useful value for the program to use.

Comparing functions and procedures

FeatureFunctionProcedure
Takes parameters?May or may notMay or may not
Returns a value?Yes, must return a valueNo
Common purposeCalculate or produce a resultPerform an action
Python keyword used to define itdefdef
Usually called how?In an assignment or expressionOn its own line

Choosing between a function and a procedure

When deciding which one to write, ask:

  • Does the subprogram need to give a value back?
  • Will the calling code need to store, compare, print, or calculate with the result?
  • Or does the subprogram simply perform an action?

Use a function for a result.

Use a procedure for an action.

Example

Choosing the correct subprogram

A program needs one subprogram to calculate the average score, and another subprogram to display a certificate.

  1. The average score is a value that the program may need to store or compare later, so this should be a function.
def calculate_average(score1, score2):
    average = (score1 + score2) / 2
    return average
  1. The certificate is just displayed to the user, so this should be a procedure.
def display_certificate(name):
    print("Certificate awarded to", name)
  1. The main program can use the returned average from the function, then call the procedure to display the certificate.
average_score = calculate_average(10, 8)
print("Average score:", average_score)
display_certificate("Amina")

Using functions and procedures together

Real programs often use both.

A function might calculate a value, then a procedure might display it.

def calculate_discounted_price(price, discount):
    discounted_price = price - discount
    return discounted_price

def display_price(price):
    print("Final price:", price)

final_price = calculate_discounted_price(20, 5)
display_price(final_price)

In this example:

  • calculate_discounted_price is a function because it returns a value
  • display_price is a procedure because it only prints a value
  • final_price stores the returned value from the function
Example

Tracing a returned value

Trace this code:

def add_bonus(score):
    new_score = score + 5
    return new_score

def display_result(result):
    print("Result:", result)

player_score = add_bonus(12)
display_result(player_score)
  1. The call add_bonus(12) passes the argument 12 into the parameter score.

  2. Inside the function, new_score is calculated as 12 + 5, giving 17.

  3. The function returns 17, so player_score is assigned the value 17.

  4. The procedure display_result(player_score) receives 17 and prints Result: 17, but it does not return a value.

Writing clear subprograms

Use meaningful names so the purpose of each subprogram is obvious.

Good function names often describe the value being calculated:

calculate_total()
get_username()
find_largest_number()

Good procedure names often describe the action being performed:

display_menu()
print_receipt()
save_score()

Also remember that Python uses indentation to show which lines belong inside the subprogram.

def display_message():
    print("This line is inside the procedure")

print("This line is outside the procedure")
Tip

Name clue

Function names often sound like they are getting, calculating, or finding something. Procedure names often sound like they are displaying, printing, or updating something.

Exam technique

In the exam

  1. If asked to write a function, include a return statement and make sure the returned value is used correctly.

  2. If asked to write a procedure, do not return a value; call it on its own line to perform its action.

  3. Check whether parameters are required: if the subprogram needs data from outside, include parameters in the brackets.

Self review

Check yourself

  • What is the difference between a parameter and an argument?
  • Why would calculate_total(score1, score2) probably be a function?
  • Write one example of a procedure that takes no parameters.
You've reached the end

Test yourself on this topic, or move on to the next guide.

FlashcardsSelf-test with active recall
Global and local variablesUp next

How was this guide?

Functions versus procedures Revision Guide

  1. GCSE
  2. /Computer Science
  3. /Functions versus procedures