Sub - End Sub

Sub subname ( [parameter-list] )

[statement(s)]

[Exit Sub]

[statement(s)]

End Sub

A Sub procedure is a series of nuBasic statements enclosed by the Sub and End Sub statements.

The Sub procedure performs a task and then returns control to the calling code, but it does not return a value to the calling code.

Corresponding to each parameter in the parameter-list is an argument in the procedure call of the argumentlist.

Calling syntax:

subname [argument-list]

When you call a procedure that accepts one or more parameters you can pass the arguments by position, in the order in which the related parameters appear in the procedure's definition.

Example:

Sub print_number ( number% )

If number% > 10 Then Print "Exit here..." : Exit Sub

Print "Number:"; number%

End Sub

print_number 20 div 4

print_number 20 * 4

Output:

Number:5

Exit here...

In the following example, the procedure studentInfo(name$,age%) is called with its arguments passed by position and delimited by commas:

' Main Program

studentInfo "John", 20

studentInfo "Mary", 22

' Procedures...

Sub studentInfo(name$, age%)

Print "Name = " + name$ + " age: " + Str$(age%)

End Sub

Output:

Name = John age: 20

Name = Mary age: 22

See also Function