1 / 126

Lecture 7: Control Issues

This lecture covers control issues in programming, specifically focusing on procedures, functions, and scope. Topics include scoping, parameter passing, and the difference between single-entry and multiple-entry control structures.

chisolm
Download Presentation

Lecture 7: Control Issues

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. Lecture 7: Control Issues Reading: Sebesta Chp 8, 9.1-9.8, 10-3.10.6 Supplementary Texts: Pratt 8.1-8.3, Chp 9; Tucker & Noonan 4.5-4.6, 5.1-5.2 : Tucker & Noonan is not as comprehensive as it should be.

  2. Lecture 5,6 Control Operations Data A Program (in some language) = Data + Operations + Control Lecture 7

  3. Overview • Introduction • Procedures and Functions • Scoping : Static vs Dynamic Scope • Parameter Passing: • By Value • By Reference • By Name • By Value-Result • Procedure Activation

  4. 1. Introduction • Forms of Control • Statement Level • Sequencing, Selection, Iteration • Sub-program Level • Procedures and Functions • Coverage in this lecture will be on issues relating to sub-program level control. • Statement Level control left for you to read up on your own (Sebesta Chp 8)

  5. 1. Introduction • Structured Programming “Eventually, one of our aims is to make such well-structured programs that the intellectual effort (measured in some loose sense) needed to understand them is proportional to program length (measured in some equally loose sense).” Dijkstra [1972] proposing that statements be designed so that “progress through the computation”can be understood in terms of “progress through the text” • Issue is on readability and understandability of (someone else’s) program. • Single-entry control structure is a better design.

  6. 1. Introduction • Single-Entry Control Structures: • Statement-level • Sequencing: Blocks • Selection • Logically control Iteration. • Sub-program-level • Procedures • Functions • Multiple-Entry Control Structures: • ‘Goto’ statement • Theoretically, you only need the ‘goto’ and that’s enough to express all forms of control. But practically, (especially for huge programs)…

  7. 1. Introduction “The goto statement as it stands, is just too primitive; it is too much an invitation to make a mess of one’s program” Dijkstra [1968] • Programmers using only goto statements tend to end up writing spaghetti code. • Ban the ‘goto’. Ban unconditional branching. • You can survive without a ‘goto’ statement. It has been proven that you need only two control-structures: • The selection statement. • The logically controlled iteration statement. (Real languages provide a variety of the above two to enhance readability, but actually, one of each is enough) • Some languages still include a goto. (Much like the issue of strong typing + type casting = weak typing)

  8. 1. Introduction • Statement-Level Control. • Selection: 1-way, 2-way, multiple-way • Iteration: for, while, do-while, repeat etc. For you to read up… you should be able to understand them since you have some experience in programming. • We’ll proceed to sub-program level control.

  9. 2. Procedures and Functions • A procedure/function is a construct for giving a name to a block of code (called the body), thereby providing a mechanism for executing that block of code by calling its name. • A function is called from within an expression and returns a result after invocation. A procedure is treated as an atomic statement and does not return a result after invocation. • Nested definitions of procedures/functions are usually allowed in most languages.

  10. 2. Procedures and Functions • In defining a procedure/function, the list of argument names in the definition is known as the formal parameters (or formals). function gcd(x:integer, y:integer):integer begin if (x > y) then return gcd(x-y,y) else return gcd(y-x,x) end;

  11. 2. Procedures and Functions • In invoking a procedure/function, the list of arguments passed in are known as the actual parameters (or actuals). var a,b : integer; ... writeln(gcd(a,b)); ...

  12. 2. Procedures and Functions • Each variable, when declared (including the formals), has • a binding occurrence; • a scope; • and when it is used, it has a bound/applied occurrence.

  13. Binding Occurrence of n Bound occurrence of n Scope of n Bound occurrence of n 2. Procedures and Functions • program L ; • var n : char ; • procedure W ; • begin • writeln(n) • end ; • procedure D ; • var m : char ; • begin • m := ‘D’; • W • end ; • begin { L } • n := ‘L’ ; • W ; • D • end.

  14. Binding Occurrence of m Scope of m Bound occurrence of m 2. Procedures and Functions • program L ; • var n : char ; • procedure W ; • begin • writeln(n) • end ; • procedure D ; • var m : char ; • begin • m := ‘D’; • W • end ; • begin { L } • n := ‘L’ ; • W ; • D • end.

  15. Out of scope variable 2. Procedures and Functions • Issue#1: Scope rules for variable names. • Each bound occurrence of a variable should be located within the immediate scope of its binding occurrence. • How do we determine the binding occurrence of ‘out of scope’ variables? procedure swap(x,y : integer); begin z := x; x := y; y := z; end;

  16. procedure swap(x,y : integer); var z : integer; begin z := x; x := y; y := z; end; var a,b : integer; ... swap(a,b); ... 2. Procedures and Functions • Issue#2: Parameter Passing • When a procedure is activated, how do we pass the actuals into the procedure code? (i.e. what are the various ways of doing it?)

  17. 2. Procedures and Functions • Issue#3: Implementation • There are a myriad of details to take care of to ensure proper activation and return of a procedure. For example, when procedure X calls procedure Y, X will ‘magically’ be ‘suspended’ in its computation and control (program counter) will pass to Y. When Y finishes, control (program counter) will pass back to X, and X will see the state of things (eg. the registers) just as he left it before he called Y. • Who does all these things? • The hardware does not do it. • The programmer does not do it either. • It’s the compiler who does it. How does the compiler do it?

  18. Overview • Introduction • Procedures and Functions • Scoping : Static vs Dynamic Scope • Parameter Passing: • By Value • By Reference • By Name • By Value-Result • Procedure Activation

  19. 3. Scope - Static Static/Lexical Scoping • A variable is accessible… • in the procedure it is declared in,… • and all procedures internal (i.e. internal declaration) to that procedure,… • EXCEPT a new declaration of that variable name in an internal procedure will eliminate the new variable’s scope from the scope of the outer variable.

  20. (2)… and all procedures internal to that procedure… A declared variable is accessible in… (1) The procedure it is declared in… 3. Scope - Static Static/Lexical Scoping • program L ; • var n : char ; • procedure W ; • begin • writeln(n) • end ; • procedure D ; • var n : char ; • begin • n := ‘D’; • W • end ; • begin { L } • n := ‘L’ ; • W ; • D • end. (3)… EXCEPT a new declaration of that variable name in an internal procedure will eliminate the new variable’s scope from the scope of the outer variable.

  21. This is called a ‘hole’ in the scope of n 3. Scope - Static Static/Lexical Scoping • program L ; • var n : char ; • procedure W ; • begin • writeln(n) • end ; • procedure D ; • var n : char ; • begin • n := ‘D’; • W • end ; • begin { L } • n := ‘L’ ; • W ; • D • end. A declared variable is accessible in…

  22. 3. Scope - Static • Scope is therefore dependent on the nesting of (procedure) blocks in the program (i.e. the syntax of the program). (that’s why it’s called LEXICAL scoping). • And therefore the scoping of each variable is known at compile time (that’s why it’s also called STATIC scoping). Static/Lexical Scoping

  23. var ...z... var ...z... var ...z... 3. Scope - Static • Q: How do we determine out-of-scope variables? • A: Find the innermost enclosing declaration. Static/Lexical Scoping procedure p(); begin ... z ...end;

  24. 3. Scope - Static • Q: How do we determine out-of-scope variables? • A: Find the innermost enclosing declaration. Static/Lexical Scoping procedure swap(x,y : integer); begin z := x; x := y; y := z; end;

  25. 3. Scope - Static • Q: How do we determine out-of-scope variables? • A: Find the innermost enclosing declaration. Static/Lexical Scoping Is z declared here? If yes, then this the one (by static scope rule). var ……………… procedure swap(x,y : integer); begin z := x; x := y; y := z; end; If no…

  26. 3. Scope - Static • Q: How do we determine out-of-scope variables? • A: Find the innermost enclosing declaration. Static/Lexical Scoping Is z declared here? If yes, then this the one (by static scope rule). var ………………… var ……………… procedure swap(x,y : integer); begin z := x; x := y; y := z; end; Keep repeating this. If variable is not found in top-level scope, then error => undeclared variable.

  27. 3. Scope - Static • “The variable is not in this block.” • Key Question: “Who DECLARED this block? Go to the declarer and look for the variable there.” Static/Lexical Scoping

  28. 3. Scope - Dynamic Dynamic Scoping • A variable is accessible… • in the procedure it is declared in,… • and all procedures CALLED by that procedure,… • EXCEPT a new declaration of that variable name in a CALLED procedure will eliminate the new variable’s scope from the scope of the outer variable.

  29. 3. Scope - Dynamic • “The variable is not in this block.” • Key Question: “Who called this block? Go to the caller and look for the variable there.” • Note: “Who declared me” and “Who called me” need not refer to the same procedure block. Dynamic Scoping

  30. 3. Scope - Dynamic • Scope is therefore dependent on the execution of the program and cannot be determined at compile-time. (that’s why it’s called DYNAMIC scoping). Dynamic Scoping

  31. 3. Scope - Dynamic Dynamic Scoping program L; var n : char; procedure W begin writeln(n) end; procedure D; var n : char; begin n := ‘D’; W end; begin n := ‘L’; W; D; end. L

  32. Output of Dynamic Scope L Output of Static Scope L 3. Scope - Dynamic Dynamic Scoping Who Called Me? program L; var n : char; procedure W begin writeln(n) end; procedure D; var n : char; begin n := ‘D’; W end; begin n := ‘L’; W; D; end. L Who Declared Me?

  33. Output of Dynamic Scope L Output of Static Scope L 3. Scope - Dynamic Dynamic Scoping Who Called Me? program L; var n : char; procedure W begin writeln(n) end; procedure D; var n : char; begin n := ‘D’; W end; begin n := ‘L’; W; D; end. L D Who Declared Me?

  34. Output of Dynamic Scope L Output of Static Scope L 3. Scope - Dynamic Dynamic Scoping Who Called Me? program L; var n : char; procedure W begin writeln(n) end; procedure D; var n : char; begin n := ‘D’; W end; begin n := ‘L’; W; D; end. L D D Who Declared Me? L

  35. 3. Scope • Static/Lexical Scope – C, Pascal. • Dynamic Scope – LISP.

  36. Overview • Introduction • Procedures and Functions • Scoping : Static vs Dynamic Scope • Parameter Passing: • By Value • By Reference • By Name • By Value-Result • Procedure Activation

  37. A000:0000 10 var a,b : integer; ... a := 10; b := 20; swap(a,b); ... A000:0002 20 procedure swap(x,y : integer); var z : integer; begin z := x; x := y; y := z; end; AAAA:0000 10 AAAA:0002 20 4. Parameter Passing (By Value) (A) Call by Value • Pass the R-value of the argument for the parameter.

  38. A000:0000 10 var a,b : integer; ... a := 10; b := 20; swap(a,b); ... procedure swap(x,y : integer); var z : integer; begin z := x; x := y; y := z; end; AAAA:0000 10 AAAA:0002 AAAA:0004 20 10 4. Parameter Passing (By Value) (A) Call by Value • Pass the R-value of the argument for the parameter. A000:0002 20

  39. A000:0000 10 var a,b : integer; ... a := 10; b := 20; swap(a,b); ... procedure swap(x,y : integer); var z : integer; begin z := x; x := y; y := z; end; AAAA:0000 20 AAAA:0002 AAAA:0004 10 10 4. Parameter Passing (By Value) (A) Call by Value • Pass the R-value of the argument for the parameter. A000:0002 20 Changes are local to the procedure

  40. A000:0000 10 var a,b : integer; ... a := 10; b := 20; swap(a,b); ... A000:0002 20 procedure swap(x,y : integer); var z : integer; begin z := x; x := y; y := z; end; AAAA:0000 A000:0000 AAAA:0002 A000:0002 4. Parameter Passing (By Reference) (B) Call by Reference • Pass the L-value of the argument for the parameter.

  41. A000:0000 10 var a,b : integer; ... a := 10; b := 20; swap(a,b); ... procedure swap(x,y : integer); var z : integer; begin z := x; x := y; y := z; end; AAAA:0000 A000:0000 AAAA:0002 AAAA:0004 A000:0002 10 4. Parameter Passing (By Reference) (B) Call by Reference • Pass the L-valueof the argument for the parameter. A000:0002 20

  42. var a,b : integer; ... a := 10; b := 20; swap(a,b); ... procedure swap(x,y : integer); var z : integer; begin z := x; x := y; y := z; end; AAAA:0000 A000:0000 AAAA:0002 AAAA:0004 A000:0002 10 4. Parameter Passing (By Reference) (B) Call by Reference • Pass the L-valueof the argument for the parameter. 20 A000:0000 10 A000:0002 20

  43. var a,b : integer; ... a := 10; b := 20; swap(a,b); ... procedure swap(x,y : integer); var z : integer; begin z := x; x := y; y := z; end; AAAA:0000 A000:0000 AAAA:0002 AAAA:0004 A000:0002 10 4. Parameter Passing (By Reference) (B) Call by Reference • Pass the L-valueof the argument for the parameter. 20 A000:0000 10 A000:0002 10 20

  44. var a,b : integer; ... a := 10; b := 20; swap(a,b); ... procedure swap(x,y : integer); var z : integer; begin z := x; x := y; y := z; end; AAAA:0000 A000:0000 AAAA:0002 AAAA:0004 A000:0002 10 4. Parameter Passing (By Reference) (B) Call by Reference • Pass the L-valueof the argument for the parameter. 20 A000:0000 10 A000:0002 10 20 Changes are reflected ‘outside’

  45. 4. Parameter Passing (By Reference) • Call by reference in Pascal is performed via the var prefix to a formal parameter. procedure swap(Var x : integer, Var y : integer); var z : integer; begin z := x; x := y; y := z; end; ... swap(a,b); ... (B) Call by Reference

  46. 4. Parameter Passing (By Reference) • Call by reference in C has to be simulated by the programmer via use of pointers. void function swap(int *x, int *y) { int z; z := *x; *x := *y; *y := z; } ... swap(&a,&b); ... (B) Call by Reference

  47. 4. Parameter Passing (By Name) (C) Call by Name • Textual substitution of parameters coupled with renaming to avoid name conflicts • Actuals are textually substituted for the formals. • Procedure body is textually subtituted for the call.

  48. Pure Textual Substitution causes name conflicts!!! var a,z : integer; ... a := 10; z := 20; z := a; a := z; z := z; ... var a,z : integer; ... a := 10; z := 20; swap(a,z); ... 4. Parameter Passing (By Name) (C) Call by Name procedure swap(x,y : integer); var z : integer; begin z := x; x := y; y := z; end;

  49. procedure swap(x,y : integer); var t : integer; begin t := x; x := y; y := t; end; Textual Substitution with renaming to avoid name conflicts var a,z : integer; ... a := 10; z := 20; t := a; a := z; z := t; ... 4. Parameter Passing (By Name) (C) Call by Name procedure swap(x,y : integer); var z : integer; begin z := x; x := y; y := z; end; var a,z : integer; ... a := 10; z := 20; swap(a,z); ...

  50. 4. Parameter Passing (By Name) Note: • Textual substitution WITH renaming = Call by name • Textaul substitution WITHOUT renaming = Macro Expansion (C) Call by Name

More Related