I have always assumed that parameters for custom functions are passed strictly “by value”: if you point to a field, then that field’s value is what the custom function receives. Look at the example below. The custom function is very simple and just returns the value of the parameter. I am calling the custom function and pointing it to a field for the parameter. Works exactly like you’d expect it to, doesn’t it? The custom function just returns the parameter that we passed in.
Now let’s change the custom function to return two lines:
- the value of the parameter and
- a getfieldname( ) function call on the parameter
When looking at the result in the Data Viewer we see that the result is both the value and the field name.
That seems to indicate that the parameter is not really the value of the field, but a reference to the field itself. That means that inside the custom function we have access to a field’s value but also any other information we can ask for by knowing the field’s name (through the Design functions for instance).
Scripts can also receive a parameter. Does the same thing apply to scripts then? If I specify a field as the script’s parameter, do I get just the value or also the reference to the field? Let’s try. Below is a one-line script that displays a custom dialog with the result of using the getfieldname( ) function on the script’s parameter:
When we call that script from a button that passes in a field as the parameter, the message displayed is “?”.
It does not work here. The “?” is what you get when you use the getfieldname( ) function on something that is not a field. A script just receives the value of the field, not a references to the field. So script parameters are strictly “by value”, custom function parameters are “by reference” when the parameter points to a field. Food for thought…
In the next blog post I’ll show an example of how we can use that “by reference” behavior of custom functions to our advantage.
Side-note: you can use a field that is “out of context” and pass it to a custom function. The value will not be available but the field reference will.