Subs
Previous Top Next

All program code (including global variables definitions) is written inside Subs.
Subs declaration starts with [Public|Private] Sub SubName (Parameters) [As ReturnType] and ends with End Sub.
The parameters syntax include zero or more parameters with the following format:
[ByVal | ByRef] Name [As ParameterType].
If there are no parameters for the sub, then write "Sub SubName" without the parenthesis.
Parameters are passed by default by value (ByVal), which means that the value is copied and changes done inside the Sub will not affect the actual variable used in the call.
It is also possible to pass variables by reference (ByRef). In that case changes to the parameter will apply also to the actual variable used in the call. It can also be used to return multiple values from a Sub.
There are no functions in Basic4ppc but instead every sub can return a value with the keyword "Return"
It is possible to declare the type of the return value by adding the type to the Sub's declaration.
There are two special subs:
1.   Globals - Inside this sub you can declare global variables.
Any other variable you will declare elsewhere will be local.
(Global means that the variable can be used anywhere, unlike local that of which can be used only in the same sub.)
2.   App_Start - This is the first sub that executes when the program loads.
If no form is shown at the end of this sub execution then the program will end.

Subs and events

There are three options to connect an event to a sub:
1.   Use the event menu in the Designer, and the correct sub will be added automatically.
2.   Write a sub whose name is the control's name  (the control that will trigger the event) and the event name joined by an underscore.
If the event has parameters then add them too (Their names are not important).
Example: Sub Button1_Click
Example: Sub Form1_MouseDown (px,py)
3.   Use the AddEvent keyword.

Calling a sub

To call a sub, write its name followed by parenthesis with the parameters.
If there are no parameters then do not write any parenthesis.
Example:

Sub Button1_Click
            Msgbox("The mean of 20 and 30 is " & crlf &  Mean(20,30))
End Sub

Sub Mean (a,b)
            Return (a+b)/2
End Sub

Result: A Msgbox that displays 25 when the button is pressed.

See Modules topic for more information about the Public and Private access modifiers.