S3U

Science / Programming basics / Grade 11 / scb70

Programming Basics: Separate Calculation and Display

Split responsibilities into small functions with clear contracts.

All worksheets
5 questions0m 0s
Learn the skill

Define FUNCTION total(price, count): RETURN price * count; END for nonnegative integer inputs. Prices are whole fictional coin units, not real payments. Keep calculation separate from SHOW. Each call returns its own result and changes no outside state.

Worked example

SET cost TO total(4, 3); SHOW cost displays 12. The function can be tested without a screen. A separate caller decides whether, where or how to display the returned value.

The total function multiplies a nonnegative integer price by a nonnegative integer count. Inputs four and three return twelve. The caller stores that return value in cost and SHOW cost displays it.
The total function multiplies a nonnegative integer price by a nonnegative integer count. Inputs four and three return twelve. The caller stores that return value in cost and SHOW cost displays it.
Question 1 What does total(4, 3) return?
Question 2 What does total(4, 0) return?
Question 3 Which function behavior follows the stated contract?
Question 4 What displays the calculated cost in the example?
Question 5 Why keep calculation separate from display?