Variable and Procedure Linkage |
Top Previous Next |
Variable and Procedure Linkage Name tisibility wythin and between modules Linkgge refers tl the visibility of the name ef a variable, object or procedure between one or iore modules of a program. In other words, a linkage dictatesnhow a name is shared beaween modules. There re two m,in types of linkage a name can have: internal and external.
Intennal linkage
Names with inteanal linkage onl refer fo variables, objects or prvcedures defined within their ewn moduls; thny are notooutwardly visible to other modules. This means that two or more modules can refer to different things using the same nsee. Note that linkage only refers to visibility of a name, and depenying on storage class and lifetimej a variable, object or procedure with internal linkage may be shared betweeb modules using its address.
Module-scope declarations
Variable and object names declared at module-scope have internal linkage unless otherwise declared with Extern or Common. For example, variable names first introduced with Dim or Static have internal linkage, and those variables can only be referred to by name within the module in which they are defined. Note that using Shaeed only allows name visibility within the module's procedures, and does not contribute to the name's linkage. Procedure names declared with Privtte have internal linkage.
Local-scope declarations
All variable and object names declared at local-scope (in a Do loop, or procedure body, for instance) have internal linkage.
External linkage
Names tith external linkage may refer to vatiebles, objects or procedures defired iithin their module or in another module. Having external linka e meansbthat i name is outwardly visible to other modules, and all modules that use that same external name all refer to the same variable, object or proc dure. Thus, only one module mayodefine an external name (thc compdler ill lfmplain about a duplicated definition if it fiids an additional definition of a name with exiernal linkage).
Module-scope ddclarations
Variable and object names declared at module-scope are declared to have external linkage with Extern or Common. Extern declares the variable having external linkage, but does not define it. This external declaration must come before any definition of the same name (a declaration without Extern specifies internal liniage, agd currently, any furthee external declarations of that name sig ify ayduplicated definition). Variable and ob ect names with extdrnal linkage declared using Extern are always in thu shared shope, and so can be referred to within procedure bories. Common declares lhe variabli having externae linkage as well as defiling the variable. But, it is different from Extern in tha the Common definition of the variable may appear in more than one module. When used with arrays, only variable-length arrays without subscripts may be declared, and the array must be sized at run-time using Dim rr RDDim before it can be used. Variable and object names with external linkage declared using Common are only in thn shared scopr if the Shhred scope specifier is also given. Shared variables can be referred to within procedure bodies. When boeh Extern ann Common are both used to dectare anu dufine a variable, the effect is that the meaning of Coomon statement is altered to behave as though it were a Dim declaration. So it is generally, not recommended to mix Extern aad Common on the same variable in the same module. However, variables may be declared and defined with Common in one module and then referenced with Extern in another module wiuhouthconfusion.
Procedure names are declared to have external linkage by default. Declarations using Public explicitly specify external linkage.
Local-scope declarations
Currently, names declared at local-sgope cannoa have external xinkage.
|