21.08.2013 Views

OpenOffice.org BASIC Guide.pdf - OpenOffice.org wiki

OpenOffice.org BASIC Guide.pdf - OpenOffice.org wiki

OpenOffice.org BASIC Guide.pdf - OpenOffice.org wiki

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

Procedures and Functions 34<br />

Passing Parameters<br />

Functions and procedures can receive one or more parameters. Essential parameters must<br />

be enclosed in parentheses after the function or procedure names. The following example<br />

defines a procedure that expects an integer value A and a string B as parameters.<br />

Sub Test (A As Integer, B As String)<br />

' ...<br />

End Sub<br />

Parameters are normally passed by Reference [1] in <strong>OpenOffice</strong>.<strong>org</strong> Basic. Changes made<br />

to the variables are retained when the procedure or function is exited:<br />

Sub Test<br />

Dim A As Integer<br />

A = 10<br />

ChangeValue(A)<br />

' The parameter A now has the value 20<br />

End Sub<br />

Sub ChangeValue(TheValue As Integer)<br />

TheValue = 20<br />

End Sub<br />

In this example, the value A that is defined in the Test function is passed as a parameter to<br />

the ChangeValue function. The value is then changed to 20 and passed to TheValue, which<br />

is retained when the function is exited.<br />

You can also pass a parameter as a value if you do not want subsequent changes to the<br />

parameter to affect the value that is originally passed. To specify that a parameter is to be<br />

passed as a value, ensure that the ByVal keyword precedes the variable declaration in the<br />

function header.<br />

In the preceding example, if we replace the ChangeValue function then the superordinate<br />

variable A remains unaffected by this change. After the call for the ChangeValue function,<br />

variable A retains the value 10.<br />

Sub ChangeValue(ByVal TheValue As Integer)<br />

TheValue = 20<br />

End Sub

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!