Functions · Funções
This page needs a recent browser (with SharedArrayBuffer support). Please update Chrome, Edge, Firefox or Safari to the latest version. · Esta página precisa de um navegador recente (com suporte a SharedArrayBuffer). Por favor, atualize Chrome, Edge, Firefox ou Safari para a versão mais recente.
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.
Português
Uma função retorna um único valor
- Uma função é como um procedimento, e também devolve um valor.
- A definição diz o tipo desse valor:
RETURNS INTEGER. RETURNdefine o valor. Geralmente é a última linha da função.
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.
Português
Não escreva CALL
- Uma função é usada dentro de uma expressão, onde seu valor retornado é necessário.
CALLé apenas para um procedimento.- Parâmetros de uma função são passados por valor. O guia diz para não usar
BYREFem uma função.
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.
Português
Erros comuns
RETURNpertence dentro da função. Fora dela, não há nada para retornar.- A chamada
Square(6)não é uma declaração completa por si só.OUTPUT Square(6)é.
English
Now you try
- Write a function and use its result in
OUTPUT.
Português
Agora você tenta
- Escreva uma função e use seu resultado em
OUTPUT.
Write Square so that OUTPUT Square(6) prints 36. · Escreva Square para que OUTPUT Square(6) imprima 36.
Click Run to see the output here. · Clique em Executar para ver a saída aqui.
Write Max to return the larger of two integers. OUTPUT Max(10, 24) should print 24. · Escreva Max para retornar o maior de dois inteiros. OUTPUT Max(10, 24) deve imprimir 24.
Click Run to see the output here. · Clique em Executar para ver a saída aqui.