Procedures and BYREF · Procedimentos e BYREF
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
CALL runs a procedure
- A procedure is a named block. It does a job and returns no value.
- Define it with
PROCEDURE…ENDPROCEDURE, and run it withCALL. - Parentheses are written even when there are no parameters.
Português
CALL executa um procedimento
- Um procedimento é um bloco nomeado. Ele realiza uma tarefa e não retorna valor.
- Defina-o com
PROCEDURE…ENDPROCEDURE, e execute-o comCALL. - Parênteses são escritos mesmo quando não há parâmetros.
PROCEDURE Greet()
OUTPUT "Hi"
ENDPROCEDURE
CALL Greet()
English
By value is the default
- A parameter with no mode is passed by value: the procedure gets a copy.
- Changing the copy does not change the caller's variable.
- You may also write
BYVALin front of the name. It means the same thing.
Português
Por valor é o padrão
- Um parâmetro sem modo é passado por valor: o procedimento recebe uma cópia.
- Alterar a cópia não altera a variável do chamador.
- Você também pode escrever
BYVALantes do nome. Significa a mesma coisa.
PROCEDURE Show(N : INTEGER)
N ← N + 1
OUTPUT N
ENDPROCEDURE
DECLARE A : INTEGER
A ← 5
CALL Show(A)
OUTPUT A
English
BYREF passes the real variable
BYREFmeans the parameter is the caller's variable. A change is still there afterCALL.- If several parameters in a row use the same mode, the guide lets you write the keyword once.
- Do not pass a parameter to a function by reference. Use
BYREFon a procedure.
Português
BYREF passa a variável real
BYREFsignifica que o parâmetro é a variável do chamador. Uma alteração permanece apósCALL.- Se vários parâmetros seguidos usarem o mesmo modo, o guia permite escrever a palavra-chave apenas uma vez.
- Não passe um parâmetro para uma função por referência. Use
BYREFem um procedimento.
PROCEDURE Swap(BYREF X : INTEGER, BYREF Y : INTEGER)
DECLARE Temp : INTEGER
Temp ← X
X ← Y
Y ← Temp
ENDPROCEDURE
DECLARE A : INTEGER
DECLARE B : INTEGER
A ← 1
B ← 2
CALL Swap(A, B)
OUTPUT A & "," & B
English
Common mistakes
CALLbefore a procedure. NeverCALLa function.- IGCSE 0478 has no
BYREF. On that paper every parameter is a copy.
Português
Erros comuns
CALLantes de um procedimento. NuncaCALLuma função.- IGCSE 0478 não tem
BYREF. Naquele exame, todo parâmetro é uma cópia.
English
Now you try
- Write a procedure that swaps two integers.
Português
Agora você tenta
- Escreva um procedimento que troque dois inteiros.
Write a procedure Greet with no parameters that outputs Hi, then CALL it. · Escreva um procedimento Greet sem parâmetros que imprima Hi, e depois CALL-o.
Click Run to see the output here. · Clique em Executar para ver a saída aqui.
Write Swap so that BYREF exchanges A and · e B. Start from A ← 1 and · e B ← 2, and output 2,1. · Escreva Swap para que BYREF troque A e B. Comece de A ← 1 e B ← 2, e saia 2,1.
Click Run to see the output here. · Clique em Executar para ver a saída aqui.