Macros and the IBM CognosScript Language  7.5.0
IBM CognosScript Statements and Functions >

Sub...End Sub Statement

Description

Defines a subprogram procedure.

Syntax

[ Static ] [ Private ] Sub name [ ( [Optional ] parameter [ As type] , ...) ] End Sub

where:

is:

name

The name of the subprogram.

parameter

A comma-separated list of parameter names.

type

A data type for parameter

Comments

A call to a subprogram stands alone as a separate statement. (See the Call statement). Recursion is supported.

The data type of a parameter can be specified by using a type character or by using the As clause. Record parameters are declared by using an As clause and a type that has previously been defined using the Type statement. Array parameters are indicated by using empty parentheses after the parameter. The array dimensions are not specified in the Sub statement. All references to an array within the body of the subprogram must have a consistent number of dimensions.

If a parameter is declared as Optional, its value can be omitted when the function is called. Only Variant parameters can be declared as optional, and all optional parameters must appear after all required parameters in the Sub statement. The function IsMissing must be used to check whether an optional parameter was omitted by the user or not. See the Call statement for more information on using named parameters.

The procedure returns to the caller when the End Sub statement is reached or when an Exit Sub statement is executed.

The Static keyword specifies that all the variables declared within the subprogram will retain their values as long as the program is running, regardless of the way the variables are declared.

The Private keyword specifies that the procedures will not be accessible to functions and subprograms from other modules. Only procedures defined in the same module will have access to a Private subprogram.

Basic procedures use the call by reference convention. This means that if a procedure assigns a value to a parameter, it will modify the variable passed by the caller.

The MAIN subprogram has a special meaning. In many implementations of Basic, MAIN will be called when the module is "run". The MAIN subprogram is not allowed to take arguments.

Use Function to define a procedure that has a return value.

Example

This example is a subroutine that uses the Sub...End Sub function.

Sub main
	MsgBox "Hello, World."
End Sub

See Also

Call Statement

Dim Statement

Function...End Function Statement

Global Statement

Option Explicit Statement

Static Statement