Presentation is loading. Please wait.

Presentation is loading. Please wait.

Miscellaneous topicsCS-2301 B-term 20081 Miscellaneous Topics CS-2301, System Programming for Non-majors (Slides include materials from The C Programming.

Similar presentations


Presentation on theme: "Miscellaneous topicsCS-2301 B-term 20081 Miscellaneous Topics CS-2301, System Programming for Non-majors (Slides include materials from The C Programming."— Presentation transcript:

1 Miscellaneous topicsCS-2301 B-term 20081 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)

2 Miscellaneous topicsCS-2301 B-term 20082 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

3 Miscellaneous topicsCS-2301 B-term 20083 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

4 Miscellaneous topicsCS-2301 B-term 20084 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 Scope to be defined shortly

5 Miscellaneous topicsCS-2301 B-term 20085 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

6 Miscellaneous topicsCS-2301 B-term 20086 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 Arrays and pointers may look like an exception, but they are not!

7 Miscellaneous topicsCS-2301 B-term 20087 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

8 Miscellaneous topicsCS-2301 B-term 20088 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

9 Miscellaneous topicsCS-2301 B-term 20089 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 We will see why this is important shortly!

10 Miscellaneous topicsCS-2301 B-term 200810 #define vs. 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)

11 Miscellaneous topicsCS-2301 B-term 200811 Questions?

12 Miscellaneous topicsCS-2301 B-term 200812 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

13 Miscellaneous topicsCS-2301 B-term 200813 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.

14 Miscellaneous topicsCS-2301 B-term 200814 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. Block is synonymous with compound statement

15 Miscellaneous topicsCS-2301 B-term 200815 Example int j, k; int f(int a, double b){ int c = k; j = c; }

16 Miscellaneous topicsCS-2301 B-term 200816 Example int j, k; int f(int a, double b){ int c = k; j = c; } j and k are visible throughout entire program Function f can read and/or assign to j and k.

17 Miscellaneous topicsCS-2301 B-term 200817 Example int j, k; int f(int a, double b){ int c = k; j = c; } a, b, and c are only visible inside function f

18 Miscellaneous topicsCS-2301 B-term 200818 Example #2 int f(int a, double b){ int c = k; j = c; }// f int g(int a, double b){ int c; }// g a, b, and c in this function g are entirely different from those of f

19 Miscellaneous topicsCS-2301 B-term 200819 Example #2 int f(int a, double b){ int c = k; j = c; }// f int g(int a, double b){ int c; }// g a, b, and c in this function g are entirely different from those of f This is perfectly reasonable C programming style

20 Miscellaneous topicsCS-2301 B-term 200820 Example #3 – an inner scope int j, k; int f(int a, double b){ int c = k; for (…; …; …){ double d; d = c; } j = c; } d is only visible inside the for loop, not the rest of f

21 Miscellaneous topicsCS-2301 B-term 200821 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

22 Miscellaneous topicsCS-2301 B-term 200822 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; } This j “covers up” the j defined in outer scope! They are different variables but with same name.

23 Miscellaneous topicsCS-2301 B-term 200823 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; } j of outer scope applies here!

24 Miscellaneous topicsCS-2301 B-term 200824 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

25 Miscellaneous topicsCS-2301 B-term 200825 Questions?


Download ppt "Miscellaneous topicsCS-2301 B-term 20081 Miscellaneous Topics CS-2301, System Programming for Non-majors (Slides include materials from The C Programming."

Similar presentations


Ads by Google