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

Chapter 11: Writing Large Programs by Using Subprograms 155

Note that the EXIT SUB command simply tells Liberty BASIC to stop running the function and ignore any commands that come after the EXIT SUB command. In the above example, the EXIT SUB command has a 50 percent chance of running.

Using Functions

A function is a specialized subprogram that does nothing but calculate and return a single value. If you want to calculate the cube of a number (which is the same number multiplied by itself three times), for example, you can use the following instructions:

Cube = Number * Number * Number

But if you must calculate the cube of a number in several places throughout your program, you must type the formula Number * Number * Number in each place in your program. As you know, this practice is tedious and likely to create errors, especially if you want to modify the program later.

Defining a function

Rather than type similar instructions multiple times (using slightly different numbers), you can store instructions in a function. Then you pass data to the function, and the function spits back a single value. A function consists of the following four parts:

A name

One or more instructions for the function to calculate a single value

One line that assigns a value (or an expression that represents a value) to the function name

Any data that you want the function to use

A typical function in Liberty BASIC looks as follows:

FUNCTION FunctionName(Data)

One or more instructions

FunctionName = value

END FUNCTION

Don’t leave a space between the function name and the left parenthesis, or

Liberty BASIC doesn’t run your program.

156 Part III: Advanced Programming with Liberty BASIC

Passing data to a function

Nearly all functions need to receive data from another part of your program. As another part of a program runs (or calls) a function, it needs to send (or pass) data to that function. To make sure that the function receives any data that another part of the program passes to it, you need to specify one or more variables in parentheses, as in the following example:

FUNCTION FunctionName(Variable)

One or more instructions here

FunctionName = value

END FUNCTION

The preceding function can receive one chunk of data (such as a string or number) that another part of the program passes to it.

If your function needs to receive two or more chunks of data that another part of the program passes to it, just separate the variable names with commas, as in the following example:

FUNCTION FunctionName(Variable1, Variable2, Variable3)

One or more instructions here

FunctionName = value

END FUNCTION

Don’t forget to define the type of data that the function accepts — for example, strings or numbers. (See Chapter 8 for more information about strings.) A complete declaration of variables may look as follows:

FUNCTION FunctionName(Note$, Salary)

‘ One or more instructions here

FunctionName = value

END FUNCTION

The first line of the preceding function defines the name of the function (which is FunctionName) and creates two variables, Note$ and Salary. The Note variable represents a string, and the Salary variable represents a single-precision value such as 3.14.

Calling a function

Of course, you want to use the function that you create in your program. A function represents a single value, such as an integer or a double-precision number, so you can treat the function name as a variable.

Suppose, for example, that you have a function that you call Cube, as in the following example:

Chapter 11: Writing Large Programs by Using Subprograms 157

FUNCTION Cube(Number)

Cube = Number * Number * Number

END FUNCTION

You can treat this function as you do any other variable, as the following examples show:

PRINT Cube(3)

or

MyValue = Cube(3)

The following breakdown shows how the computer interprets the first example, PRINT Cube (3):

1.The PRINT Cube(3) line tells the computer to run the function named Cube and “pass” it the number 3 as data. Print on-screen whatever value the Cube function calculates using the number 3.

2.Right away, the computer searches for the function Cube. The first line in the Cube function tells the computer that the function’s name is Cube and it needs an integer, which the variable Number represents — and in this case, it represents the number 3.

3.The second line in the Cube function tells the computer to multiply the value that the Number variable represents three times and to assign

this value to the function name of Cube. In this case, the value that the Number variable represents is 3, so the value of Cube is 3 * 3 * 3, or 27.

4.The third line in the Cube function tells the computer it’s the end of the function, so go back to the part of the program that originally called the Cube function. In this case, the computer goes back to the instruction

PRINT Cube(3).

5.Because the value of Cube(3) is actually 27, the computer reinterprets the instruction PRINT Cube(3) to actually mean PRINT 27. Thus the number 27 appears on-screen.

The complete Liberty BASIC program that calls the Cube function might look like this:

NOMAINWIN

PROMPT “Type a number:”; mynumber

NOTICE “This is the cube = “; Cube(mynumber)

END

FUNCTION Cube(Number)

Cube = Number * Number * Number

END FUNCTION

158 Part III: Advanced Programming with Liberty BASIC

Exiting prematurely from a function

A function acts like a miniature program that runs from start to finish. However, there may be some cases when you want to exit a function before it finishes running. To do this, you just need to insert the magic EXIT FUNCTION command such as:

NOMAINWIN

PROMPT “Type a number:”; mynumber

NOTICE “This is the cube = “; Cube(mynumber)

END

FUNCTION Cube(Number)

EXIT FUNCTION

Cube = Number * Number * Number

END FUNCTION

Note that the EXIT FUNCTION command simply tells Liberty BASIC to stop running the function and ignore any commands that come after the EXIT FUNCTION command. For more flexibility, you may want to use an IF-THEN statement to determine if a function should stop prematurely, such as:

FUNCTION Cube(Number)

IF (Condition = TRUE) THEN EXIT FUNCTION

Cube = Number * Number * Number

END FUNCTION

The above example simply checks to see if a certain condition is true; then it exits the function prematurely, which means that sometimes the entire function may run and sometimes it may exit prematurely.

Passing Data by Value or by Reference

When a subroutine or function needs data, your main program can pass it data in one of two ways:

By value

By reference

Obviously these two terms mean nothing to most people, so the plain-English translation means that if you pass data to a subroutine or function by value, your program actually creates two separate copies of the same data; one copy stays with the main program and a second copy gets used by the subroutine or function itself. Anything the subroutine or function does to the data remains isolated within that subroutine or function.

Chapter 11: Writing Large Programs by Using Subprograms 159

In the following code, the main program passes the data enemy$ to the MakeMessage subroutine function by value (which is the default method that Liberty BASIC passes data to a subroutine or function).

If you type the name Bobby Lee at the Prompt dialog box, the main program stores the string Bobby Lee into the enemy$ variable. When the main program passes the data by value to the MakeMessage subroutine, the MakeMessage subroutine creates a second copy of that same data, but stores it in a new variable called stuff$. At this time, both the enemy$ and stuff$ variable contain the string Bobby Lee.

The MakeMessage subroutine then modifies the value of the stuff$ variable. But because the main program originally passed the data by value, these changes never get returned back to the main program, so when the main program displays the value of the enemy$ variable, it’s just the original string of Bobby Lee as shown, in Figure 11-1.

NOMAINWIN

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

CALL MakeMessage enemy$

NOTICE enemy$

END

SUB MakeMessage stuff$

stuff$ = “Bo the Cat hates “ + stuff$ + “ too!” END SUB

Figure 11-1:

When you pass data by value, a subroutine or function can alter data as much as it wants, but that altered data will never be returned back to the main program.

160 Part III: Advanced Programming with Liberty BASIC

If you pass data from the main program to the subroutine by reference, however, any changes made to the data within the subroutine or function will get passed back to the main program. To specify that you want a subroutine or function to get data passed to it by reference instead of by value, you have to use the magic BYREF command, such as:

SUB MakeMessage BYREF stuff$

Adding this single BYREF command to the DisplayMessage subroutine lets the subroutine pass any changes made to the data back to the main program, as shown in Figure 11-2.

NOMAINWIN

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

CALL MakeMessage enemy$

NOTICE enemy$

END

SUB MakeMessage BYREF stuff$

stuff$ = “Bo the Cat hates “ + stuff$ + “ too!” END SUB

Figure 11-2:

When you pass data by reference using the

BYREF command, any changes that the subroutine or function makes to the data get passed back to the main program.

Chapter 12

Drawing Pictures and

Making Noise

In This Chapter

Making a graphics control

Toying with turtle graphics

Drawing circles and boxes

Adding sound

In the old days, computer programs printed data on a single strip of paper that consisted of one long scroll. So after computers eventually got monitors, most programs still displayed information on-screen the same as if they were printing on a scrolling piece of paper. Only after monitors were around for a while did programmers realize that they could write programs that use

fancy graphics, color, and sound.

So in this chapter, you can learn different ways to make your Liberty BASIC programs more colorful and visually appealing. After you add graphics and sound to your program, people will naturally find your program more interesting and (sometimes) easier to use.

Creating a Graphics Control

Before you can create and display graphics, you must create a graphics control, which is any window or part of a window that can display graphics on the screen. You can create a graphics control in the following two ways:

Create a separate graphics window

Create a graphics box inside an existing window