WinBatch Tech Support Home

Database Search

If you can't find the information using the categories below, post a question over in our WinBatch Tech Support Forum.

TechHome

UDF - UDS Library
plus
plus
plus
plus
plus
plus
plus
plus
plus
plus
plus
plus
plus
plus
plus
plus
plus
plus
plus
plus
plus
plus
plus

Can't find the information you are looking for here? Then leave a message over on our WinBatch Tech Support Forum.

Passing Variables in UDFs

Keywords: 	 passing variables UDFs

Question:

I understand that one cannot use a variable defined elsewhere in a WIL script inside a UDF, but I was wondering what the rationale for that design is... why must you pass the variable to the UDF as one of the parameters in the UDF?

Answer:

UDFs are not subroutines but rather true functions with no dependencies. The idea is that UDFs execute independently and can be used in any script whatsoever without being affected by local variables defined in the script, unless those variables are passed directly to the UDF as a parameter.

Variables can be passed to a UDF in one of two ways:

  1. When you pass an array name (ie, not an array element) as a parameter to a function, the array gets passed "by reference". That is, the function receives a pointer to the array, and is therefore able to make changes to it "in place". This is similar to passing a binary buffer handle to a function, where the function is then able to make wholesale changes to the binary buffer. In the case of arrays and binary buffers, variables are passed by reference.

    When a UDF is called that uses the value in an array or binary buffer, if that array or binary buffer has been changed by the calling program, any changed values in the array or binary buffer will be seen and used by the UDF. In other words, variables passed by reference are dynamic and global, therefore a UDF will have access to the array or binary buffer variables in the main calling WIL script.

  2. In contrast, passing an array element (ie, with a subscript) to a function is like passing a regular string or integer parameter to a function -- it gets passed "by value". i.e., the function receives the value of the array element, but is not able to modify the array itself.

    Variables passed by value are local variables used within the UDF. A UDF will not have access to any variables in the main WIL script, other than the variables passed as param1 - param16. Any variables set in a UDF will be destroyed when the UDF returns, other than the return value of the UDF.


Article ID:   W14994
File Created: 2001:11:08:12:41:18
Last Updated: 2001:11:08:12:41:18