Performance Impact: By Reference or By Value

T

TheVisionThing

Assuming that you're not modifying a parameter within a procedure, is there
any performance difference in passing the parameter as 'ByRef' or 'ByVal'.

For example

Dim arr as variant
arr = Array(12,40)
arr = TimesTwo(arr)

Function TimesTwo(ByRef or ByVal arr as variant)
Dim i as integer,arrTemp as variant
Redim arrTemp(ubound(arr))
For i = lbound(arr) to ubound(arr)
arrTemp(i) = 2 * arr(i)
Next
TimesTwo = arrTemp
End Function

Also can arrays only be declared as variants in situations such as this one.

Thanks,
Wayne C.
 
T

Tom Ogilvy

You can only assign an array to a variant or (exl2000 and later) to an
dynamic variant array.

I would think passing byval would require copying the array and this would
incur a penalty.
 
T

TheVisionThing

Interesting info, Tom, thanks.

By dynamic variant array in Excel 2000 or later, did you mean:

Dim arr as variant
Redim arr(2,2)
Redim Preserve arr(2,50)

Where only the last dimension of the array can be altered.

Regards,
Wayne C.
 
T

Tom Ogilvy

No,
Dim arr() as Variant

as opposed to

Dim arr as Variant

However, there is no advantage to doing that and it just makes it
incompatible with xl97.
 
T

TheVisionThing

Understood. Thanks.

Regards,
Wayne C.

Tom Ogilvy said:
No,
Dim arr() as Variant

as opposed to

Dim arr as Variant

However, there is no advantage to doing that and it just makes it
incompatible with xl97.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top