Skip to content
S3U
Login Create Account

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 first

Programs follow precise instructions

A program is a set of instructions. A variable stores a value. A condition chooses between paths. A loop repeats steps. Output is the result produced.

How to work it out Trace one instruction at a time. Keep a record of values and check exactly when a condition or repeated step is evaluated.

A separate example In a separate trace, start with n = 2, add 3, then double n. The two instructions happen in order: 5 is an intermediate value before the final result.

Learn the skill

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

Practice setup

The caller runs SET cost TO total(4, 3), followed by SHOW cost. The function total returns price × count. Distinguish returning a value, storing it, and displaying 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.
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?