Writing functions
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.
- Copy the function name and parameters from the question exactly.
- Work out one example by hand, such as ticket_price(16), so you know what the answer should be.
- Write the body: selection for choices, a loop for anything repeated.
- Return the answer once it is worked out.
- 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.
| Job | OCR style | AQA style | Python |
|---|---|---|---|
| Assign | x = 0 | x ← 0 | x = 0 |
| Equal to | == | = | == |
| Whole-number division | DIV | DIV | // |
| Remainder | MOD | MOD | % |
| Count-controlled loop | for i = 1 to n ... next i | FOR i ← 1 TO n ... ENDFOR | for i in range(1, n + 1): |
| Subroutine | function f(x) ... endfunction | SUBROUTINE f(x) ... ENDSUBROUTINE | def 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.