Using Procedures and Functions
The following describes the basic use of procedures and functions in OpenOffice.org Basic.
Procedures (SUBS) and functions (FUNCTIONS) help you maintaining a structured overview by separating a program into logical pieces.
One benefit of procedures and functions is that, once you have developed a program code containing task components, you can use this code in another project.
Passing Variables to Procedures (SUB) and Functions (FUNCTION)
Variables can be passed to both procedures and functions. The SUB or FUNCTION must be declared to expect parameters:
SUB SubName(Parameter1 As Type, Parameter2 As Type,...)
Program code
END SUB
The SUB is called using the following syntax:
SubName(Value1, Value2,...)
The parameters passed to a SUB must fit to those specified in the SUB declaration.
The same process applies to FUNCTIONS. In addition, functions always return a function result. The result of a function is defined by assigning the return value to the function name:
FUNCTION FunctionName(Parameter1 As Type, Parameter2 As Type,...) As Type
Program code
FunctionName=Result
End Function
The FUNCTION is called using the following syntax:
Variable=FunctionName(Parameter1, Parameter2,...)
![]() | You can also use the fully qualified name to call a procedure or function: Library.Module.Macro() For example, to call the Autotext macro from the Gimmicks library, use the following command: Gimmicks.AutoText.Main() |
Passing Variables by Value or Reference
Parameters can be passed to a SUB or a FUNCTION either by reference or by value. Unless otherwise specified, a parameter is always passed by reference. That means that a SUB or a FUNCTION gets the parameter and can read and modify its value.
If you want to pass a parameter by value insert the key word "ByVal" in front of the parameter when you call a SUB or FUNCTION, for example:
Result = Function(ByVal Parameter)
In this case, the original content of the parameter will not be modified by the FUNCTION since it only gets the value and not the parameter itself.
![]() | When you create a new module, OpenOffice.org Basic automatically inserts a SUB called "Main". This default name has nothing to do with the order or the starting point of a OpenOffice.org Basic project. You can also safely rename this SUB. |
Scope of Variables
A variable defined within a SUB or FUNCTION, only remains valid until the procedure is exited. This is known as a "local" variable. In many cases, you need a variable to be valid in all procedures, in every module of all libraries, or after a SUB or FUNCTION is exited. This property is controlled in a variable declaration through key words in the Dim statement.
Declaring Variables Outside a SUB or FUNCTION
DIM GLOBAL VarName As TYPENAME
The variable is valid as long as the OpenOffice.org session lasts.
DIM PUBLIC VarName As TYPENAME
The variable is valid in all modules.
DIM PRIVATE VarName As TYPENAME
The variable is only valid in this module.
DIM VarName As TYPENAME
The variable is only valid in this module.
Saving Variable Content after Exiting a SUB or FUNCTION
DIM STATIC VarName As TYPENAME
The variable retains its value until the next time the FUNCTION or SUB is entered. The declaration must exist inside a SUB or a FUNCTION.
Specifying the Return Value Type of a FUNCTION
As with variables, include a type-declaration character after the function name, or the type indicated by "As" and the corresponding key word at the end of the parameter list to define the type of the function's return value, for example:
Function WordCount(WordText as String) as Integer
Index
procedures
functions,using
variables,passing to procedures and functions
parameters,for procedures and functions
parameters,passing by reference or value
variables,scope
scope of variables
GLOBAL variables
PUBLIC variables
PRIVATE variables
functions,return value type
return value type of functions
This help text is published from the OpenOffice-Help files under the Public Documentation License 1.0.














