OCR GCSE Computer Science June 2022 Paper 2, Question 5(c): writing and calling a function

OCR J277/02 June 2022, Question 5(c): create a function newPrice that takes nights and room type and returns the price, then write the code that calls it for a premium room for 5 nights. Defining against calling, the marks explained, and the function to run.

Past paper questionOCR J277/02June 2022 Paper 27 marksWrite a program

Question 5(c) of the OCR GCSE Computer Science Paper 2 sat on 27 May 2022 (J277/02) splits one skill into two halves. Part (i): define a function (4 marks). Part (ii): call it and output what it returns (3 marks). Mixing the two up is an easy way to lose marks.

We do not copy the exam paper here. Open it beside this page: OCR June 2022 J277/02 question paper (PDF). When you have finished, check the mark scheme too.

The question in short

A basic room costs £60 a night. A premium room costs £80 a night. Create a function, newPrice(), that takes the number of nights and the type of room as parameters, and calculates and returns the price. You do not have to validate the parameters.

Then write code that uses newPrice() to output the price of a premium room for 5 nights.

Part (i): define it

In OCR Exam Reference Language:

function newPrice(nights, room)
    if room == "basic" then
        price = 60 * nights
    else
        price = 80 * nights
    endif
    return price
endfunction

In Python:

def newPrice(nights, room):
    if room == "basic":
        price = 60 * nights
    else:
        price = 80 * nights
    return price

The four marks: a function header for newPrice; two parameters; the right calculation from those parameters; returning the price.

Part (ii): call it

print(newPrice(5, "premium"))

Or in two steps: price = newPrice(5, "premium") and then print(price).

The three marks: calling newPrice; passing "premium" and 5; outputting the value that comes back. The arguments must be in the same order as your parameters.

Where the marks are lost

  • print(price) inside the function. The question says returns. A function that prints gives nothing back to the code that called it.
  • input() inside the function. The values arrive as parameters. Asking for them again overwrites them.
  • premium with no quotation marks in the call. It is a string. Without quotes it is a variable that does not exist.
  • Defining the function again in part (ii). Part (ii) is one line. A second definition earns nothing there.
  • Calling without using the result. newPrice(5, "premium") on its own works the price out and throws it away.

Run it

The function, the call from part (ii), and a bill for whatever stay you type in.

The first line it prints is 400: five nights at 80. Then try 3 nights in a basic room for 180.
The program
from bugbot import *
connect()

def newPrice(nights, room):
    if room == "basic":
        price = 60 * nights
    else:
        price = 80 * nights
    return price

print(newPrice(5, "premium"))

nights = int(input("How many nights? "))
room = input("basic or premium? ")
print("That will be", newPrice(nights, room), "pounds")
Put this demo on your own site

Paste it into a school website, Moodle, Google Sites or a blog. More options on the embed page.

Questions

What is the answer to OCR J277 June 2022 Paper 2 Question 5(c)?

Part (i): function newPrice(nights, room), with an if that sets price to 60 * nights for a basic room and 80 * nights otherwise, then return price. Part (ii): print(newPrice(5, "premium")).

What is the difference between defining and calling a function?

Defining a function is writing its header and body, which says what it does. Calling it is using its name with arguments in brackets, which makes it run. A function is defined once and can be called many times.

What is the difference between return and print?

return sends a value back to the code that called the function, where it can be stored or used. print only displays a value on the screen.

More from this paper

Every OCR J277 question we have worked

Learn it step by step

  1. F4.1 Writing functions Functions and structured code
  2. F4.2 Parameters and return values Functions and structured code
Open the lessons

This is our own explanation of a published exam question. It is not written or endorsed by OCR, and the question paper and mark scheme remain OCR's copyright. Read them on OCR's site with the links on this page.