I recently did some testing regarding variable passing and was a bit surprised at the following:
PASSING VARIABLES TO FUNCTIONS:
BY default, changes to SHARED variables (passed to functions via parameters) are not seen by the calling script. ~ BUT: If SHARED variables (declared in the main script) are changed inside a function (without being passed to that function via parameters) the changes will be seen outside the function. Because of this, it seems like shared variables are very risky to use in terms of debugging.
Also, when passing pointers to functions (via parameters) I cannot figure out how to pass pointers using a "BYVAL equivalent", with the exception of passing each element separately via individual parameters. (See notes at bottom.) ~ There must be some other/better way to do this?
In these regards, using functions apparently does little to help you isolate code! I've spent some time trying different code and techniques using the insight from the CSDA website but thus far haven't come up with anything to addressing these potential issues.
Notes:
SHARED: This is the minimum scope needed for functions to see (non-passed parameters) from calling scripts.
*BYREF & BYVAL: These declarations are ignored when passing a pointer (even user declared pointer variables) as parameters.
SHARED VARIABLES declared within the function are available outside the function after function is called.
GLOBAL and LOCAL (script embedded) functions appear to behave identical in terms of parameters and variable usage.
If a variable is declared as type SHARED (in the calling script), you are not forced to pass it into the function via parameters. ~ Potential for disaster.
PS: I have not yet tested the behavior of arrays in the context of this thread. I suspect they may behave similarly to pointers?
PASSING VARIABLES TO FUNCTIONS:
BY default, changes to SHARED variables (passed to functions via parameters) are not seen by the calling script. ~ BUT: If SHARED variables (declared in the main script) are changed inside a function (without being passed to that function via parameters) the changes will be seen outside the function. Because of this, it seems like shared variables are very risky to use in terms of debugging.
Also, when passing pointers to functions (via parameters) I cannot figure out how to pass pointers using a "BYVAL equivalent", with the exception of passing each element separately via individual parameters. (See notes at bottom.) ~ There must be some other/better way to do this?
In these regards, using functions apparently does little to help you isolate code! I've spent some time trying different code and techniques using the insight from the CSDA website but thus far haven't come up with anything to addressing these potential issues.
FYI: Here are some of the tests I did/confirmed:
SHARED variables ARE changed in calling script (unless sent to fn by parameter)
SHARED variables passed BYVAL ARE NOT changed in calling script.
SHARED variables passed UNSPECIFIED ARE NOT changed in calling script!
SHARED variables passed BYREF ARE changed in calling script.
LOCAL variables passed BYREF ARE changed in calling script.
LOCAL variables passed UNSPECIFIED ARE NOT changed in calling script?
SHARED variables ARE changed in calling script (unless sent to fn by parameter)
SHARED variables passed BYVAL ARE NOT changed in calling script.
SHARED variables passed UNSPECIFIED ARE NOT changed in calling script!
SHARED variables passed BYREF ARE changed in calling script.
LOCAL variables passed BYREF ARE changed in calling script.
LOCAL variables passed UNSPECIFIED ARE NOT changed in calling script?
Notes:
SHARED: This is the minimum scope needed for functions to see (non-passed parameters) from calling scripts.
*BYREF & BYVAL: These declarations are ignored when passing a pointer (even user declared pointer variables) as parameters.
SHARED VARIABLES declared within the function are available outside the function after function is called.
GLOBAL and LOCAL (script embedded) functions appear to behave identical in terms of parameters and variable usage.
If a variable is declared as type SHARED (in the calling script), you are not forced to pass it into the function via parameters. ~ Potential for disaster.
PS: I have not yet tested the behavior of arrays in the context of this thread. I suspect they may behave similarly to pointers?
Comment