Try it on paper first. A hint, the key idea and the worked solution are here when you need them.
A shop's program works out sale prices in dollars.
FUNCTION SalePrice(Price : REAL, Percent : INTEGER) RETURNS REAL
RETURN Price - Price * Percent / 100
ENDFUNCTION
DECLARE Total : REAL
Total ← SalePrice(40.0, 15) + SalePrice(20.0, 50)
OUTPUT TotalWhat does each call of SalePrice return, and what is output?
A procedure does a job: PROCEDURE Name(Param : TYPE) ... ENDPROCEDURE, run with CALL Name(value). A function returns one value: FUNCTION Name(Param : TYPE) RETURNS TYPE ... RETURN value ... ENDFUNCTION, used inside an expression without CALL. A variable declared inside a subroutine is local; one declared in the main program is global.
Have you tried it?
Work it out on paper first. It is fine to get stuck or make a mistake: that is how we learn. When you are ready, go through the solution one step at a time, and check each step against your own.
Step 1. The first call. Price is 40.0 and Percent is 15. The function returns .
Step 2. The second call. .
Step 3. The output. The two returned values replace the calls: Total is , and the output is 44.0.
Check. Half price of 20 dollars is 10 dollars. Good.
Watch out. Multiplication and division come before subtraction, so Price * Percent / 100 is worked out first.
Log in to join the discussion.
Loading…