Ready-made helpers
Answers are for parents and teachers.
1
Write a function twelve() that returns 12 and a line that prints twelve() + 5. What is printed?
Model answer (lines are separated by /, and [indent] means four spaces at the start of the line): def twelve(): / [indent] return 12 / print(twelve() + 5). Note: It prints 17.
2
What does this program print? Replace each call with its returned value as you trace it.
Python
1def plus_ten(n): 2 return n + 10 3 4print(plus_ten(3) + plus_ten(2))
25. plus_ten(3) is replaced by 13 and plus_ten(2) by 12, so the sum is 13 + 12 = 25.
3
Say whether each is a procedure or a function: (a) a sub-routine that prints a menu; (b) a sub-routine that returns the price with delivery added.
(a) procedure (b) function
4
Give one benefit and one risk of using a library, and one thing you would do about the risk.
Benefit: saves time because the code is written and tested. Risk: code from an unknown source could be wrong or harmful. Ask a trusted adult and use trusted sources.
5
Explain why a sub-routine helps when two programmers work together on one big program.
Each programmer can write and test a different sub-routine, and the sub-routines are put together afterwards. A sub-routine is also written once and used in many places.