#define scope

Discussion about the ZBasic language including the System Library. If you're not sure where to post your message, do it here. However, do not make test posts here; that's the purpose of the Sandbox.
Post Reply
FFMan
Posts: 502
Joined: 09 January 2010, 12:52 PM

#define scope

Post by FFMan »

I am using a #define to define the size of a queue for serial comms.

A function in a separate bas file needs to dimension (and initiialise) an array of the same size for recieving characters. If i try and reference the identifier declared in the main module i get a compiler error (reference to undefined identifier). if the code is not in a separate bas file i do not get the error.

i infer from this that the scope of identifiers does not extend to functions delcared in other files ?

If this is the case, what is the best way of achieving what i want without resorting to amending code in my separate source file that may be common to several main programs ?

thanks
dkinzer
Site Admin
Posts: 3120
Joined: 03 September 2005, 13:53 PM
Location: Portland, OR

Re: #define scope

Post by dkinzer »

FFMan wrote:i infer from this that the scope of identifiers does not extend to functions delcared in other files ?
That is correct. The scope of a #define extends from the point at which it appears until either 1) the end of the file, or 2) a matching #undef.

For a constant value that is used in multiple modules, use a Const:

Code: Select all

Public Const MyQueueSize as Integer = 27
- Don Kinzer
FFMan
Posts: 502
Joined: 09 January 2010, 12:52 PM

Post by FFMan »

I'll use a const as well, i was trying to use an identifier for both compiler directives and dimensioning arrays, but i'll use an identifier and a const and that will be just as easy to maintain in reality.

thanks
dkinzer
Site Admin
Posts: 3120
Joined: 03 September 2005, 13:53 PM
Location: Portland, OR

Post by dkinzer »

Another alternative for defining global constants is to put definitions in your .pjt file with the -D option. Such definitions are global in scope, i.e., they are known in all modules. These should be placed in the .pjt file before the modules in which you want the definition to be in effect.

Code: Select all

-DMyQueueSize=27
- Don Kinzer
Post Reply