Wiki source for ProPgVarProcLinkage


Show raw source

{{fbdoc item="title" value="Variable and Procedure Linkage"}}----
Name visibility within and between modules
//Linkage// refers to the visibility of the name of a variable, object or procedure between one or more modules of a program. In other words, a linkage dictates how a name is shared between modules. There are two main types of linkage a name can have: //internal// and //external//.

{{fbdoc item="section" value="Internal linkage"}}
Names with //internal linkage// only refer to variables, objects or procedures defined within their own module; they are not outwardly visible to other modules. This means that two or more modules can refer to different things using the same name. Note that linkage only refers to visibility of a name, and depending on storage class and lifetime, a variable, object or procedure with internal linkage may be shared between modules using its address.

{{fbdoc item="subsect" value="Module-scope declarations"}}

Variable and object names declared at module-scope have internal linkage unless otherwise declared with ##[[KeyPgExtern|Extern]]## or ##[[KeyPgCommon|Common]]##. For example, variable names first introduced with ##[[KeyPgDim|Dim]]## or ##[[KeyPgStatic|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 ##[[KeyPgShared|Shared]]## only allows name visibility within the module's procedures, and does not contribute to the name's linkage.

Procedure names declared with ##[[KeyPgPrivate|Private]]## have internal linkage.

{{fbdoc item="subsect" value="Local-scope declarations"}}

All variable and object names declared at local-scope (in a ##[[KeyPgDo|Do]]## loop, or procedure body, for instance) have internal linkage.

{{fbdoc item="section" value="External linkage"}}
Names with //external linkage// may refer to variables, objects or procedures defined within their module or in another module. Having external linkage means that a name is outwardly visible to other modules, and all modules that use that same external name all refer to the same variable, object or procedure. Thus, only one module may define an external name (the compiler will complain about a duplicated definition if it finds an additional definition of a name with external linkage).

{{fbdoc item="subsect" value="Module-scope declarations"}}

Variable and object names declared at module-scope are declared to have external linkage with ##[[KeyPgExtern|Extern]]## or ##[[KeyPgCommon|Common]]##.

##[[KeyPgExtern|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 ##[[KeyPgExtern|Extern]]## specifies internal linkage, and currently, any further external declarations of that name signify a duplicated definition). Variable and object names with external linkage declared using ##[[KeyPgExtern|Extern]]## are always in the shared scope, and so can be referred to within procedure bodies.

##[[KeyPgCommon|Common]]## declares the variable having external linkage as well as defining the variable. But, it is different from ##[[KeyPgExtern|Extern]]## in that the ##[[KeyPgCommon|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 ##[[KeyPgDim|Dim]]## or ##[[KeyPgRedim|ReDim]]## before it can be used. Variable and object names with external linkage declared using ##[[KeyPgCommon|Common]]## are only in the shared scope if the ##[[KeyPgShared|Shared]]## scope specifier is also given. Shared variables can be referred to within procedure bodies.

When both ##[[KeyPgExtern|Extern]]## and ##[[KeyPgCommon|Common]]## are both used to declare and define a variable, the effect is that the meaning of ##[[KeyPgCommon|Common]]## statement is altered to behave as though it were a ##[[KeyPgDim|Dim]]## declaration. So it is generally, not recommended to mix ##[[KeyPgExtern|Extern]]## and ##[[KeyPgCommon|Common]]## on the same variable in the same module. However, variables may be declared and defined with ##[[KeyPgCommon|Common]]## in one module and then referenced with ##[[KeyPgExtern|Extern]]## in another module without confusion.

Procedure names are declared to have external linkage by default. Declarations using ##[[KeyPgPublic|Public]]## explicitly specify external linkage.

{{fbdoc item="subsect" value="Local-scope declarations"}}

Currently, names declared at local-scope cannot have external linkage.

{{fbdoc item="back" value="CatPgProgrammer|Programmer's Guide"}}
Valid XHTML :: Valid CSS: :: Powered by WikkaWiki



sf.net phatcode