Rexx 程式設計 / Rexx 入門 / 函式
外觀
函式呼叫後面必須加括號,其中包含可選的引數。如果函式沒有可選引數,則使用一對空括號。
包含函式引數的括號必須緊跟在函式名後面。函式名與其引數列表之間不允許有空格,否則函式名可能會被誤解為變數名。
say random() /* No whitespace is allowed between the function and the parentheses */
Rexx 中的函式總是返回一個結果,該結果將在函式執行完畢後替換函式呼叫本身,從而成為表示式的一部分。Rexx 中已經提供了一些內建函式供您使用,例如上面示例中使用的 RANDOM 函式,或者用於求數字絕對值的 ABS 函式。
say abs(4 - 3 * (-5)) /* Absolute value of: 4 - 3 * (-5) --> replaced by 19 */ say 4 - 3 * abs(-5) /* abs(-5) gets replaced by 5, final answer is -11 */
Rexx 有內建函式可以處理和返回各種型別的值,包括數字、布林值或更一般的字串。您可能已經在本入門指南的不同部分見過其中的一些函式。
say right(x, 5) /* Right-justifies some text to a particular width. */ if datatype(x, 'N') then /* Checks the data type of a given string. */ x = abs(x) /* Takes the absolute value of a number. */ say random() /* Returns a random number. */
您也可以透過編寫子程式來建立自己的函式,如其他地方所述。