Variable Scope in WinBatch
Keywords: Common Global Local Variable Pointer Scope Space Context Program Function Level User Defined Function Subroutine UDF UDS Array
Question:
In the language that I am used to, I can create a "COMMON" statement where the data would be common to a main program and also within subroutines called by that main program.
ie COMMON D(20) ;an array
ie COMMON QTY,DESC,PRICE,etc
If I use the common statement in the beginning of the main program and any of its subroutines, the data is passed back and forth without any special attention by me.
If I set QTY to null and call the sub and the sub changes the QTY to 1,the main program sees the new QTY after the return of the subroutine. Do you have a function that would do this?
Answer:
Variable Scope in WinBatch
Each executing WinBatch program has its own variable space, or "scope". Many times before you use a variable it must be defined. You can not define a variable at any spot in the program and expect to use it in any spot, there is
a specific relationship between where a variable is defined and where it can be used. This is known as the Scope of the variable.
- Scope
The context within which a variable is defined.
- Program-Level ('global') Scope
The top level context of the program. Variables that are defined at the top level of the program (outside of any User Defined Function) have program-level scope. For example, all variables in the main code, User Defined Subroutines,
subroutines and called scripts have program-level scope.
- Function-Level ('local') Scope
The context of a User Defined Function. Variables that are defined within the context of a user defined function have function-level scope. For example, the all variables that are only accessible within that same user defined function have function-level scope.
Variables that are declared at the top level of the program (outside of any User Defined Functions) have program-level or "global" scope. They are visible from all top-level (program-level) code, from any User Defined Subroutines called from top-level code, and from any external scripts called from top-level code using the "Call" function. These all share the same variable space, so that if you declare a variable "x" and then call a User Defined Subroutine, the User Defined Subroutine can change the value of "x" and it will keep its new value even after the User Defined Subroutine has completed.
Each User Defined Function executing within a WinBatch program has its own variable space, with function-level or "local" scope. A User Defined Function does not see variables declared in the program (or User Defined Function)
that called it, so a variable "x" declared in a User Defined Function is a different variable than the variable "x" declared in the program above it, and they can each have different values. Local variables within a User Defined Function
are visible from within the User Defined Function itself, from any User Defined Subroutines called from that User Defined Function, and from any external scripts called from that User Defined Function using the "Call" function.
- A User Defined Subroutine shares the same variable space as the code that called it. An external script called using the "Call" function also shares the same variable space as the code that called it.
- A User Defined Function has its own variable space, and does not share the same variable space as the code that called it.
In conclusion, scope basically refers to the visibility of variables. In other words, which parts of your program can see or use it. Normally, every variable has program-level scope. Once defined, every part of
your program can access a variable (except for UDFs).
Also WinBatch offers the ability to:
- Create Global Variables to be used with User Defined Functions using Pointers: http://techsupt.winbatch.com/webcgi/webbatch.exe?techsupt/nftechsupt.web+Tutorials+Pointers.txt
- Create Arrays that can be passed “by reference” to User Defined Functions.
Article ID: W18424
Filename: Variable Scope in WinBatch.txt
File Created: 2014:07:18:09:51:40
Last Updated: 2014:07:18:09:51:40