SECTION 15 / 15

Call procedures correctly: name, count and order

2 min read

A procedure call must match its definition. Tutorial 3.3 builds an average for Big Bazaar bills by returning two updated values after each card. The procedure name, number of arguments and argument order all matter.

SumBB and CountBB are returned and assigned in that order. The final Big Bazaar average is₹2,214.
SumBB and CountBB are returned and assigned in that order. The final Big Bazaar average is₹2,214.
A tiny arithmetic example shows why the sum and count arguments cannot be swapped.
A tiny arithmetic example shows why the sum and count arguments cannot be swapped.
Procedure AddIfBigBazaar(X, SumBB, CountBB)
    if (X.Store == "Big Bazaar") {
        SumBB = SumBB + X.BillAmount
        CountBB = CountBB + 1
    }
    return (SumBB, CountBB)
End Procedure

SumBB = 0; CountBB = 0
for each row X in Bills {
    (SumBB, CountBB) = AddIfBigBazaar(X, SumBB, CountBB)
}
if (CountBB > 0) { AverageBB = SumBB / CountBB }
else { report "No matching bills" }
REAL COURSE DATA · FOLLOW THE PROCEDURE

Return an updated sum and count

Read the current record, check the explanation, and watch the variables. Use Back to revisit any step.

Ready

CURRENT RECORD

Ready to begin

No records have been processed. Start with Next step.

What we remember

Processed
0

Start at the first step. Watch the current row and the values we remember.

ErrorWhy it fails
AddingIfBigBazaar(X,SumBB,CountBB)The name differs from the definition AddIfBigBazaar.
AddIfBigBazaar(X,SumBB)A required argument is missing.
AddIfBigBazaar(X,SumBB,CountBB,AverageBB)There is an extra argument.
AddIfBigBazaar(X,CountBB,SumBB)Sum and count occupy the wrong positions.

Imagine the current sum is 100 and count is 2, and a matching bill is₹50. The correct call returns(150,3). Swapping the arguments makes the helper treat 2 as the sum and 100 as the count: it returns(52,101), which is nonsense for our intended task.

If the bill is not from Big Bazaar, return the incoming sum and count unchanged. A local update is not a promise that the caller’s variables changed automatically; the caller explicitly assigns both returned values. The final values for our data are SumBB=13,284 and CountBB=6, so AverageBB=2,214.

Tuple notation (SumBB,CountBB) is used here to show two returned values in a fixed order. It does not mean that sum and count are interchangeable.

Try it yourself

What are the final Big Bazaar sum and count?

Show explained answer

₹13,284 and 6 bills.

Try it yourself

Why is calling AddingIfBigBazaar an error?

Show explained answer

It is not the defined procedure name AddIfBigBazaar.

Try it yourself

Correct incoming values are 100 and 2; bill amount 50. What is returned?

Show explained answer

(150,3), in the order sum then count.

Try it yourself

Must a nonmatching bill reset the counters to zero?

Show explained answer

No. It should return the existing values unchanged.

Try it yourself

What should happen when no Big Bazaar bills exist?

Show explained answer

CountBB is zero, so do not divide. Report that no matching bills exist.

YOUR STUDY PROGRESS

Finished this section? Save your progress.

Back to top ↑