Programming
Writing real, structured programs: procedures, functions, parameters and scope.
RUN THE PSEUDOCODE
Write the pseudocode your syllabus uses and press Run. Assignment is the arrow — type <- and it works the same as ←. Both syllabuses are accepted, so OUTPUT and PRINT both work; where a word differs from the one the exam prints, the runner says so.
Press Run to see what it doesTotal ← 0
FOR Count ← 1 TO N
Total ← Total + Count
NEXT Count
OUTPUT TotalSTARTN = 5
| Count | Total | OUTPUT |
|---|---|---|
One row per pass through the loop. Leave a cell blank if it does not change on that pass.
//By value or by reference
BYVALUE passes a COPY, so changes inside the procedure do not affect the caller's variable. BYREF passes the original, so changes DO affect it. Use BYREF when the procedure must give something back through a parameter.
PROCEDURE Double(BYVALUE n : INTEGER)
n ← n * 2 // caller's variable unchanged
ENDPROCEDURE
PROCEDURE Double(BYREF n : INTEGER)
n ← n * 2 // caller's variable IS changed
ENDPROCEDURE//Procedure or function?
A FUNCTION returns a value and is used inside an expression: x ← Square(5). A PROCEDURE performs an action and is CALLed on its own line: CALL PrintHeader(). Choosing the wrong one loses marks even if the logic is right.
//String handling functions
Know the standard ones and that positions usually start at 1 in pseudocode.
LENGTH("Hello") = 5
SUBSTRING("Hello",1,3) = "Hel"
UCASE("hi") = "HI"
LCASE("HI") = "hi"CHECK YOURSELF
1.A procedure changes a parameter passed BYVALUE. What happens to the caller's variable?
2.Which should you use when the code must give a value back for use in an expression?