Functions · Funções
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.
A FUNCTION retorna um valor
- Uma function calcula algo e devolve um valor com
RETURN. RETURNS(no cabeçalho) declara o tipo de valor que ela devolve.
FUNCTION Square(N : INTEGER) RETURNS INTEGER
RETURN N * N
ENDFUNCTION
OUTPUT Square(6) // → 36
Run, then RETURN a value · Execute, então RETURN um valor
A FUNCTION runs its steps, then RETURNs a value you can use in an expression. · Um FUNCTION executa seus passos, então RETURNs um valor que você pode usar em uma expressão.
Use the returned value
- Call a function inside an expression, or store its result in a variable.
Usar o valor retornado
- Chame uma function dentro de uma expressão, ou armazene seu resultado em uma variável.
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.
Uma function pode retornar um Boolean
- O resultado pode ser um
BOOLEAN— útil para um teste sim/não.
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.
Atenção
- Uma procedure faz algo; uma function devolve um valor que você usa depois.
- Todo caminho através de uma function deve alcançar um
RETURN.
Common mistakes
FUNCTION Name(...) RETURNS TYPE ... RETURN Value ... ENDFUNCTION.- A function must
RETURNa value.
Erros comuns
FUNCTION Name(...) RETURNS TYPE ... RETURN Value ... ENDFUNCTION.- Uma function deve
RETURNum valor.
Now you try
- Define a function with
RETURNSandRETURN. - Press Check answer to test it.
Agora você tenta
- Defina uma function com
RETURNSeRETURN. - Pressione Check answer para testá-lo.
Define FUNCTION Square(N : INTEGER) RETURNS INTEGER that returns N × N, then output Square(5) (it is 25). · Defina FUNCTION Square(N : INTEGER) RETURNS INTEGER que retorne N × N, depois saia com Square(5) (é 25).
Click Run to see the output here. · Clique em Executar para ver a saída aqui.
Define FUNCTION Add(a : INTEGER, b : INTEGER) RETURNS INTEGER that returns a + b, then output Add(3, 4) (it is 7). · Defina FUNCTION Add(a : INTEGER, b : INTEGER) RETURNS INTEGER que retorne a + b, depois saia com Add(3, 4) (é 7).
Click Run to see the output here. · Clique em Executar para ver a saída aqui.
Define FUNCTION IsEven(N : INTEGER) RETURNS BOOLEAN that returns TRUE when N is even, then output IsEven(8) (it is TRUE). · Defina FUNCTION IsEven(N : INTEGER) RETURNS BOOLEAN que retorne TRUE quando N for par, depois saia com IsEven(8) (é TRUE).
Click Run to see the output here. · Clique em Executar para ver a saída aqui.