Funciones
A function returns one value
- A function is like a procedure, but it also hands one value back to the line that called it.
- The first line gives the type of that value after
RETURNS:RETURNS INTEGER. - Inside the function,
RETURNhands the value back. The function stops there. ENDFUNCTIONcloses the definition, likeENDIFcloses anIF.
FUNCTION Square(Number : INTEGER) RETURNS INTEGER
RETURN Number * Number
ENDFUNCTION
OUTPUT Square(6)
What happens when you call it
Follow OUTPUT Square(6) one step at a time:
| Step | What happens | Value |
|---|---|---|
| 1 | The call copies 6 into the parameter Number |
Number = 6 |
| 2 | The body works out Number * Number |
36 |
| 3 | RETURN hands 36 back to the call |
Square(6) becomes 36 |
| 4 | OUTPUT prints the value it was given |
prints 36 |
- The parameter already holds the value from the call. Do not
INPUTit again inside the function: that would throw the6away. - Inside the function, use the parameter's name (
Number). The function's own name (Square) is only for calling it.
A function used inside an expression
- Do not write
CALLfor a function.CALLis only for a procedure. - Use a function where a value is needed: in
OUTPUT, on the right of←, or in a condition. - Parameters of a function are passed by value. The guide says not to use
BYREFon a function.
FUNCTION Max(Number1 : INTEGER, Number2 : INTEGER) RETURNS INTEGER
IF Number1 > Number2 THEN
RETURN Number1
ELSE
RETURN Number2
ENDIF
ENDFUNCTION
DECLARE Biggest : INTEGER
Biggest ← Max(10, 24)
OUTPUT Biggest
OUTPUT Max(7, 3) + 1
A function can return TRUE or FALSE
RETURNS BOOLEANmeans the answer isTRUEorFALSE.- A comparison such as
Mark >= 50is alreadyTRUEorFALSE, so you can return it directly. AnIFthat returnsTRUEin one branch andFALSEin the other does the same job. - Use the result in a condition:
IF HasPassed(Mark) THEN.
FUNCTION HasPassed(Mark : INTEGER) RETURNS BOOLEAN
RETURN Mark >= 50
ENDFUNCTION
OUTPUT HasPassed(72)
OUTPUT HasPassed(31)
IF HasPassed(50) THEN
OUTPUT "50 is a pass"
ENDIF
Testing your function
- While you work, you may add your own test lines under the function, for example
OUTPUT HasPassed(49). - When a task says Write only the function, Check calls your function itself with several different values, and your own test lines do not stop it from passing.
- In the other tasks, Check compares the whole output. Remove your extra test lines before you press Check.
- Test values on both sides of a boundary, such as
49and50, and also0and a negative number.
Common mistakes
Each row shows a wrong line that students really wrote, then the correct line and the reason.
| Wrong | Right | Why |
|---|---|---|
RETURNS Number * Number |
RETURN Number * Number |
RETURNS belongs only in the first line |
RETURN ← TRUE |
RETURN TRUE |
← stores into a variable; RETURN takes a value |
Square ← Number * Number |
RETURN Number * Number |
the function's name is not a variable |
IF Value MOD 2 == 0 THEN |
IF Value MOD 2 = 0 THEN |
pseudocode compares with one = |
OUTPUT Square("6") |
OUTPUT Square(6) |
"6" in quotes is text, not a number |
END FUNCTION |
ENDFUNCTION |
one word, no space |
Square(6) alone on a line |
OUTPUT Square(6) |
a function's value must be used |
- A definition prints nothing by itself. Only the line that calls the function, such as
OUTPUT Max(4, 7), prints. - An extra
OUTPUTinside the function prints a second line and changes the output.
Now you try
- Write a function and use its result in
OUTPUT.
Independent transfer
The final task has no supplied algorithm. Plan its inputs, processing and outputs, then solve it before opening support. Check more than the sample case.
Dificultad 2/5 · 140 monedas + un pequeño bono en el primer intento de hoy
Write Square so that OUTPUT Square(6) prints 36.
Haz clic en Ejecutar para ver la salida aquí.
Dificultad 2/5 · 140 monedas + un pequeño bono en el primer intento de hoy
Write Max to return the larger of two integers. OUTPUT Max(10, 24) should print 24.
Haz clic en Ejecutar para ver la salida aquí.
Dificultad 3/5 · 200 monedas + un pequeño bono en el primer intento de hoy
Write only FUNCTION IsEven(Value : INTEGER) RETURNS BOOLEAN. Return TRUE exactly when Value is even.
Haz clic en Ejecutar para ver la salida aquí.