程式設計基礎/函式虛擬碼示例
外觀
< 程式設計基礎
建議的函式虛擬碼約定以及一些示例。
不存在虛擬碼語法的標準。但是,有一些普遍遵循的約定,有助於使一個程式設計師編寫的虛擬碼易於被另一個程式設計師理解。以下描述了一種使用虛擬碼編寫函式的方法,這種方法可以被程式設計師理解。五種概念是
- 使用一個開始語句詞來開始函式
- 使用一個通訊語句詞來標識傳遞給函式的專案
- 使用縮排顯示函式的動作部分
- 使用一個通訊語句詞來標識從函式傳遞出的專案
- 使用一個結束語句詞來結束函式
- 使用一個呼叫語句詞來指示你的程式使用一個函式
以下是建議的函式語句詞概述
| 專案/目的 | 開始語句詞 | 結束語句詞 |
| 開始 | 函式 | N/A |
| 通訊傳入 | 傳入 | 無 |
| 操作 | N/A | N/A |
| 通訊傳出 | 傳出 | 無 |
| 結束 | N/A | 結束函式 |
| 呼叫函式 | 呼叫 | 無 |
以下是一些使用我們上面描述的約定,以虛擬碼定義的函式的示例。
Function clear monitor
Pass In: nothing
Direct the operating system to clear the monitor
Pass Out: nothing
Endfunction
Function delay program so you can see the monitor
Pass In: integer representing tenths of a second
Using the operating system delay the program
Pass Out: nothing
Endfunction
Function main
Pass In: nothing
Doing some lines of code
Call: clear monitor
Doing some lines of code
Pass Out: value zero to the operating system
Endfunction
- 語句詞
- 用於使虛擬碼邏輯對任何讀者都清晰的詞語。