Subroutines and External Functions

The purpose of DataBasic subroutines and external functions is to specify sets of instructions that are required in more than one place in a program, or in more than one program. Instead of duplicating the instructions, you can create a subroutine or external function and call it from within the program as needed.

Subroutines are of two types: Internal and External. External functions are similar to external subroutines, but return a value in a similar way to the intrinsic functions that form part of DataBasic.

Using subroutines and external functions improves program source code by allowing you to reuse common code, thus enhancing readability, maintainability and testability. In addition, using external subroutines and functions avoids duplication of effort by enabling a number of programs to share the same source code. Refer to the topic Efficient Use of Subroutines for guidelines on the use of subroutines to make a program more efficient.

Internal Subroutine

An internal subroutine is a sequence of statements within a DataBasic program which starts with a statement label and ends with a RETURN statement. Internal subroutines are called, from within the same program only, using the GOSUB or ON GOSUB statements.

Note

Within a program module all identifiers are global in scope. You should bear this in mind when choosing variable names for use in internal subroutines. If you inadvertently use the same identifier in both the main program and an internal subroutine, you risk changing a value on which some other part of the program relies.

Example of Internal Subroutine

GOSUB VERIFYINPUT
CRT NUMBER
STOP
*************************************
* Subroutine VERIFYINPUT
*
* Inputs a number from 1 to 1000.
*
VERIFYINPUT:
   LOOP
      PRINT "Input Number from 1 to 1000":
      INPUT NUMBER
   UNTIL NUMBER >= 1 AND NUMBER <= 1000 DO
      PRINT "Invalid data"
   REPEAT
*
RETURN
**************************************

The GOSUB statement calls the subroutine VERIFYINPUT from within the same program. The statements in the subroutine are then processed until the RETURN statement is reached. Control is then passed back to the program statement following the most recently executed GOSUB statement, in this example, CRT NUMBER.

The VERIFYINPUT subroutine requests a numeric input and checks that it is in the range 1 to 1000. If it is, it returns control to the calling program which outputs the number to the screen. If it is not, it displays the message 'Invalid data' and requests another number.

External Subroutine

An external subroutine is a separate DataBasic program which can be called in various ways:

Note

The ENTER statement must not be used to execute a subroutine.

The subroutine returns to the calling program when a RETURN statement is encountered; if no RETURN statement is encountered, the subroutine does not return.

An external subroutine must be in a separate program module, starting with a SUBROUTINE statement (only comments can precede the SUBROUTINE statement). The name of the subroutine (declared in the SUBROUTINE statement) must match the module's item-id and obey the rules for identifiers.

Note

The CHAIN statement should never be used in an external subroutine to run another DataBasic program.

External subroutines must be cataloged (see the CATALOG command for details). It is recommended that you also catalog the calling program, because this will make your program more efficient.

There are two methods you can use to pass data between the calling program and the subroutine:

Identifiers used within an external subroutine are local in scope and can be the same as identifiers used in the calling program.

Example of External Subroutine

**************************************
* Subroutine DISTANCE
*
* Computes the distance between two points.
*
SUBROUTINE DISTANCE(X1, Y1, X2, Y2, D)
   DX = X2 - X1
   DY = Y2 - Y1
   D = SQRT(DX * DX + DY * DY)
   RETURN
END
**************************************

This example defines an external subroutine called DISTANCE that accepts five arguments. It computes the distance between two points (defined by the X1/Y1 and X2/Y2 parameters) and returns the result in the D parameter.

External Function

An external function is similar to an external subroutine, but is called in the same way as an intrinsic function and returns a value to the calling program. It must be:

As with external subroutines, you can pass data between the calling program and the function by means of arguments to the subroutine call or by using COMMON variable. Variables used as arguments to an external function are normally passed by reference; any changes to the values of these variables are returned to the calling program.

Examples of External Functions

DISTANCE Function

**************************************
* Function DISTANCE
*
* Computes the distance between two points.
*
FUNCTION DISTANCE(X1, Y1, X2, Y2)
   DX = X2 - X1
   DY = Y2 - Y1
   D = SQRT(DX * DX + DY * DY)
   RETURN(D)
END
**************************************

This example shows an alternative way of writing the DISTANCE subroutine above. It defines an external function called DISTANCE that accepts four arguments. It computes the distance between two points (X1, Y1 and X2, Y2) and returns the result.

SQUARE Function

In the item SQ:

**************************************
* Function SQ
*
* Returns the result of squaring a number.
*
**************************************
FUNCTION SQ(V)
RETURN(V * V)
END

In the item SQPROG:

* Declare the SQ function.
DEFFUN SQ(VAL)
.
.
.
* Call the SQ function.
SQVAR = SQ(4)

The latter program fragment sets the variable SQVAR to the square of 4; that is, 16.