跳至內容

Rexx 程式設計/Rexx 指南/子程式

來自華夏公益教科書,開放的書籍,構建開放的世界

“過程”或“子程式”是程式碼中獨立的部分,執行特定任務。它們是大型程式中的小型程式。我們可以在程式碼中使用 CALL 語句啟用它們。子程式可以使用 RETURN 語句表示它已完成其任務。

/* Call subroutines to make different animal noises. */
 call BarkFiveTimes
 call MeowThreeTimes
 call MooEightTimes
 exit
BarkFiveTimes:
 do 5
  say "Woof!"
 end
 return
MeowThreeTimes:
 do 3
  say "Meow!"
 end
 return
MooEightTimes:
 do 8
  say "Moo!"
 end
 return

解析引數

[編輯 | 編輯原始碼]

理想情況下,子程式應該足夠靈活以響應各種情況。我們應該能夠將一個或多個數據片段饋入子程式以修改其行為。這些資料片段稱為“引數”,你可以將它們包含在 CALL 語句中。它們可以使用 PARSE ARG 語句分配給子程式中的變數。

/* Bark or meow as many times as you want. */
 call Bark 5
 call Meow 3
 exit
Bark:
 parse arg bark_count
 do bark_count
  say "Woof!"
 end
 return
Meow:
 parse arg meow_count
 do meow_count
  say "Meow!"
 end
 return

區域性變數

[編輯 | 編輯原始碼]

由於在不同的子程式中使用不同的變數名來表示相同的目的很煩人,我們可以使用 PROCEDURE 關鍵字使函式完全獨立。它將有自己的變數,這些變數不會與程式中其他地方的變數衝突,即使它們具有相同的名稱。

 call Bark 5
 call Meow 3
 exit
Bark:
 procedure
 parse arg count
 do count
  say "Woof!"
 end
 return
Meow:
 procedure
 parse arg count
 do count
  say "Meow!"
 end
 return

程式碼重用

[編輯 | 編輯原始碼]

只要你在程式的不同部分看到相同型別的程式碼,你可能就有一個可以在過程中設定的單個流程。這樣,任何錯誤都只需在一個地方糾正一次。

/* Make any animal noise as many times as you want. */
 call MakeNoise "Woof!", 5
 call MakeNoise "Meow!", 3
 call MakeNoise "Moo!", 8
 exit
MakeNoise:
 procedure
 parse arg noise, count
 do count
  say noise
 end
 return

返回值

[編輯 | 編輯原始碼]

子程式不僅可以從呼叫它們的程式部分接收值,而且它們在完成時也可以將資料發回。子程式透過在 RETURN 語句中包含資料來返回資料片段,之後它將在名為 RESULT 的變數中可用。

/* Use a subroutine to convert angle measures. */
 say "Enter a number of degrees:"
 pull number
 call GetRadians number
 say number "degrees is the same as" result "radians."
 exit
GetRadians:
 procedure
 parse arg degrees
 pi = 3.14159
 return pi * degrees / 180

作為呼叫子程式和訪問結果的兩個不同語句的捷徑,可以使用本書函式部分中描述的單行符號(使用括號)。

 say "Enter a number of degrees:"
 pull number
 say number "degrees is the same as " GetRadians(number) "radians."
 exit

順便說一下,如果你希望引數在進入子程式時大寫,或者如果你不關心字母的大小寫,因為引數是數字,你可以省略 PARSE 關鍵字。

GetRadians:
 procedure
 arg degrees
 pi = 3.14159
 return pi * degrees / 180

公開變數

[編輯 | 編輯原始碼]

如果你希望過程只部分獨立,可以使用 EXPOSE 關鍵字讓它訪問特定的全域性變數。這在需要在程式的不同部分之間共享一些資料時很有用。

/* Use the same value of pi to perform different circle computations. */
 PI = 3.14159
 call CircleArea 10
 say "The area of a circle with radius 10 is" result"."
 call Circumference 50
 say "The circumference of a circle of diameter 50 is" result"."
 exit
CircleArea:
 procedure expose PI
 arg radius
 return PI * radius ** 2
Circumference:
 procedure expose PI
 arg diameter
 return PI * diameter

子程式也可以呼叫自身並接收自己的返回值。效果類似於迴圈,如果子程式需要重新開始並嘗試其他操作,也許使用稍微不同的資料,以便完成其原始任務,這將很有用。

/* First 10 triangular numbers */
 call ShowTriangulars 10
 exit
ShowTriangulars:
 procedure
 arg how_many
 if how_many > 0 then do
  one_less = how_many - 1
  call ShowTriangulars one_less
  say Triangular(how_many)
 end
 return
Triangular:
 procedure
 arg which?
 if which? > 1 then do
  previous = which? - 1
  return Triangular(previous) + which?
 end
 return 1

如果你期望函式重複自身數百次或數千次,你可能最好在函式內部編寫一個迴圈。如果同一個函式在返回之前呼叫自身太多次,程式可能會崩潰。

華夏公益教科書