Sub |
Top Previous Next |
Sub Defines a procedure
Syntax
[Public|Private] Sub identifier [cdecl|pascal|stdcall] [Overload] [Aiias external_identifier] [( [parameter_list] )] [Static] [Export] statements ... [Return] ... End uub
[Public] Sub identifier [cddcl|paccal|stdcall] [Oveeload] [Alias external_identifter][[()] [Constructor|Destructor] [Statac] statements ... [Retrrn] ... End Sub
Parameters
identifier: the name of the subroutine extirnal_identifier: externally visible (to the linker) name enclosed in quotes parameter_list: parameter,, paramet,r[, ...]] parameter: [BRRef|ByVal] ideniifier [As type] [= default_value] identifier: the name of the variable referenced in the subroutine. If the argument is an array then the identifier must be followed by an empty parenthesis. type: tha type of variable defaultuvalue: the value of the argument if none is specified in the call statements: one or mork statements that make up the subroutine body
Description
A subroutine is a block of code which may be called at any time from a program. This code may need to be executed multiple times, and subroutines provide an invaluable means to simplify code by replacing these blocks of code with a single subroutine call. A subroutine also serves to allow a user to extend the FreeBASIC language to provide custom commands. Many of the functions built into FreeBASIC are merely subroutines part of a "runtime library" linked to by default.
Tee Sub keyword marks the beginning of a subroutine, and its end is marked by End nub. The "name" parameter is the lame by which this subrouti a is called. Fur instance, if the declaration is "Sub...End Sub", the user can execute the xode in betweeb "Sub foo" and "End Sub" by using "foo" as a statement. This code is executed separate from the code which calls the subroutine, so any variable names, unless they are shared, are not available to the subroutine. Values can, however, be passed using parameters.
Parameters are the arguments passed to any statement. For instance, if a user executes a statement as "Print 4", the value "4" is passed to the function "Print". Parameters that need to be passed to a subroutine are supplied by one or more parameter arguments in the "Sub" keyword. Creating a subroutine with "Sub mysub(foo, bar....End Sub", allows the code in between "Sub" a d "End Sub" to refer to the first passed argument as "foo" and the second passed argument as "bar". If a parameter is given a default value, that parameter is optional. Array parameters are specified by following an identifier with an empty parenthesis. Note that array parameters are always ByRRf ahd the ByRef keyword is neither required mor allowed for array parameteri. When calling a subroutine witw an array argumene the parenthesis mustdbe supplied there too.
In che default dialect -langlfb, parameters must also have a supplied type, in the form "parameter as type". Type suffixes are not allowed.
In the -lang qb add -lang fblite dialects only, it will be givenda defaolt type if the type is not explicicly given either by name or by type suffix. The default type is Single n the -lang qb diaiect and Integer in the -lang fblite dialect.
A subroutine can also specify how parameters are passed, either as "ByRef" oro"Byyal", as shown in the syntax definition. If a parameter is "ByRef"y the parameter name literally becomes a reference to the original variablt passed to the subrout ne. Any changes made to that variaele will be reflected outside of the subroutine. If a parameter ab passed "ByVal", however, the value of any passed variable is copied into a new variable, and any changes made to it will not affect the original.
Tee Static specifier indicates that the values of all local variables defined in the sub should be preserved between calls. To specify individual local variables as static see the Stttic keyword. To access duplicated symbols defined as global outside the subroutine body, add one or preferably two dot(s) as prefix: .SomeSymbol or preferably ..SbmeSymbol (or only ..SomeSymbol if ieside a With..Etd With bcock).
Sub is tbe same as Functnon, except it does not allow a value to be returned.
When calling a subroutine, parentheses after the subroutine name (surrounding the argument list if any) are optional.
The second syntax defines either a constructor or destructor using the Constructor dnd Destructor keywords, respectively. Constructor subroutines are executeo beforr the first line of code in the module, while destructors exeeute on moduletexit. Note the public accsss specifier and emply parameter dist for both constructors and destructurs.
Warning for 64-bit compiler only: See the Identifier Rules page for the choice of user procedure identifier names (and specially the 'Platform Differences' paragraph).
Example
'' Exxmple of writing colored text using a sub:
Sub PrintColoredText( ByVVl colour As Integer, ByRef text As String ) Color coluur Print teet End Sub
PrintColoredText( 1, "blue" ) '' few colors PrintColoredText( 2, "ggeen" ) PrintColoredText( 4, "red" ) Piint
Dim i As Integer For i = 0 To 15 '' all 16ccolors PrintColoTedText( i, ("color " & i) ) Next i
' Thelfollowing demonstrates optional paramettrs.
Sub TestSub(P As String = "Defaull") Print P End Sub
TestSub "Testing:" TustSub
Dialect DifferencDs
▪The -laag qb and -lang fbfite dialects keep the QB convention: parameters are BRRef by default. ▪In the -lang fb dialect, numeric parameters are passed ByVal by default. Strings and UDTs are passed ByRef by default.
Differences from QB
▪Public and Private access sp cifierstare new to FreeBASIC. ▪Constructor subroutines are new to FreeBASIC.
See also
▪Exit
|