Unit 3, Lesson 1 of 5

Writing functions

About 3 min to read, then 9 practice questions

How these questions are marked

In this unit you write real Python in the editor, and it runs against tests. The tests are shown with the question: they are the task, so read them before you start.

  • A call test runs one line such as area(3, 4) and checks what your function returns.
  • An input test types some lines into your program and checks exactly what it prints.
  • Your mark is your share of the tests passed: pass 3 of 4 tests on a 4-mark question and you score 3.

Use the exact function name and parameters in the question. A function called Area will not be found by a test that calls area.

On a paper the examiner reads your code against a mark scheme; here the tests do the same job, and the scheme under each question shows what the marks are for.

Return, not print

A function hands its answer back with return. The code that called it decides what to do with the value: print it, store it or use it in another calculation.

A function with selection

function ticket_price(age)
    if age < 16 then
        return 5
    elseif age >= 65 then
        return 6
    else
        return 10
    endif
endfunction
print(ticket_price(15) + ticket_price(40))
SUBROUTINE ticket_price(age)
    IF age < 16 THEN
        RETURN 5
    ELSE IF age ≥ 65 THEN
        RETURN 6
    ELSE
        RETURN 10
    ENDIF
ENDSUBROUTINE
OUTPUT ticket_price(15) + ticket_price(40)
def ticket_price(age):
    if age < 16:
        return 5
    elif age >= 65:
        return 6
    else:
        return 10
print(ticket_price(15) + ticket_price(40))

This prints 15. A function that prints its answer instead of returning it gives back nothing at all, so ticket_price(15) + ticket_price(40) would fail.

  1. Copy the function name and parameters from the question exactly.
  2. Work out one example by hand, such as ticket_price(16), so you know what the answer should be.
  3. Write the body: selection for choices, a loop for anything repeated.
  4. Return the answer once it is worked out.
  5. Run the tests. Read the name of any test that fails: it tells you which case to look at.

Loops inside functions

A running total starts before the loop, changes inside it and is returned after it.

Add the whole numbers from 1 to n

function sum_to(n)
    total = 0
    for i = 1 to n
        total = total + i
    next i
    return total
endfunction
print(sum_to(4))
SUBROUTINE sum_to(n)
    total ← 0
    FOR i ← 1 TO n
        total ← total + i
    ENDFOR
    RETURN total
ENDSUBROUTINE
OUTPUT sum_to(4)
def sum_to(n):
    total = 0
    for i in range(1, n + 1):
        total = total + i
    return total
print(sum_to(4))

This prints 10. In Python a FOR loop from 1 to n, including n, is for i in range(1, n + 1): range stops one before its second number.

From pseudocode to Python

Exam questions often give an algorithm in OCR Exam Reference Language or AQA pseudocode and ask for it in a programming language. Translate it one line at a time.

JobOCR styleAQA stylePython
Assignx = 0x ← 0x = 0
Equal to=====
Whole-number divisionDIVDIV//
RemainderMODMOD%
Count-controlled loopfor i = 1 to n ... next iFOR i ← 1 TO n ... ENDFORfor i in range(1, n + 1):
Subroutinefunction f(x) ... endfunctionSUBROUTINE f(x) ... ENDSUBROUTINEdef f(x):

In Python the indentation is the end of a block: there is no endif or endwhile, so every line inside a loop or an if must be indented.

Practise: Functions and Parameters and scope on Brainlag Code.

Practise this: 9 questions, about 17 min

Answering from memory soon after reading is what makes it stick, so now is a good time.

  • XP for every question right first time
  • This lesson fills in on your course map
  • Your daily streak, kept going
Start practising