Functions · Funciones
This page needs a recent browser (with SharedArrayBuffer support). Please update Chrome, Edge, Firefox or Safari to the latest version. · Esta página necesita un navegador reciente (con soporte para SharedArrayBuffer). Por favor, actualiza Chrome, Edge, Firefox o Safari a la última versión.
English
A function returns one value
- A function is like a procedure, and it also hands one value back.
- The definition says the type of that value:
RETURNS INTEGER. RETURNsets the value. It is normally the last line of the function.
Español
Una función devuelve un valor
- Una función es como un procedimiento, y también devuelve un valor.
- La definición indica el tipo de ese valor:
RETURNS INTEGER. RETURNestablece el valor. Generalmente es la última línea de la función.
FUNCTION Square(N : INTEGER) RETURNS INTEGER
RETURN N * N
ENDFUNCTION
OUTPUT Square(6)
English
Do not write CALL
- A function is used inside an expression, where its returned value is needed.
CALLis only for a procedure.- Parameters of a function are passed by value. The guide says not to use
BYREFon a function.
Español
No escribir CALL
- Una función se utiliza dentro de una expresión, donde se necesita su valor devuelto.
CALLsolo se usa para un procedimiento.- Los parámetros de una función se pasan por valor. La guía indica no usar
BYREFen una función.
FUNCTION Max(Number1 : INTEGER, Number2 : INTEGER) RETURNS INTEGER
IF Number1 > Number2 THEN
RETURN Number1
ELSE
RETURN Number2
ENDIF
ENDFUNCTION
OUTPUT Max(10, 24)
English
Common mistakes
RETURNbelongs inside the function. Outside it, there is nothing to return to.- The call
Square(6)is not a complete statement on its own.OUTPUT Square(6)is.
Español
Errores comunes
RETURNpertenece dentro de la función. Fuera de ella, no hay nada a lo que devolver.- La llamada
Square(6)no es una instrucción completa por sí sola.OUTPUT Square(6)sí lo es.
English
Now you try
- Write a function and use its result in
OUTPUT.
Español
Ahora tú intentas
- Escribe una función y usa su resultado en
OUTPUT.
Write Square so that OUTPUT Square(6) prints 36. · Escribe Square de modo que OUTPUT Square(6) imprima 36.
Click Run to see the output here. · Haz clic en Ejecutar para ver la salida aquí.
Write Max to return the larger of two integers. OUTPUT Max(10, 24) should print 24. · Escribe Max para devolver el mayor de dos enteros. OUTPUT Max(10, 24) debería imprimir 24.
Click Run to see the output here. · Haz clic en Ejecutar para ver la salida aquí.