The Three Attributes of an Identifier C Functions

The Three Attributes of an Identifier C Functions 1 Identifiers have three essential attributes: storage duration scope linkage CS@VT August 2009 Computer Organization I © 2006 -09 Mc. Quain, Feng & Ribbens

Storage Duration C Functions 2 storage duration determines when memory is set aside for the variable and when that memory is released automatic allocated when the surrounding block is executed deallocated when the block terminates static stays in the same storage location as long as the program is running can retain its value indefinitely (until program terminates) CS@VT August 2009 Computer Organization I © 2006 -09 Mc. Quain, Feng & Ribbens

Scope C Functions 3 scope (of an identifier) the range of program statements within which the identifier is recognized as a valid name block scope visible from its point of declaration to the end of the enclosing block place declaration within a block file scope visible from its point of declaration to the end of the enclosing file place declaration outside of all blocks (typically at beginning of file) CS@VT August 2009 Computer Organization I © 2006 -09 Mc. Quain, Feng & Ribbens

Linkage C Functions 4 linkage determines the extent to which the variable can be shared by different parts of the program external linkage may be shared by several (or all) files in the program internal linkage restricted to a single file, but shared by all functions within that file no linkage restricted to a single function CS@VT August 2009 Computer Organization I © 2006 -09 Mc. Quain, Feng & Ribbens

Determining the Attributes C Functions 5 The default storage duration, scope and linkage of a variable depend on the location of its declaration: inside a block automatic storage duration, block scope, no linkage outside any block static storage duration, file scope, external linkage When the defaults are not satisfactory, see: auto static extern register CS@VT August 2009 Computer Organization I © 2006 -09 Mc. Quain, Feng & Ribbens

Function Declaration vs Definition C Functions 6 A function name is an identifier, so it must be formally declared before it is used. Unlike a simple variable or constant, a function has a return type and (possibly) a formal parameter list. The function declaration must also specify those. The function declaration is essentially just a copy of the function header from the function definition. A function declaration must specify the types of the formal parameters, but formal parameter names are optional. double circle. Area(double Radius); double circle. Area(double Radius) { const double PI = 3. 141596224; } return (PI * Radius); Many authors refer to a function declaration as a prototype. However, it is good practice to include the formal parameter names in the function declaration. Function declarations are typically declared at file scope or within a header file, although they may be placed anywhere. The placement determines the scope of the function name, and hence where it may be called. CS@VT August 2009 Computer Organization I © 2006 -09 Mc. Quain, Feng & Ribbens

Formal Parameters are Pass-by-Value C Functions 7 Formal parameters have automatic storage duration and block scope, just like local variables. Formal parameters are automatically initialized with a copy of the value of the corresponding actual parameter. There is no connection between the actual and formal parameters other than that they store the same value at the time of the function call. In Java, you can pass a function a reference to an object (in fact, there's no other way to pass an object) and the function can use the reference to modify the object that's held by the calling code. In C, you can't do that until you understand how pointers work… CS@VT August 2009 Computer Organization I © 2006 -09 Mc. Quain, Feng & Ribbens

Typical C Code Organization C Functions 8 For now, we'll restrict our attention to single-file programs. The typical organization is: // include directives. . . // file-scoped declarations of functions // and constants. . . int main() {. . . } // implementations of other functions CS@VT August 2009 Computer Organization I © 2006 -09 Mc. Quain, Feng & Ribbens

Example Program C Functions 9 #include <stdio. h> #include <math. h> #include <stdbool. h> int next. Prime. After(int Start); bool is. Prime(int Value); int main() { int Value; printf("Enter a positive integer: "); fflush(stdout); scanf("%d", &Value); printf("n"); while ( Value <= 0 ) { printf("No, I said a POSITIVE integer: "); scanf("%d", &Value); printf("n"); } // continues. . . CS@VT August 2009 Computer Organization I © 2006 -09 Mc. Quain, Feng & Ribbens

Example Program C Functions 10 //. . . continued int Prime = 2; while ( Value > 1 ) { bool factor. Found = false; while ( Value % Prime == 0 ) { if ( !factor. Found ) printf("Prime factor: "); printf("%5 d", Prime); factor. Found = true; Value = Value / Prime; } if ( factor. Found ) printf("n"); Prime = next. Prime. After( Prime ); } return 0; } CS@VT August 2009 Computer Organization I © 2006 -09 Mc. Quain, Feng & Ribbens

Example Program C Functions 11 int next. Prime. After(int Start) { int Value = Start + 1; while ( !is. Prime( Value ) ) { ++Value; } return Value; } CS@VT August 2009 Computer Organization I © 2006 -09 Mc. Quain, Feng & Ribbens

Example Program C Functions 12 bool is. Prime(int Value) { int Ceiling = (int) sqrt( Value ); for (int Divisor = 2; Divisor <= Ceiling; Divisor++) { if ( Value % Divisor == 0 ) return false; } return true; } CS@VT August 2009 Computer Organization I © 2006 -09 Mc. Quain, Feng & Ribbens
- Slides: 12