bas7advr.hlp (Topic list)
Important Notice
The pages on this site contain documentation for very old MS-DOS software, purely for historical purposes. If you're looking for up-to-date documentation, particularly for programming, you should not rely on the information found here, as it will be woefully out of date.
SUB Statement Details
  Syntax  Details  Example                 Contents  Index  Back
──────────────────────────────────────────────────────────────────────────────
The SUB statement declares the name and the parameters of a SUB procedure.
 
SUB globalname[(parameterlist)] [STATIC]
  [statementblock]
[EXIT SUB]
  [statementblock]
END SUB
    ■ Names in the parameterlist are separated by commas. The syntax
      of parameterlist is:
 
       [BYVAL] variable[( )] [AS type] [, [BYVAL] variable[( )] [AS type]]...
 
            ■ BYVAL       Defines the variable as being passed by value
                          rather than by reference (the default). BYVAL
                          can be used only with simple numeric types
                          (INTEGER, LONG, SINGLE, DOUBLE, or CURRENCY).
            ■ variable    A BASIC variable name. Previous versions of BASIC
                          required the number of dimensions in parentheses
                          after an array name. In the current version of
                          BASIC, only empty parentheses are required.
            ■ AS type     The type of the variable: INTEGER, LONG, SINGLE,
                          DOUBLE, STRING, CURRENCY, or a user-defined
                          type. You cannot use a fixed-length string
                          as a parameter unless it is contained in an
                          array. You can use a simple fixed-length
                          string as an argument in a CALL statement;
                          BASIC converts a simple fixed-length string
                          argument to a variable-length string argument
                          before passing the string to a SUB procedure.
 
    ■ STATIC indicates that the SUB's local variables are to be saved
      between calls. Without STATIC, the local variables are allocated
      each time the SUB is invoked, and the variables' values are lost
      when the SUB returns to the calling program. The STATIC attribute
      does not affect variables that are used in a SUB but declared
      outside the SUB in DIM or COMMON statements using the SHARED
      statement.
    ■ EXIT SUB immediately exits a SUB procedure. Program execution
      continues with the statement after the CALL statement. EXIT SUB can
      be used only in a SUB procedure.
 
Usage Notes
    ■ A SUB procedure is a separate procedure, like a FUNCTION procedure.
      However, unlike a FUNCTION procedure, a SUB procedure cannot be used
      in an expression.
    ■ SUB and END SUB mark the beginning and end of a SUB procedure. You
      also can use the optional EXIT SUB statement to exit a SUB procedure.
    ■ SUB procedures are called by a CALL statement or by using the
      SUB procedure name followed by the argument list. See the
      CALL Statement (BASIC Procedures).
    ■ BASIC SUB procedures can be recursive--they can call themselves to
      perform a given task.
    ■ The STATIC attribute indicates that all variables local to the
      procedure are static--their values are saved between calls. STATIC
      is not usually used with recursive SUB procedures.
    ■ Any SUB procedure variables or arrays are considered local to that
      SUB procedure, unless they are explicitly declared as shared
      variables in a SHARED statement.
    ■ You cannot define SUB procedures, DEF FN functions, or FUNCTION
      procedures inside a SUB procedure.
    ■ Earlier versions of BASIC did not allow you to pass arrays
      containing fixed-length strings as parameters. BASIC now
      supports this.
    ■ If a parameter is passed by reference, any change to the parameter's
      value inside the procedure changes its value in the calling program.
    ■ If a parameter is passed by value, any changes to the parameter's
      value inside the procedure is local to that procedure and does not
      affect its value in the calling program.
 
Important
     ■ You cannot use GOSUB, GOTO, or RETURN to enter or exit a SUB
       procedure.