Routine在一般英文的意思是 “例行公事" , 程式裡面的 “例行公事" 就是 program (主程式), 而 “在例行公事" 之下的–也就是主程式之下的–就是 subroutine, 一般翻譯成 “副程式" . 主程式(program)的程式碼在程式開始時就會自動執行, 而副程式(subroutine)不會自動執行自己的程式碼, 它必須被 “呼叫" (call), 才會執行程式碼.
在程式設計時經常遇到需要重覆執行同一組步驟的情況, 在這種情況下, 一直重複寫同一段程式碼是非常腦殘的事情, 所以我們需要副程式, 每當要用到的時候只要呼叫(call)一下就可以.
As you know, the computer runs a program by reading and processing the statements line by line, one at a time. Sometimes, you may want the computer to break the flow and jump to another line of code while the program is running. You can instruct the computer to process a line of code out of sequence if you use the Goto statement.
In this program, the Task_A: statement is called a label, which is similar to a bookmark. You can add as many labels as you want and name them whatever you want, as long as you don’t use the same name more than once. (可以自由命名, Task_A是我自己取的, 一個名字只能用一次, label名稱不能重複). The Goto statement instructs the computer to run the statements after the Task_A: label again only if the condition in the If statement is true. You can also use the Goto statement to make a program run forever. (所以自己要很清楚Goto是執行哪一段程式, 以免掉進無窮盡執行的窘境, 導致電腦當機).
Subroutines in Small Basic Programs
Very often while writing programs we’ll run into cases where we’ll have to execute the same set of steps, over and over again. In those cases, it probably wouldn’t make sense to rewrite the same statements multiple times. That’s when Subroutines come in handy.
A subroutine is a portion of code within a larger program that usually does something very specific, and that can be called from anywhere in the program. Subroutines are identified by a name that follows the Sub keyword and are terminated by the EndSub keyword. For example, the following snippet (片段) represents a subroutine whose name is PrintTime, and it does the job of printing the current time to the TextWindow.
Below is a program that includes the subroutine and calls it from various places.
每次需要用到這個副程式的時候只要呼叫 PrintTime() 就可以了. You execute a subroutine by calling SubroutineName(). As usual, the punctuators “()” are necessary to tell the computer that you want to execute a subroutine.
- 第一點: As we just saw above, subroutines help reduce the amount of code you have to type in (可以減少重複打相同的一段程式碼). Once you have the PrintTime subroutine written, you can call it from anywhere in your program and it’ll print the current time.
- 第二點: In addition, subroutines can help decompose complex problems into simpler pieces (把複雜的大程式切小塊解決, 化整為零). Say you had a complex equation to solve, you can write several subroutines that solved smaller pieces of the complex equation. Then you can put the results together to get the solution to the original complex equation.
- 第三點: Subroutines can also aid in improving the readability of a program (增加程式可讀性, 讓別人讀起來也覺得簡單易懂). In other words, if you have well named subroutines for commonly run portions of your program, your program becomes easy to read and comprehend. This is very important if you want to understand someone else’s program or if you want your program to be understood by others. Sometimes, it is helpful even when you want to read your own program, say a week after you wrote it.
You can access and use any variable that you have in a program from within a subroutine. As an example, the following program accepts two numbers and prints out the larger of the two. Notice that the variable max is used both inside and outside of the subroutine. (max 這個變數在副程式內外可以同時使用)
Let’s look at another example that will illustrate the usage of Subroutines. This time we’ll use a graphics program that computes various points which it will store in variables x and y. Then it calls a subroutine DrawCircleUsingCenter which is responsible for drawing a circle using x and y as the center. (以變數 x 和 y 的值為圓心座標值不斷畫圓. 這邊有用到三角函數的正弦(sin)和餘弦(cos), 再配合座標和角度, 這部分我已經還給高中數學老師了, 所以看範例的時候不清楚為什麼要設那個數字, 也無法在心中先有個圖形的輪廓, 這部分只好傻傻按照教材範例打)
Sometimes subroutines get called from inside a loop, during which time they execute the same set of statements but with different values in one or more of the variables. For instance, say if you have a subroutine named PrimeCheck and this subroutine determines if a number is prime (質數) or not. You can write a program that lets the user to enter a value and you can then say if it is prime or not, using this subroutine. (質數判斷程式)
隨便打一個很大的數字, 結尾用奇數試試看, 因為用偶數就絕對不是質數, 至少有 2 這個因數
如果有一個任務是 “列出100以內的所有質數" , 並調用 PrimeCheck 這個 subroutine, 要怎麼利用迴圈撰寫這個程式呢?
- 質數: http://goo.gl/gfLZT
- 尋找大質數: http://goo.gl/gPbo9
- 質數的故事: http://goo.gl/dV09I
- 質數產生器: http://goo.gl/zOFlm