Functions
24 Grade 10 Computer Studies questions on Functions, with worked marking schemes. 24 questions are available on this topic in full.
Question 1
1 markIn a program that works out water bills for an estate in Nakuru, two lines appear. Line 1 reads FUNCTION waterBill(units) and line 2 reads amountDue = waterBill(35). Which statement about these two lines is correct?
A. Line 2 defines the function and line 1 supplies the value it returns.
B. Line 1 defines the function and line 2 calls it.
C. Both lines call the function, so its statements run twice.
D. Line 1 calls the function and line 2 defines it.
(1 mark)
Marking scheme for this question
Create a free account to see itQuestion 2
3 marksA programmer wishes to use a built-in function from the language library without seeing the code inside it. Describe three things the programmer must know in order to use it correctly.
(3 marks)
Marking scheme
1 mark each for any THREE: 1. The exact name of the function, spelt as the language requires, since names are often case-sensitive. 2. The number of arguments it expects and the order in which they must be written. 3. The type of each argument, for example a number rather than a piece of text. 4. The value it returns, or the action it performs, so that the result can be assigned, displayed or used in an expression. 5. Whether the library holding it has to be imported or included before the call is made.
Question 3
1 markA function named displayReceipt prints a receipt for a customer at a hardware shop in Machakos, but it does not send any value back to the statement that called it. Such a function is best described as one that:
A. has no parameters at all
B. can only be used by the main program
C. can only be called once in a program
D. performs an action but returns no value
(1 mark)
Marking scheme for this question
Create a free account to see itQuestion 4
1 markA program uses a loop that runs once for each of the 40 learners in a class, and inside the loop it calls a function markGrade(score). How many times are the statements inside markGrade executed?
A. Forty-one times, because the loop counter is also passed each time.
B. Once for the whole loop and once more when the loop ends.
C. Forty times, once for each learner.
D. Once, because a function is stored in memory only once.
(1 mark)
Marking scheme for this question
Create a free account to see itQuestion 5
1 markInside a function that works out the total litres of milk delivered to a dairy co-operative in Bomet, the statement RETURN total is reached. What happens immediately?
A. The value held in total is saved automatically in a file.
B. The function begins again from its first line.
C. The function stops and sends the value held in total back to the statement that called it.
D. The value held in total is printed on the screen.
(1 mark)
Marking scheme for this question
Create a free account to see itQuestion 6
1 markA variable named counter is created inside a function that counts bags of maize as they are weighed. After the function has finished, the main program tries to display counter. What is the most likely outcome?
A. The value zero is displayed, because all variables reset to zero.
B. The last value of counter is displayed, because functions share all variables.
C. An error occurs, because counter exists only while the function is running.
D. The name of the function is displayed instead of a number.
(1 mark)
Marking scheme for this question
Create a free account to see itQuestion 7
4 marksA learner writes a function to work out the perimeter of a rectangular fish pond, but when the program is run nothing is printed. Suggest four possible causes of this.
(4 marks)
Marking scheme for this question
Create a free account to see itQuestion 8
1 markWhich of the following is the most suitable name for a user-defined function that works out the mean rainfall recorded at a weather station in Nyeri?
A. average-rainfall
B. averageRainfall
C. 2ndAverageRainfall
D. average rainfall
(1 mark)
Marking scheme for this question
Create a free account to see itQuestion 9
1 markA main program displays a welcome message, then calls a function that works out a boda boda rider's earnings for the day, and then displays those earnings. When the function has finished running, where does execution continue?
A. At the first line of the main program.
B. At the first line of the function.
C. At the last line of the main program.
D. At the statement immediately after the one that called the function.
(1 mark)
Marking scheme for this question
Create a free account to see itQuestion 10
3 marksDistinguish between a parameter and an argument as used in functions, and illustrate your answer with one example.
(3 marks)
Marking scheme for this question
Create a free account to see itQuestion 11
2 marksState two items of information that must appear in the header (first line) of a user-defined function.
(2 marks)
Marking scheme for this question
Create a free account to see itQuestion 12
1 markA learner writes a function that works out the area of a rectangular school garden plot, but forgets to call it anywhere in the main program. When the program is run, what happens to the statements inside that function?
A. They run first, before any statement of the main program.
B. They run once automatically after the last line of the main program.
C. They are never executed.
D. They run again and again until the program is stopped.
(1 mark)
Marking scheme for this question
Create a free account to see itQuestion 13
5 marksDescribe, in order, what happens when a main program reaches a statement that calls a value-returning function.
(5 marks)
Marking scheme
1. Execution of the main program pauses at the call statement. (1) 2. The arguments in the call are copied into the function's parameters, matched in the order in which they are written. (1) 3. Control transfers to the first statement in the body of the function. (1) 4. The statements in the body are executed until a RETURN statement or the end of the function is reached. (1) 5. The returned value is passed back to the call, and execution of the main program resumes at the statement immediately after the call. (1)
Question 14
3 marksState three built-in functions commonly provided by programming languages and describe what each one does.
(3 marks)
Marking scheme for this question
Create a free account to see itQuestion 15
1 markWhich one of the following routines would a learner normally NOT need to write, because it is already supplied in the programming language's library?
A. A routine that decides whether a learner has passed the end-of-term test.
B. A routine that works out a coffee farmer's payment from the kilogrammes delivered.
C. A routine that returns the number of characters in a piece of text.
D. A routine that prints the school's Grade 10 timetable.
(1 mark)
Marking scheme for this question
Create a free account to see itQuestion 16
5 marksA boarding school in Kitui wants to work out the cost of feeding its boarders for one day. Describe, in words, the pseudocode of a user-defined function named mealCost that receives the number of boarders and the cost of one meal per learner and returns the total cost, and show how the main program would use it.
(5 marks)
Marking scheme
1. Write a function header naming the function mealCost and listing two parameters, for example numberOfBoarders and costPerMeal. (1) 2. Inside the function, work out a total by multiplying numberOfBoarders by costPerMeal and store it in a variable such as totalCost. (1) 3. Use a RETURN statement to send totalCost back to the caller. (1) 4. Close the function with an end-of-function marker such as END FUNCTION. (1) 5. In the main program, call the function with actual values, for example dayCost is assigned mealCost(450, 60), then display dayCost. (1)
Question 17
3 marksDistinguish between a function that returns a value and a function that only performs an action, giving one example of each from a shop program.
(3 marks)
Marking scheme
1. A value-returning function sends a result back to the calling statement using RETURN, for example a function that receives the kilogrammes of sugar sold and returns the amount payable. (1) 2. An action function performs a task and sends no value back, for example a function that prints a customer's receipt on the screen. (1) 3. The call of a value-returning function can be used inside an assignment or expression, whereas the call of an action function stands alone as a complete statement. (1)
Question 18
2 marksExplain what is meant by calling a function.
(2 marks)
Marking scheme for this question
Create a free account to see itQuestion 19
2 marksState two rules that must be observed when naming a user-defined function.
(2 marks)
Marking scheme for this question
Create a free account to see itQuestion 20
6 marksSuggest three ways in which the use of functions makes a large school management program easier to test and to maintain. Explain each.
(6 marks)
Marking scheme
2 marks each for any THREE (1 for the point, 1 for the explanation): 1. Testing in isolation - each function can be run on its own with sample values, so a fault is traced to one small block instead of being hunted through the whole program. 2. Single point of change - a rule such as the fee rate or the pass mark lives inside one function, so an update is made once and every part that calls it uses the new rule. 3. Division of work - different programmers can write and test different functions at the same time, since each only needs to know what a function receives and what it returns. 4. Readability - meaningful function names make the main program read like a list of tasks, so a new programmer understands it quickly.
Question 21
1 markA function is defined to receive three parameters: the name of the county, the number of beehives and the kilogrammes of honey per hive. None of the parameters has a default value. A learner calls the function supplying only the county name and the number of hives. What is the most likely result?
A. An error occurs, because the number of arguments does not match the number of parameters.
B. The function runs normally but returns the wrong county name.
C. The function uses zero for the missing value automatically.
D. The function prompts the user to type the missing value.
(1 mark)
Marking scheme for this question
Create a free account to see itQuestion 22
4 marksExplain the difference between a local variable and a global variable, and state two effects this difference has on a program.
(4 marks)
Marking scheme
1. A local variable is created inside a function and exists only while that function is running; its storage is released when the function ends. (1) 2. A global variable is declared in the main program and can be read or changed by any part of the program, including its functions. (1) 3. Effect: two different functions may each use a local variable with the same name without interfering with each other. (1) 4. Effect: a global variable altered inside one function affects every other part of the program, so faults caused by it are harder to trace. (1)
Question 23
1 markA Grade 10 learner writes a program that converts the kilogrammes of green tea leaf delivered by a farmer into the payment due at a buying centre in Kericho. The same conversion is needed in five different places in the program. What is the main reason for placing the conversion inside a function?
A. It makes the computer's processor run at a faster speed.
B. It removes the need to test the conversion before use.
C. It stores the farmers' weights permanently on the hard disk.
D. The conversion is written once and called wherever it is needed.
(1 mark)
Marking scheme for this question
Create a free account to see itQuestion 24
1 markA function is written with the header FUNCTION fareTotal(trips). Later in the same program the statement fareTotal(14) appears. In this program, the value 14 is best described as:
A. a global variable of the program
B. a parameter of the function
C. the return value of the function
D. an argument passed to the function
(1 mark)
Marking scheme for this question
Create a free account to see it18 more marking schemes are on this page. A free account opens all of them, and lets you build your own paper on the topics you have actually taught, laid out in KNEC format with its full marking scheme.
Create a free account