Добавил:
Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
Beginning Programming for Dummies 2004.pdf
Скачиваний:
109
Добавлен:
17.08.2013
Размер:
8.05 Mб
Скачать

154 Part III: Advanced Programming with Liberty BASIC

Liberty BASIC interprets the preceding program as follows:

1.The first line prevents the main window from appearing.

2.The second line displays a Prompt dialog box that reads, Give me the name of someone you hate:. After the user types a name, the enemy$ variable stores this name.

3.The third line calls the subroutine DisplayMessage and “passes” it the data that the variable enemy$ stores. When the subroutine Display Message runs, it runs all the lines of the program starting from line five (SUB DisplayMessage stuff$) to line 12 (END SUB).

4.The fourth line ends the program.

5.The fifth line is the start of the DisplayMessage subroutine that accepts a string and stores it in the variable stuff$.

6.The sixth line creates a random number, either 1 or 2, and stores this value into the variable X.

7.The seventh through eleventh lines display two different Notice dialog boxes, using the name that the stuff$ variable stores.

8.The twelfth line marks the end of the subroutine. When the computer reaches this line, it returns back to the main program starting with the line immediately following the CALL DisplayMessage command. In this case, it returns the computer back to line four, where the program ends.

Exiting prematurely from a subroutine

Normally a subroutine runs from start to finish. However, you may want to exit a subroutine before it finishes running. To do this, you just need to insert the magic EXIT SUB command such as:

NOMAINWIN

PROMPT “Give me the name of someone you hate:”; enemy$

CALL DisplayMessage enemy$

END

SUB DisplayMessage stuff$

X = INT(RND(1) * 2) + 1

IF X = 1 THEN

EXIT SUB

ELSE

NOTICE stuff$ + “ must be a moron.”

END IF

END SUB