Procedures and BYREF
This page needs a recent browser (with SharedArrayBuffer support). Please update Chrome, Edge, Firefox or Safari to the latest version. · עמוד זה דורש דפדפן עדכני (תומך ב-SharedArrayBuffer). אנא עדכן את Chrome, Edge, Firefox או Safari לגרסה העדכנית ביותר.
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.
עברית
CALL מפעיל פרוצדורה
- פרוצדורה היא בלוק בעל שם. היא מבצעת משימה ואינה מחזירה ערך.
- הגדר אותה עם
PROCEDURE…ENDPROCEDURE, והפעל אותה עםCALL. - סוגרי עגולים נכתבים גם כאשר אין פרמטרים.
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.
עברית
העברה לפי ערך היא ברירת המחדל
- פרמטר ללא מצב (mode) מועבר פנימה לפי ערך: לפרוצדורה מגיע עותק.
- שינוי העתק אינו משנה את המשתנה של הקורא.
- ניתן גם לכתוב
BYVALלפני השם. המשמעות זהה.
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.
עברית
BYREF מעביר את המשתנה האמיתי
BYREFאומר שהפרמטר הוא המשתנה של הקורא. שינוי נשמר גם לאחרCALL.- אם כמה פרמטרים ברצף משתמשים באותו מצב, מדריך הלימודים מאפשר לך לכתוב את המילה-המפתח פעם אחת.
- אל תעביר פרמטר ל-פונקציה על ידי ייחוס. השתמש ב
BYREFבפרוצדורה.
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.
עברית
טעויות נפוצות
CALLלפני פרוצדורה. לעולם אל תעבירCALLלפונקציה.- IGCSE 0478 אין לה
BYREF. במבחן הזה כל פרמטר הוא עתק.
English
Now you try
- Write a procedure that swaps two integers.
עברית
כעת תנסו בעצמכם
- כתוב פרוצדורה שמחליפה שני שלמים.
Write a procedure Greet with no parameters that outputs Hi, then CALL it.
Click Run to see the output here. · לחץ על הרץ כדי לראות את התוצא כאן.
Write Swap so that BYREF exchanges A and B. Start from A ← 1 and B ← 2, and output 2,1.
Click Run to see the output here. · לחץ על הרץ כדי לראות את התוצא כאן.