Homework Assignment #3 Solutions CSE 452: Fall 2004 Due: 09/30/04 Textbook Evaluations: Please complete and turn-in your evaluations of Chapter 3 by the due date shown above. Evaluations should be emailed to cse452@cse.msu.edu and should include your name (preferably as part of evaluation file name). Please be sure to give concrete examples of improvements to be made when you are not satisfied with a specific part of the chapter. Instructions: Answer each question below. Your answers must be generated using a common word processing program such as MS Word or LaTex (handwritten work will not be accepted). Figures may be hand-drawn or you may generate these using a graphics package such as XFig. Hand in all answers in hardcopy form (no emails) by the due date shown. 1. Taking into account what you should now know about scoping and declaration orders from Chapter 3, consider the following C program: void fun(void) { int a, b, c; /* definition 1*/ … while (…) { int b, c, d; /* definition 2 */ … <=============== while (…) { int c, d, e; /* definition 3 */ … <=============== } … <=============== } … <=============== } 1 2 3 4 For each of the four marked points in this function, list each visible variable, along with the number of the definition statement that defines it. 1. a – definition 1 b, c, d – definition 2 2. a – definition 1 b – definition 2 c, d, e – definition 3 3. Same as point #1 above. 4. a, b, c – definition 1 2. Consider the following skeletal program: procedure Main is X, Y, Z : Integer; procedure Sub1 is A, Y, Z : Integer; begin -- of Sub1 … end -- of Sub1 procedure Sub2 is A, B, Z : Integer; begin -- of Sub2 … end -- of Sub2 procedure Sub3 is A, X, W : Integer; begin -- of Sub3 … end -- of Sub3 begin -- of Main … end -- of Main Given the following calling sequences and assuming that dynamic scoping is used, what variables are visible during the execution of the last subprogram activated? Include with each visible variable the name of the unit where it is declared. (a) (b) Variable a, x, w b, z y Where Declared sub3 sub2 sub1 a, y, z sub1 (c) x, w b sub3 sub2 a, y, z x, w sub1 sub3 3. Briefly describe the difference between a declaration and a definition and why the distinction between the two is important. A declaration introduces a name and indicates its scope, whereas a definition describes the thing to which the declared name is bound. A declaration may not be complete enough to be a definition and as such a separate definition must be included somewhere within the scope of that name in order for it to be used. Thus, a declaration may or may not contain information which defines what is bound to a given name (function or variable).