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

Chapter 11: Writing Large Programs by Using Subprograms 147

Writing Modular Programs

If you’re really ambitious, you can write a large program as one huge list of instructions. The larger your program is, however, the harder reading, writing, and understanding the program become. Writing a large program as one set of instructions is like trying to build a house out of sand. Most likely, one part of the structure (such as a wall) is weaker than the rest of the structure and will eventually cause the whole thing to collapse.

Rather than create one huge program, programmers create a bunch of smaller programs and paste them together (sort of like using bricks to build a house). That way, if one part of the program doesn’t work, you can unplug that portion, rewrite or replace it, and leave the rest of your program unaffected.

In the computer world, little programs that make up part of a larger program are known as subprograms. Subprograms are also known as modules, hence the term modular programming.

Every modular program contains at least one subprogram, known as the main program. The main program usually does nothing more than tell the computer which subprograms to use next to accomplish a specific task.

A subprogram typically solves a single task, such as multiplying two numbers or verifying that the user types a correct password. For really complicated programs, you may have several subprograms that themselves break down into several smaller subprograms.

Suppose, for example, that you want to write a program to break into another computer. The overall task is simply as follows:

Break into another computer.

Of course, you can’t tell the computer just to break into another computer because it doesn’t know how to do it. You must tell the computer, in specific detail, exactly how to do what you want it to do. This task means defining the overall goal of the program by using smaller tasks, as follows:

1.Find the phone number of the target computer.

2.Guess a password to access the system.

3.After gaining access, beep to notify the user.

Ideally, you can solve each task by using a separate subprogram that’s completely independent of any other part of the program. After you get each subprogram to work correctly, you can paste all the subprograms together to create a larger working program.

148 Part III: Advanced Programming with Liberty BASIC

Programmers use subprograms for the following two reasons:

To make writing large programs easier: By writing a bunch of small programs that perform a specific task, you can paste together a bunch of subprograms to make a much larger program.

To store repetitive instructions in a single location: Sometimes you need to run the same set of instructions over and over again. Rather than write these instructions each time that you need them, you can write them just once and store them in a subprogram that any other part of your program can run at any time.

The main advantage of breaking a large program into subprograms is so that you can easily modify a large program just by modifying one or more of its smaller subprograms. Because small programs are easier to understand and modify than large ones, subprograms can help make your program more reliable.

To create a subprogram in Liberty BASIC, you need to perform the following tasks:

Write any subprogram instructions after the END command in your main program.

Identify the start of your subprogram with a unique name, enclosing it in square brackets [like this].

Identify the end of your subprogram by using the RETURN command, which tells the computer to return back to the part of the program that was running before the subprogram ran.

You can choose any name for your subprogram, but a good idea is to choose a descriptive name. If your subprogram prints a warning message to the user, for example, you may want to name your subprogram [warning].

The RETURN command identifies the end of your subprogram and tells the computer to return back to the main program.

So a typical subprogram may look as follows in Liberty BASIC:

Main program instructions

Main program instructions

END

[subprogram] Subprogram instructions Subprogram instructions RETURN

At this point, the computer totally ignores your subprogram because the computer starts at the top, beginning with the first instruction, and follows

Chapter 11: Writing Large Programs by Using Subprograms 149

each succeeding instruction until it reaches the END command, which tells the computer to stop running the program before it can reach any of the instructions that your subprogram stores.

So if you want the computer to run your subprogram instructions, you must use the GOSUB command, such as in the following line:

GOSUB [Subprogram name]

This command does nothing more than tell the program to jump to the subprogram that the label in brackets identifies. If you want your subprogram to run, you must insert a GOSUB command somewhere in your main program as follows:

Main program instructions1

GOSUB [subprogram]

Main program instructions2

END

[subprogram] Subprogram instructions Subprogram instructions RETURN

In the above example, the Main program instructions1 run and then the GOSUB command starts the subprogram running. After the subprogram instructions run, the RETURN command returns the computer to the line immediately following the GOSUB command. This makes the computer run the Main program instructions2 until it stops at the END command.

To see a real program that you can type and try yourself, take a look at the following example:

NOMAINWIN

PROMPT “What is your password?”; password$

IF password$ = “open” THEN

NOTICE “You typed a valid password.”

ELSE

NOTICE “Invalid password.”

GOSUB [hackeralert]

END IF

END

[hackeralert]

PROMPT “Are you a hacker? (Y or N)?”; answer$ IF answer$ = “Y” THEN

NOTICE “The police are on their way to pick you up now.” ELSE

NOTICE “Then you must just be incompetent.” END IF

RETURN