Back to the topic

The mark scheme starts on a new page, so you can print pages one and two for a student and keep the last page yourself. Everything prints in black on white whichever theme you are using.

Python · 2.4

Functions and Structure

Section A: 10 multiple choice questions, 10 marks. Section B: 5 written questions, 19 marks. Total 29 marks.

Name

Class

Date

Mark  / 29

Section A

Circle the letter of the correct answer. One mark each.

  1. 1What keyword defines a function in Python?1
    1. Afunction
    2. Bdef
    3. Cfunc
    4. Ddefine
  2. 2What is the difference between a parameter and an argument?1
    1. AThey are the same
    2. BParameters only work with numbers
    3. CA parameter is in the definition, an argument is the value passed in
    4. DAn argument is in the definition
  3. 3What does a function return if it has no return statement?1
    1. ANone
    2. BAn error
    3. CAn empty string
    4. D0
  4. 4Why should a calculation function return rather than print?1
    1. Areturn uses less memory
    2. BPython requires it
    3. Cprint is slower
    4. DSo the value can be stored, reused or used in further calculations
  5. 5What is a local variable?1
    1. AOne that never changes
    2. BOne available everywhere
    3. COne that exists only inside the function where it was created
    4. DOne stored in a file
  6. 6In def greet(name, greeting="Hello"), what is greeting?1
    1. AA global variable
    2. BAn error
    3. CA return value
    4. DA parameter with a default value
  7. 7What happens if you access a local variable outside its function?1
    1. AIt returns 0
    2. BA NameError, because it does not exist there
    3. CIt returns None
    4. DNothing happens
  8. 8Why are globals usually avoided?1
    1. AAny part of the program can change them, which causes hard to find bugs
    2. BThey cannot hold numbers
    3. CThey are slower
    4. DPython does not support them
  9. 9What does return min(x), max(x) do?1
    1. APrints both values
    2. BCauses an error
    3. CReturns both values together, which can be unpacked into two variables
    4. DReturns only the minimum
  10. 10What is a docstring?1
    1. AA comment starting with a hash
    2. BA string at the top of a function describing what it does
    3. CA type of error
    4. DA variable name

Section B

Answer in the space provided. The number of marks tells you how many separate points to make.

  1. 1ExplainExplain the difference between print and return in a function.3
  2. 2WriteWrite a function called convert_to_celsius that takes a temperature in Fahrenheit as a parameter and returns the equivalent in Celsius. The formula is (F - 32) multiplied by 5 divided by 9.4
  3. 3ExplainExplain what is meant by the scope of a variable and why local variables are usually preferred.4
  4. 4ExplainExplain two benefits of splitting a program into several functions.4
  5. 5WriteWrite a function that takes a list of numbers and returns the mean, and explain why it should return rather than print the result.4

Mark scheme

Section A

1B2C3A4D5C6D7B8A9C10B

Section B

1 Explain the difference between print and return in a function. 3

One mark for each of the following, to a maximum of 3.

  • print displays a value to the user
  • return sends a value back to the code that called the function
  • A returned value can be stored or used again, a printed value cannot

Model answer. The print function displays a value on the screen for the user to read, and once it has been displayed the program has no further access to it. The return statement sends a value back to whatever code called the function, so it can be stored in a variable, passed into another function or used in a further calculation. A function that calculates something should almost always return it, because a function that only prints has effectively thrown the result away and cannot be used as a building block anywhere else in the program.

2 Write a function called convert_to_celsius that takes a temperature in Fahrenheit as a parameter and returns the equivalent in Celsius. The formula is (F - 32) multiplied by 5 divided by 9. 4

One mark for each of the following, to a maximum of 4.

  • Uses def with a suitable name and a parameter
  • Applies the subtraction of 32 first
  • Multiplies by 5 and divides by 9
  • Returns the result rather than printing it

Model answer. def convert_to_celsius(fahrenheit): return (fahrenheit - 32) * 5 / 9 print(convert_to_celsius(212)) # 100.0 The brackets around the subtraction are essential, because without them Python would multiply by 5 before subtracting 32 and give the wrong answer. The function returns the value rather than printing it, so the result can be stored, rounded, or used in a further calculation by whatever called it.

3 Explain what is meant by the scope of a variable and why local variables are usually preferred. 4

One mark for each of the following, to a maximum of 4.

  • Scope is the region of the program where a variable can be accessed
  • A local variable exists only inside its function
  • A global variable can be accessed from anywhere
  • Local variables cannot be changed accidentally elsewhere, so bugs are easier to avoid

Model answer. The scope of a variable is the part of the program in which that variable can be accessed. A local variable is created inside a function and exists only while that function is running, so nothing outside can see or alter it. A global variable is created outside all functions and can be read and modified from anywhere in the program. Local variables are usually preferred because their limited scope means no other part of the program can change them by accident, which removes a very common and difficult to trace source of bugs. It also means two different functions can safely use the same variable name without interfering, and it makes each function far easier to test, because its behaviour depends only on the arguments passed into it rather than on the state of the rest of the program.

4 Explain two benefits of splitting a program into several functions. 4

One mark for each of the following, to a maximum of 4.

  • Code that is needed more than once is written only once
  • A change only needs to be made in one place
  • Each function can be tested on its own
  • The program becomes easier to read and understand

Model answer. The first benefit is the removal of duplication. Code that is needed in several places can be written once as a function and called wherever it is required, so the program is shorter, and crucially any correction or improvement only has to be made in one place rather than being repeated in every copy, which removes the risk of leaving an old version behind. The second benefit is that a program built from small functions is far easier to develop, test and read. Each function does one clearly defined job and can be tested independently with a range of inputs until it is known to be correct, and the main program then reads as a sequence of well named steps that describe what the program does without the reader having to follow every line of detail.

5 Write a function that takes a list of numbers and returns the mean, and explain why it should return rather than print the result. 4

One mark for each of the following, to a maximum of 4.

  • Defines a function with a list parameter
  • Calculates the total and divides by the number of items
  • Uses return
  • Explains that returning allows the value to be reused elsewhere

Model answer. def mean(numbers): return sum(numbers) / len(numbers) average = mean([4, 8, 15, 16, 23]) print(f"The mean is {average:.2f}") The function should return the value rather than printing it because returning keeps the function general and reusable. The calling code can then decide what to do with the answer: display it, round it, compare it with another average, store it in a file or use it in a further calculation. A function that printed the mean directly would be locked to one use and could not be built on, and it would also be far harder to test automatically, because a test can compare a returned value against an expected one but cannot easily check what was printed.