MS Small Basic第九章: Subroutines(副程式)

Routine在一般英文的意思是 “例行公事" , 程式裡面的 “例行公事" 就是 program (主程式), 而 “在例行公事" 之下的–也就是主程式之下的–就是 subroutine, 一般翻譯成 “副程式" . 主程式(program)的程式碼在程式開始時就會自動執行, 而副程式(subroutine)不會自動執行自己的程式碼, 它必須被 “呼叫" (call), 才會執行程式碼.

在程式設計時經常遇到需要重覆執行同一組步驟的情況, 在這種情況下, 一直重複寫同一段程式碼是非常腦殘的事情, 所以我們需要副程式, 每當要用到的時候只要呼叫(call)一下就可以.

今天的MS Small Basic學習以使用手冊第九章 Chapter 9: Subroutines 以及初學者開發人員學習中心的線上教材 Lesson 1.5: Branching and Subroutines 為學習重點. 首先先來複習一下 Branching.

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.

第7行到第9行就是一個簡單的subroutine

每次需要用到這個副程式的時候只要呼叫 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.

使用變數(Variables):

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. (以變數 xy 的值為圓心座標值不斷畫圓. 這邊有用到三角函數正弦(sin)餘弦(cos), 再配合座標角度, 這部分我已經還給高中數學老師了, 所以看範例的時候不清楚為什麼要設那個數字, 也無法在心中先有個圖形的輪廓, 這部分只好傻傻按照教材範例打)

在迴圈(Loops)內調用副程式

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. (質數判斷程式)

20除了1和它本身以外還有許多因數, 所以當然不是質數

國中數學有教, 1到100以內最大的質數是97

隨便打一個很大的數字, 結尾用奇數試試看, 因為用偶數就絕對不是質數, 至少有 2 這個因數

如果有一個任務是 “列出100以內的所有質數" , 並調用 PrimeCheck 這個 subroutine, 要怎麼利用迴圈撰寫這個程式呢?

關於質數的文章:

  • 質數: http://goo.gl/gfLZT
  • 尋找大質數: http://goo.gl/gPbo9
  • 質數的故事: http://goo.gl/dV09I
  • 質數產生器: http://goo.gl/zOFlm
廣告

About leeoxygen

吱吱喳喳,嚕嚕啦啦,嗯嗯啊啊.
本篇發表於 我的Small Basic學習記錄 並標籤為 , 。將永久鏈結加入書籤。

發表迴響

在下方填入你的資料或按右方圖示以社群網站登入:

WordPress.com Logo

您的留言將使用 WordPress.com 帳號。 登出 / 變更 )

Twitter picture

您的留言將使用 Twitter 帳號。 登出 / 變更 )

Facebook照片

您的留言將使用 Facebook 帳號。 登出 / 變更 )

Google+ photo

您的留言將使用 Google+ 帳號。 登出 / 變更 )

連結到 %s