Functions · 関数
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
A function returns one value
- A function is like a procedure, and it also hands one value back.
- The definition says the type of that value:
RETURNS INTEGER. RETURNsets the value. It is normally the last line of the function.
日本語
関数は1つの値を返す
- 関数は手続きに似ており、1つの値も手渡します。
- 定義でその値の型を指定します:
RETURNS INTEGER。 RETURNは値を設定します。通常、関数の最終行にあります。
FUNCTION Square(N : INTEGER) RETURNS INTEGER
RETURN N * N
ENDFUNCTION
OUTPUT Square(6)
English
Do not write CALL
- A function is used inside an expression, where its returned value is needed.
CALLis only for a procedure.- Parameters of a function are passed by value. The guide says not to use
BYREFon a function.
日本語
CALL を書かない
- 関数は式の中で使用され、その返される値が必要です。
CALLは手続き专用的です。- 関数の引数は値渡しで渡されます。ガイドでは関数に
BYREFを使用しないよう指示しています。
FUNCTION Max(Number1 : INTEGER, Number2 : INTEGER) RETURNS INTEGER
IF Number1 > Number2 THEN
RETURN Number1
ELSE
RETURN Number2
ENDIF
ENDFUNCTION
OUTPUT Max(10, 24)
English
Common mistakes
RETURNbelongs inside the function. Outside it, there is nothing to return to.- The call
Square(6)is not a complete statement on its own.OUTPUT Square(6)is.
日本語
よくあるミス
RETURNは関数内にあります。外側には返すものがありません。- 呼び出し
Square(6)は独立した完全文ではありません。OUTPUT Square(6)はそうです。
English
Now you try
- Write a function and use its result in
OUTPUT.
日本語
あなたも試してみよう
- 関数を書き、その結果を
OUTPUTで使用します。
Write Square so that OUTPUT Square(6) prints 36. · Squareを記述し、OUTPUT Square(6)が36を印刷するようにします。
Click Run to see the output here. · 実行ボタンをクリックして出力を確認してください。
Write Max to return the larger of two integers. OUTPUT Max(10, 24) should print 24. · Maxを記述し、2つの整数のうち大きい方を返します。OUTPUT Max(10, 24)は24を印刷する必要があります。
Click Run to see the output here. · 実行ボタンをクリックして出力を確認してください。