Passing Arguments by Value and by Reference
In ViviFire, you can pass an argument to a procedure by value or by reference. This is known as the passing mechanism. It controls if the procedure can change the object that is the argument passed by the caller. The procedure declaration controls the passing mechanism for each parameter with the keyword ByRef.
Qualities of arguments and parameters
When you pass an argument to a procedure, there are many risks to think about.
- Can the data be changed?
- Can the argument be changed?
- Is the argument passed by value or by reference?
- Is the argument's data type a value type or a reference type?
Selection of passing mechanism
Select the passing mechanism carefully for each argument.
- Protection
- When you select a passing mechanism, The most important decision is how much risk of change is permitted to variables that you pass as arguments.
When you pass an argument by reference(
ByRef
), the procedure can return a value to the caller through that argument. But when you pass an argument by value, the procedure cannot change the value of the variable. - Performance
- Although the passing mechanism can have an effect on the performance of your code, the difference is usually very small.
But one category of variable usually does worse when passed by value.
For large value types, for example, structures, ViviFire copies all of the data contents of the argument.
Thus, for a large value type,
ByRef
can give better performance. - For reference types, only the pointer to the data is copied (four bytes on 32-bit platforms, eight bytes on 64-bit platforms).
Thus, you can pass arguments of type
String
or objects by value, and it does not decrease performance.
Control the passing mechanism
The procedure's declaration controls the passing mechanism for each parameter. The caller cannot override the passing mechanism “by value”.
If a parameter is declared with ByRef
, the caller can call by value if you write the argument between parentheses.
The default passing mechanism in ViviFire is by value.
When to pass by value
TODO
When to pass by reference
TODO
Example
TODO