Functions · Funciones
A FUNCTION returns a value
- A function works something out and hands back a value with
RETURN. RETURNS(in the header) states the type of value it gives back.
UNA FUNCTION devuelve un valor
- Una function calcula algo y devuelve un valor mediante
RETURN. RETURNS(en el encabezado) indica el tipo de valor que devuelve.
FUNCTION Square(N : INTEGER) RETURNS INTEGER
RETURN N * N
ENDFUNCTION
OUTPUT Square(6) // → 36
Run, then RETURN a value · Ejecutar, luego RETURN un valor
A FUNCTION runs its steps, then RETURNs a value you can use in an expression. · Una FUNCTION ejecuta sus pasos y luego RETURN un valor que puedes usar en una expresión.
Use the returned value
- Call a function inside an expression, or store its result in a variable.
Usar el valor devuelto
- Llame a una función dentro de una expresión, o almacene su resultado en una variable.
FUNCTION Square(Number : INTEGER) RETURNS INTEGER
RETURN Number * Number
ENDFUNCTION
DECLARE Answer : INTEGER
Answer ← Square(4)
OUTPUT Answer + 1 // → 17
A function can return a Boolean
- The result can be a
BOOLEAN— handy for a yes/no test.
Una function puede devolver un Booleano
- El resultado puede ser un
BOOLEAN— útil para una prueba de sí/no.
FUNCTION IsPositive(N : INTEGER) RETURNS BOOLEAN
RETURN N > 0
ENDFUNCTION
OUTPUT IsPositive(5) // → TRUE
Watch out
- A procedure does something; a function returns a value you then use.
- Every path through a function must reach a
RETURN.
Tenga cuidado
- Una procedure hace algo; una function devuelve un valor que usted usa después.
- Cada camino a través de una function debe llegar a un
RETURN.
Common mistakes
FUNCTION Name(...) RETURNS TYPE ... RETURN Value ... ENDFUNCTION.- A function must
RETURNa value.
Errores comunes
FUNCTION Name(...) RETURNS TYPE ... RETURN Value ... ENDFUNCTION.- Una function debe
RETURNun valor.
Now you try
- Define a function with
RETURNSandRETURN. - Press Check answer to test it.
Ahora pruébalo
- Defina una function con
RETURNSyRETURN. - Presione Check answer para probarlo.
Define FUNCTION Square(N : INTEGER) RETURNS INTEGER that returns N × N, then output Square(5) (it is 25). · Define FUNCTION Square(N : INTEGER) RETURNS INTEGER que retorne N × N, luego muestra Square(5) (es 25).
Click Run to see the output here. · Haz clic en Ejecutar para ver la salida aquí.
Define FUNCTION Add(a : INTEGER, b : INTEGER) RETURNS INTEGER that returns a + b, then output Add(3, 4) (it is 7). · Define FUNCTION Add(a : INTEGER, b : INTEGER) RETURNS INTEGER que retorne a + b, luego muestra Add(3, 4) (es 7).
Click Run to see the output here. · Haz clic en Ejecutar para ver la salida aquí.
Define FUNCTION IsEven(N : INTEGER) RETURNS BOOLEAN that returns TRUE when N is even, then output IsEven(8) (it is TRUE). · Define FUNCTION IsEven(N : INTEGER) RETURNS BOOLEAN que retorne TRUE cuando N sea par, luego muestra IsEven(8) (es TRUE).
Click Run to see the output here. · Haz clic en Ejecutar para ver la salida aquí.