Custom Search
 
  


Using predefined functions

 

Programming Intro

Chapter 5 Questions

 
CONSTRUCTING AND USING SUBROUTINES

Like functions, subroutines are designed so they can be used over and over within a program, or so they can be inserted easily into other programs.

A subroutine is defined as a small program within another program. It does not have to be given a name or begin with a particular keyword. Subroutines can be used when sets of instructions are to be performed several times in one or more programs. They can also be useful when more than one programmer is working on a program. Each programmer can be assigned a portion of the program to write, and that portion can be written as a subroutine. When all portions of the program have been written, they can be put together and referenced as subroutines in the main program. This reduces the possibility of statements written by one programmer conflicting with the statements written by another programmer.

To execute a subroutine, you must transfer control to the subroutine by using the keyword, GOSUB. Once executed, the subroutine transfers control back to the statement immediately following the GOSUB statement.

GOSUB and RETURN Statements

The GOSUB statement is used to transfer control to a subroutine. It is made up of a statement number followed by the keyword GOSUB and the number of the first statement in the subroutine.

The RETURN statement is used to transfer control back to the main program. It must be the last statement in the sub-routine. The RETURN statement consists of a statement number followed by the keyword RETURN. When the RETURN statement is executed, it transfers control back to the main program to the statement immediately following the GOSUB statement. The structure is as follows:

Line 30 transfers control to line 200, the first statement of the subroutine. Line 270, the last statement of the subroutine, returns control to line 40, the line immediately following the GOSUB.

The following example shows the use of the GOSUB and RETURN statements in transferring control to a subroutine and returning control back to the main program.

Example:

The GOSUB statement in line 70 transfers control to the subroutine, line 160, which prints a prompt to the user. At this point, the subroutine gives the user the option, either to continue running the program or to terminate it. After a string value is entered at line 180, the RETURN statement, line 190 returns control to the main program at line 80. This line tests the value of the string variable, X$. If the value equals Y, control is transferred to line 30; if the value is equal to N, the program will STOP.

STOP Statement

The STOP statement, line 90, terminates execution of the program. Although the STOP statement terminates execution of the program, it does not replace the END statement. Remember, the END statement has two functions: to terminate execution of the program, and to indicate there are no more instructions for the BASIC interpreter to translate. Therefore you must include an END statement in every program, regardless of how many STOP statements you use. STOP statements may appear anywhere you need them in a program, and you can use as many as you want.

Summary

Constructing loops is made easier by using the FOR-NEXT loop structure. A FOR-NEXT loop always begins with a FOR-TO statement and ends with a NEXT statement. A numeric variable, called a running variable is used to control the number of times a loop is executed. The running variable used in the NEXT statement must be the same as the one used in the FOR-TO statement. The value of the running variable is incremented by one each time the loop is executed, unless a STEP clause is used to alter this. Control can be transferred out of a loop but not into one.

A loop may have another loop inside it. This is called a nested loop. Each nested loop must be completely embedded within the outer loop. They cannot overlap. Each nested loop must begin with its own FOR-TO statement and end with its own NEXT statement. Control cannot be transferred into a nested loop from a point outside the nest.

When working with one- or two-dimensional arrays you may reference any element in them by using a subscripted variable. The DIM (DIMENSION) statement is used to define the size of an array. It reserves space in the computers memory for the specified number of elements.

Each array must be assigned a unique name, using either a numeric-variable name or a string-variable name. An array may contain either numeric or string data; however, all the elements in a given array must be of the same type (all numeric or all string).

Some of the more common mathematical functions have been predefine by the BASIC language; however, there are times when you may need to define your own. This can be done by using the DEF (DEFINE) statement.

A subroutine is a small program inside a larger program. It is useful when sets of instructions are to be performed several times in one or more programs, or when several programmers are working on the same program. A subroutine is executed by transferring control to the subroutine using a GOSUB statement with the statement number of the 1st statement in the subroutine. It is terminated by the keyword RETURN. When a RETURN statement is executed, control is transferred from the subroutine back to the main program to the statement immediately following the GOSUB statement.

Execution of a program may be terminated anywhere in a program by the use of STOP statements.







Western Governors University
 


Privacy Statement - Copyright Information. - Contact Us

Integrated Publishing, Inc. - A (SDVOSB) Service Disabled Veteran Owned Small Business