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

Variables and Parameters

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

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.

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.

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:


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