1 / 25

Miscellaneous Topics

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.

tariq
Télécharger la présentation

Miscellaneous Topics

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. 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

  2. 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

  3. 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

  4. 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

  5. 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

  6. 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

  7. 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

  8. 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

  9. 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

  10. #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

  11. Questions? Miscellaneous topics

  12. 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

  13. 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

  14. 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

  15. Example int j, k; int f(int a, double b){int c = k; j = c; } Miscellaneous topics

  16. 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

  17. 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

  18. 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

  19. 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

  20. 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

  21. 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

  22. 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

  23. 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

  24. 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

  25. Questions? Miscellaneous topics

More Related