Procedures and BYREF · Procedimientos y BYREF
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
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.
Español
CALL ejecuta un procedimiento
- Un procedimiento es un bloque con nombre. Realiza una tarea y no devuelve ningún valor.
- Definirlo con
PROCEDURE…ENDPROCEDURE, y ejecutarlo conCALL. - Los paréntesis se escriben incluso cuando no hay 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.
Español
Por defecto se pasa por valor
- Un parámetro sin modo se pasa por valor: el procedimiento recibe una copia.
- Modificar la copia no cambia la variable del llamador.
- También puedes escribir
BYVALdelante del nombre. Significa lo mismo.
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.
Español
BYREF pasa la variable real
BYREFsignifica que el parámetro es la variable del llamador. Un cambio persiste después deCALL.- Si varios parámetros consecutivos usan el mismo modo, la guía permite escribir la palabra clave una sola vez.
- No pases un parámetro a una función por referencia. Usa
BYREFen un procedimiento.
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.
Español
Errores comunes
CALLantes de un procedimiento. NuncaCALLuna función.- IGCSE 0478 no tiene
BYREF. En ese examen cada parámetro es una copia.
English
Now you try
- Write a procedure that swaps two integers.
Español
Ahora tú intentas
- Escribe un procedimiento que intercambie dos enteros.
Write a procedure Greet with no parameters that outputs Hi, then CALL it. · Escribe un procedimiento Greet sin parámetros que emita Hi, luego CALL it.
Click Run to see the output here. · Haz clic en Ejecutar para ver la salida aquí.
Write Swap so that BYREF exchanges A and · y B. Start from A ← 1 and · y B ← 2, and output 2,1. · Escribe Swap para que BYREF intercambie A y B. Comienza con A ← 1 y B ← 2, y emite 2,1.
Click Run to see the output here. · Haz clic en Ejecutar para ver la salida aquí.