next up previous
Next: 20. Arrays Up: VisualBasic Previous: 18. Functions


19. Subroutines

Subroutines are similar to Functions (see [*]) in that they are a separate piece of code that can be refered to from elsewhere in the program. However, unlike Functions, they do not return a value to the calling part of the code.

There are some cases where the same piece of code is performed on numerous occasions, but where a value is not passed back to the main program. Consider the following example.

Input "Enter two numbers",a,b
Call Equal(a,b)
.
.
.

Sub Equal(a,b)

If a = b Then
   Print "a and b ARE EQUAL !!!!"
End If

End Sub

In this program a Subroutine called Equal is called which then performs some instructions. When the computer reaches the End Sub command it returns to the main program just after the Call Equal(a,b) line. As you can see, no value has been passed between the subroutine and the main program, unlike the case of Functions (see [*]).

Here is an example programme using a Subroutine to swap two numbers around.

A Subroutine is a separate, self-contained piece of code which can be called, or refered to by another part of the program, and which does not return a value to the calling part of the program. They are very useful when the same piece of code is used many times. They are also useful to structure your programme (i.e. make it easier for someone else to understand).


next up previous
Next: 20. Arrays Up: VisualBasic Previous: 18. Functions
Chris Allton 2006-10-27