Design subroutines and parameter contracts
Choose a procedure or function
A function returns a value used in an expression. A procedure performs an action, such as changing stock or printing a report. Specify parameter types and the promised result before implementation.

Worked example: a returned calculation
FUNCTION Double(Value : INTEGER) RETURNS INTEGER
RETURN Value * 2
ENDFUNCTION
OUTPUT Double(6)
The caller chooses what to do with 12. OUTPUT inside the function would print a value but would not return it. BYVAL supplies a value; BYREF allows a procedure to change the caller's variable.
Common mistakes
OUTPUTinside a function instead ofRETURN. The value is printed, but the caller receives nothing to use.- Passing
BYVALwhen the procedure must change the caller's variable. It changes only a copy:
PROCEDURE AddByValue(BYVAL Count : INTEGER)
Count ← Count + 1
ENDPROCEDURE
PROCEDURE AddByReference(BYREF Count : INTEGER)
Count ← Count + 1
ENDPROCEDURE
DECLARE Stock : INTEGER
Stock ← 5
CALL AddByValue(Stock)
OUTPUT Stock
CALL AddByReference(Stock)
OUTPUT Stock
The first OUTPUT gives 5; the second gives 6.
CALLwith a function, or a procedure inside an expression. A function call is a value; a procedure call is a statement.- A missing
RETURNStype in the header. The type is part of the interface the question asks for.
Review the interfaces
For each task, say what changes in the caller and what is returned or printed. The checker supplies calls, so write only the requested subroutine. Test both a successful operation and a refused operation; refusal must preserve state.
ความยาก 3/5 · 200 เหรียญ + บอนัสน้อยๆ เมื่อทำสำเร็จครั้งแรกในวันนี้
Write only FUNCTION Charge(Hours : INTEGER) RETURNS INTEGER. For 1 or 2 hours return 3; each hour above 2 adds 2. Hours is positive.
คลิก Run เพื่อดูผลลัพธ์ที่นี่
ความยาก 3/5 · 200 เหรียญ + บอนัสน้อยๆ เมื่อทำสำเร็จครั้งแรกในวันนี้
Write only PROCEDURE Supply(BYREF Stock : INTEGER, BYVAL Wanted : INTEGER). If Wanted is positive and fits, decrease Stock and output supplied; otherwise output refused.
คลิก Run เพื่อดูผลลัพธ์ที่นี่
ความยาก 3/5 · 200 เหรียญ + บอนัสน้อยๆ เมื่อทำสำเร็จครั้งแรกในวันนี้
P starts at 10. PROCEDURE Add(BYVAL X : INTEGER) sets X ← X + 4. After CALL Add(P), what is P? Enter the number.
Explain your choice of data structure and loop. Identify one boundary case and predict its result. For a subroutine, explain what is returned or changed in the caller.
บันทึกสำหรับรอบนี้บนเบราว์เซอร์นี้ ทบทวนด้วยตนเองหรือร่วมกับครู; คำอธิบายเหล่านี้ไม่ถูกให้คะแนนอัตโนมัติ
ทบทวนคำอธิบายของคุณตามเกณฑ์เหล่านี้
- My types and data structure match the data and the question.
- I justified the loop and checked its first and last iterations.
- Initial values and rejected operations preserve the intended state.
- My test includes input, purpose and expected result.
- I distinguished returning a value from printing and changing caller state.