250 likes | 659 Views
Miscellaneous Topics. CS-2301, System Programming for Non-majors (Slides include materials from The C Programming Language , 2 nd ed., by Kernighan and Ritchie and from C: How to Program , 5 th ed., by Deitel and Deitel). Arguments vs. Parameters.
E N D
Miscellaneous Topics CS-2301, System Programming for Non-majors (Slides include materials from The C Programming Language, 2nd ed., by Kernighan and Ritchie and from C: How to Program, 5th ed., by Deitel and Deitel) Miscellaneous topics
Arguments vs. Parameters • Many people think of these terms as synonyms • C defines the difference precisely • Pages 25 and 201 in Kernighan & Ritchie • But even K & R are not completely consistent — see p. 70! • A parameter is a variable defined in a function header • An argument is an expression evaluated by the caller of a function and copied to the corresponding parameter Miscellaneous topics
Parameters • Every function definition has the form return-type function-name (parameter declarations) compound statement • I.e., a comma-separated list of identifiers and types • Each parameter behaves exactly like an automatic variable • Allocated on the stack • Scope limited to the function body • May be assigned to (subject to const specifications) • Deallocated when function returns Miscellaneous topics
Scope to be defined shortly Parameters • Every function definition has the form return-type function-name (parameter declarations) compound statement • I.e., a comma-separated list of identifiers and types • Each parameter behaves exactly like an automatic variable • Allocated on the stack • Scope limited to the function body • May be assigned to (subject to const specifications) • Deallocated when function returns Miscellaneous topics
Arguments • Expressions evaluated prior to calling a function • I.e., a comma-separated list of values • Number and types of expressions must match number and types of parameters • … or be automatically convertible to them • Argument values are assigned to parameters as part of function call • … and never looked at again by calling function! • Assignments by called function to its parameters are never seen by calling function Miscellaneous topics
Arrays and pointers may look like an exception, but they are not! Arguments • Expressions evaluated prior to calling a function • I.e., a comma-separated list of values • Number and types of expressions must match number and types of parameters • … or be automatically convertible to them • Argument values are assigned to parameters as part of function call • … and never looked at again by calling function! • Assignments by called function to its parameters are never seen by calling function Miscellaneous topics
const Specifier and Parameters • int func(const int n, double a)in function header means • … body of func may assign to a but not to n • Not used very often in C; Very frequently used in C++ • Helps write clearer code • Helps think through pre- and post-conditions • Improves checking by compiler Miscellaneous topics
const Specifier and Parameters (cont’d) • Especially relevant when arguments are arrays • int func(const A[], const int size)in function header means • Body of func may not assign to A • Body of func may not pass A to another function that takes a non-constant array • … directly or indirectly Miscellaneous topics
We will see why this is important shortly! const Specifier and Parameters (cont’d) • Especially relevant when arguments are arrays • int func(const A[], const int size)in function header means • Body of func may not assign to A • Body of func may not pass A to another function that takes a non-constant array • … directly or indirectly Miscellaneous topics
#definevs. const • #define creates a textual substitution • C Preprocessor reads entire program and … • Expands #include • i.e., substitutes text of included file • Replaces #define • i.e., substitutes definition for defined text wherever it occurs • Other stuff – see §4.11 and Appendix §A.12 • const variables are like any other variables • … except that compiler refuses to allow them on left sides of assignment statements (directly or indirectly) Miscellaneous topics
Questions? Miscellaneous topics
Scope & Linkage • Definition:– scope of a name (p. 195, §A11.1) • The region of the program in which the name is known • Definition:– linkage of a name (§A11.2) • Whether or not the same name in another scope refers to the same object or function Miscellaneous topics
More on Scope • Scope of a variable name starts where it is declared and continues through • End of block or function where it is declared, or • End of C program if not declared in a block or function • Same for typedefs, structures, labels, unions, function names, etc. Miscellaneous topics
Block is synonymous with compound statement More on Scope • Scope of a variable name starts where it is declared and continues through • End of block or function where it is declared, or • End of C program if not declared in a block or function • Same for typedefs, structures, labels, unions, function names, etc. Miscellaneous topics
Example int j, k; int f(int a, double b){int c = k; j = c; } Miscellaneous topics
j and k are visible throughout entire program Function f can read and/or assign to j and k. Example int j, k; int f(int a, double b){int c = k; j = c; } Miscellaneous topics
a, b, and c are only visible inside function f Example int j, k; int f(int a, double b){int c = k; j = c; } Miscellaneous topics
a, b, and c in this function g are entirely different from those of f Example #2 int f(int a, double b){int c = k; j = c; } // f int g(int a, double b){int c; }// g Miscellaneous topics
a, b, and c in this function g are entirely different from those of f Example #2 int f(int a, double b){int c = k; j = c; } // f int g(int a, double b){int c; }// g This is perfectly reasonable C programming style Miscellaneous topics
d is only visible inside the for loop, not the rest of f Example #3 – an inner scope int j, k; int f(int a, double b){int c = k;for (…; …; …){ double d; d = c;} j = c; } Miscellaneous topics
Nested Scopes • A name may not be redefined for in the same scope • Compiler error • A name may be redefined in a nested scope • Within the inner scope, the redefined name prevails • Name from outer scope is temporarily suspended Miscellaneous topics
This j “covers up” the j defined in outer scope! They are different variables but with same name. Example #4 – nested scopes int j, k; int f(int a, double b){int c = k;if (…){ int j; for (j = 0; j < …; j++) { }} j = c; } Miscellaneous topics
j of outer scope applies here! Example #4 – nested scopes int j, k; int f(int a, double b){int c = k;if (…){ int j; for (j = 0; j < …; j++) { }} j = c; } Miscellaneous topics
Example #4 – nested scopes int j, k; int f(int a, double b){int c = k;if (…){ int j; for (j = 0; j < …; j++) { }} j = c; } As a matter of style, it is best to avoid redefining names in nested scope. The potential for confusion & error is too great. Kernighan & Ritchie, p. 85 Miscellaneous topics
Questions? Miscellaneous topics