Lecture #6
Programming Languages MISHRA 95
C
,
---Slide 2---
Scope
A pointer variable is a name denoting a single object whose value is a reference to another object.
---Slide 3---
Extent
---Slide 4---
Range
C
, Algol 60),
Modules (Ada).
---Slide 5---
Dynamic and Static Ranges
Free or nonlocal variables are given name bindings in the static context of a range.
Free or nonlocal variables are given name bindings in the dynamic context of a range.
---Slide 6---
Example 1
var i, k: integer; procedure P(var j: integer); var i: integer; begin i := 1; Q; j := i end; procedure Q; begin i := i+1 end; begin i := 3; P(k); write(k) end.
---Slide 7---
Example 2
var i: integer; function GLOP(function Q: integer, lower, upper: integer): integer; var i,S: integer; begin S := 0; for i := lower to upper do S := S + Q; GLOP := S end; function A; begin A := i*i end; begin i := 0; write(GLOP(A,1,3)) end.
---Slide 8---
Procedures and Functions
int succ(int i){ function succ(i: in INTEGER) return (i+1)%size; return INTEGER is } begin return (i+1 mod size); end succ;
---Slide 9---
Parameter Passing Methods
---Slide 10---
Calling Mechanisms
---Slide 11---
Variations
in
, out
and
in out
. The compiler can implement these with
call-by-value-result (copy-in/copy-out) or
call-by-reference, the choice based on efficiency considerations.
in
: Used for passing information to the callee.
out
: Used for passing information to the caller.
in out
: Information is passed in and back out
through the same parameter.
procedure USE(X: in INTEGER) is ... procedure GENERATE(X: out INTEGER) is ... procedure MODIFY(X: in out INTEGER) is ...
---Slide 12---
Example
var i: integer; A: array[1..3] of integer; procedure testbind( <binding> f, g: integer); begin g := g + 1; f := 5 * i; (* i= nonlocal) end; begin for i := 1 to 3 do A[i] := i; i := 2; testbind(A[i], i); print(i, A[1], A[2], A[3]); end;
---Slide 13---
Example (contd)
i = 2; A = (1, 2, 3);
i = 3; A = (1, 15, 3);
i = 3; A = (1, 10, 3);
i = 3; A = (1, 2, 15);
---Slide 14---
Macros: Text Substitution
C
,
#define
compiler directive in C
#define SQR(x) ((x)*(x)) main(){ int a, b=2; a = SQR(b); /* ((b)*(b)) */ a = SQR(b++); /* ((b++)*(b++)) */ }
---Slide 15---
Macros (contd)
testbind
as a macro, we get:
for i := 1 to 3 do A[i] := i; i := 2; i := i + 1; A[i] := 5 * i; (* i is captured *) print(i, A[1], A[2], A[3]);
---Slide 16---
Subprograms in Ada
function <name>(<parameters>) return <result-type> is --Local declarations begin --Local statements end; procedure <name>(<parameters>) is --Local declarations begin --Local statements end;
in
(copy-in or call-by-value)
parameters.
in
(copy-in or call-by-value),
out
(copy-out or call-by-result) or in out
parameters.
in
.
---Slide 17---
Example
in
parameter can be an
R-value (e.g., expression).
out
or in out
parameter must have an L-value (e.g., variable, selector).
in
out
in out
procedure COMPUTE_RISE(V_X, V_Y, DIST: in FLOAT; RISE: out FLOAT) is TIME: FLOAT; begin TIME := DIST/V_X; RISE := V_Y * TIME - (G/2.0) * (TIME**2); end;
---Slide 18---
Procedure Calls in Ada
COMPUTE_RISE(X_VEL, Y_VEL, TARGET_DIST, ELEVATION); COMPUTE_RISE(50.0, 60.0, (POSN + 10.0), R(1)); --R = array of FLOAT
COMPUTE_RISE(RISE => Z, DIST => D, V_X => 50.0; V_Y => 60.0);
in
parameters.
procedure GENERATE_REPORT( DATA_FILE: in FILE := SUPPLY_DATA; NUM_COPIES: in INTEGER := 1; HEADER: in LINE := STD_HEADER; CENTERING: in BOOLEAN := TRUE; TYPE_FONT: in PRINTER := TIMES_ROMAN) is ...
GENERATE_REPORT; GENERATE_REPORT(NUM_COPIES => 5); GENERATE_REPORT(HEADER => "FINAL REPORT", TYPE_FONT => HELVETICA, NUM_COPIES => 100);
---Slide 19---
Usage of Default Values
procedure PLOT(X, Y: in FLOAT; PEN: in PEN_POS := DOWN; GRID: in BOOLEAN := FALSE; ROUND: in BOOLEAN := FALSE) is ... PLOT(0.0, 0.0); PLOT(0.0, 0.0, GRID => TRUE); PLOT(0.0, 0.0, UP, ROUND => TRUE); PLOT(0.0, 0.0, ROUND => TRUE, PEN => UP);
---Slide 20---
Separation of Subprogram Bodies
Declaration
Body
Declaration: procedure ADD(I: in ITEM; Q: in out QUEUE); procedure REMOVE(I: out ITEM; Q: in out QUEUE); function FRONT(Q: QUEUE) return ITEM; function IS_EMPTY(Q: QUEUE) return BOOLEAN; Body: procedure ADD(I: in ITEM; Q: in out QUEUE) is -- ... end ... function IS_EMPTY(Q: QUEUE) return BOOLEAN is -- ... end
---Last Slide---
Overloading
procedure PUT(X: INTEGER); procedure PUT(X: FLOAT); procedure PUT(X: STRING); function "+"(X,Y: VECTOR) return VECTOR;
PUT(I+1); PUT(SQRT(Y)); PUT("HELLO WORLD!"); A := A + B;
I, J: INTEGER; R: FLOAT; R := I + J; --Which + does the compiler use?
[End of Lecture #6]