절과 BYREF
이 페이지는 최신 브라우저(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.
English
PROCEDURE ShowRule()
OUTPUT "----------"
ENDPROCEDURE
CALL ShowRule()
OUTPUT "Club results"
CALL ShowRule()
- One definition, two
CALLs: the line is printed twice. The definition alone prints nothing.
English
By value is the default
- If no mode has been specified, parameters start by value: the procedure gets a copy.
- Changing the copy does not change the caller's variable.
BYVALexplicitly selects a copy. At the start of a parameter list it is the default; afterBYREF, writeBYVALto switch back.
English
PROCEDURE Show(Number : INTEGER)
Number ← Number + 1
OUTPUT Number
ENDPROCEDURE
DECLARE FirstNumber : INTEGER
FirstNumber ← 5
CALL Show(FirstNumber)
OUTPUT FirstNumber
English
BYREF passes the real variable
BYREFmeans the parameter is the caller's variable. A change is still there afterCALL.- A mode keyword continues across later parameters until another mode keyword changes it.
BYREF FirstValue : INTEGER, SecondValue : INTEGERmakes both parameters references. - Do not pass a parameter to a function by reference. Use
BYREFon a procedure.
English
PROCEDURE AddVisit(BYREF Visits : INTEGER)
Visits ← Visits + 1
ENDPROCEDURE
DECLARE Count : INTEGER
Count ← 0
CALL AddVisit(Count)
CALL AddVisit(Count)
CALL AddVisit(Count)
OUTPUT "Visits: ", Count
| After | Count with BYREF |
Count if it were by value |
|---|---|---|
first CALL |
1 | 0 |
second CALL |
2 | 0 |
third CALL |
3 | 0 |
- A procedure that must change two of the caller's variables needs
BYREFon both parameters.
English
Common mistakes
CALLbefore a procedure. NeverCALLa function.- IGCSE 0478 has no
BYREF. On that paper every parameter is a copy.
English
Now you try
- Write a procedure with no parameters, then one that swaps two integers. A swap changes both variables, so think about which mode each parameter needs.
English
Independent transfer
The final task has no supplied algorithm. Plan its inputs, processing and outputs, then solve it before opening support. Check more than the sample case.
난이도 2/5 · 140코인 + 오늘 첫 완료 시 작은 보너스 추가
매개변수가 없는 절차 Greet을 작성하여 Hi을 출력한 뒤, 이를 CALL하십시오.
출력을 보려면 '실행'을 클릭하세요.
난이도 2/5 · 140코인 + 오늘 첫 완료 시 작은 보너스 추가
BYREF이 FirstNumber과 SecondNumber을 교체하도록 Swap을 작성하십시오. FirstNumber ← 1과 SecondNumber ← 2에서 시작하여 2,1을 출력하십시오.
출력을 보려면 '실행'을 클릭하세요.
난이도 3/5 · 200코인 + 오늘 첫 완료 시 작은 보너스 추가
PROCEDURE Reduce(BYREF Amount : INTEGER, BYVAL Discount : INTEGER)만 작성합니다. Amount를 Discount만큼 감축하되, 0보다 작아지지 않도록 합니다. 체크기가 이를 호출합니다.
출력을 보려면 '실행'을 클릭하세요.