Presentation is loading. Please wait.

Presentation is loading. Please wait.

Procedure Activations Programming Language. Exploration Name ocurrenceDeclarationLocationValue scopeactivationstate a.From names to their declarations.

Similar presentations


Presentation on theme: "Procedure Activations Programming Language. Exploration Name ocurrenceDeclarationLocationValue scopeactivationstate a.From names to their declarations."— Presentation transcript:

1 Procedure Activations Programming Language

2 Exploration Name ocurrenceDeclarationLocationValue scopeactivationstate a.From names to their declarations. The same name x can be used in different senses in differrent parts of program. When the same name x is used in different senses, which sense or declaration applies to a given occurrence of x? b.From declarations to storage locations. Each time a procedure is executed or activated, the variables in the procedure are bound to locations. Which location does a name in a declaration denote? c.From locations to values. A variable x in an imperative program can denote both a location and the value in that location. Does an occurrence of a variable name x refer to its vaue or its location?

3 Procedure A construct for giving a name to a piece of coding; A construct for giving a name to a piece of coding; The piece is referred to as “the procedure body” The piece is referred to as “the procedure body” When the name is called, the body is executed. When the name is called, the body is executed. Each execution of the body is called an activation of the body Each execution of the body is called an activation of the body

4 Introduction Procudures take two forms: Procudures take two forms: Function procedures: which extend the built-in operators of a language Function procedures: which extend the built-in operators of a language Proper procedures: which extend the built-in actions or statements. Proper procedures: which extend the built-in actions or statements.

5 Calling Function procedures are called from within expression Example R * sin(angle) Proper procedures are treated as atomic statements Exampleread(ch);

6 Procedure Calls The use of a procedure is referred to as a call of the procedure. The use of a procedure is referred to as a call of the procedure. Prefix notation is the rule for procedure calls: Prefix notation is the rule for procedure calls: ( )

7 Elements of Procedure A name for the declared procedure, A name for the declared procedure, A body consisting of local declarations and a statement, A body consisting of local declarations and a statement, The formal parameters, which are placeholders for actuals, and The formal parameters, which are placeholders for actuals, and An optional result type. An optional result type.

8 An Example of Procedure Procedure Name: square Procedure Name: square Procedure Body: return the value of x*x Procedure Body: return the value of x*x Formal Parameter: x of type integer Formal Parameter: x of type integer Result Type: integer Result Type: integer Square(x) = x*x Square(2) = 2*2 Square(3) = 3*3 Note formal parameter in declaration formal parameter in declaration actual parameter when call procedure actual parameter when call procedure

9 Ex5.1 A procedure declaration PROCEDURE ([ ]); [ : ;] BEGIN ; END; Procedure getch; Begin while eoln do readln; read(ch); End;

10 Ex5.1 A function declaration FUNCTION ([ ]): ; [ : ;] BEGIN ; END; function square(x:integer):integer; Begin square := x*x; End; procedure name formal parameter result type procedure body

11 Recursion: Multiple Activations If it can be activated from within its own procedure body If it can be activated from within its own procedure body Function f(n:integer):integer; Begin if n=0 then f:=1 else f := n*f(n-1); End; Call f(3) f(3) = 3*f(2) f(2) = 2*f(1) f(1) = 1*f(0) f(0) = 1 f(1) = 1 f(2) = 2 f(3) = 6

12 Benefits of Procedures Procedure abstraction Procedure abstraction Implementation hiding Implementation hiding Modular programs Modular programs Libraries Libraries

13 PARAMETER-PASSING METHOD Call-by-value. Pass the value of A[j] Call-by-value. Pass the value of A[j] Call-by-reference. Pass the location of A[j] Call-by-reference. Pass the location of A[j] Call-by-name. Pass the text A[j] itself, avoiding “Name clashes.” Call-by-name. Pass the text A[j] itself, avoiding “Name clashes.”

14 CALL-by-Value A formal parameter corresponds to the value of an actual parameter. A formal parameter corresponds to the value of an actual parameter. Function square(x:integer):integer; Begin square := x*x; End; Square(5); X := 5; Execute the body: Square := x * x := 5 * 5 = 25 Return square

15 CALL-by-Reference A formal parameter becomes a synonym for the location of an actual parameter. A formal parameter becomes a synonym for the location of an actual parameter. Procedure add(a:int;var b:int); Begin b := b + a; End; X := 9; Y := 4; Add(x,y); 9 4 Y X @000F @00AF 9 a @00BB b A := X = 9; B is locate as location of y B := 9 + 4 = 13; Then B&Y := 13; 13

16 Call-by-Value-Result Call-by-value-result is also known as copy- in/copy-out because the actuals are initially copied into the formals and the formals are copied back out to the actual Call-by-value-result is also known as copy- in/copy-out because the actuals are initially copied into the formals and the formals are copied back out to the actual 1. Copy-in phase. Both the values and the locations of the actual parameter are computed 2. Copy-out phase. After the procedure body is executed, the final values of the formals are copied back out to the locations computed in the copy-in phase

17 Legal Ada programs Ada supports 3 kinds of parameter In parameters, corresponding to value parameters, In parameters, corresponding to value parameters, Out parameters, corresponding to the copy-out phase of call –by-value-result, and Out parameters, corresponding to the copy-out phase of call –by-value-result, and In out parameters, corresponding to either reference parameters or value- result parameters. In out parameters, corresponding to either reference parameters or value- result parameters.

18 In C Language In C has only call-by-value parameter- passing method. In C has only call-by-value parameter- passing method. void swapc(int *px,int *py) { int z; z = *px; *px = *py; *py = z; } swapc(&a,&b); px = &a; {pass the address of a to px} py = &b; {pass the address of b to py} z = *px; {assign z the initial value of a} *px = *py; {assign a the value of b} *py = z; {assign b in z}

19 SCOPE FULES FOR NAMES Names are able to denote anything in PLs such as procedures, types, constants and variables Names are able to denote anything in PLs such as procedures, types, constants and variables 2 kinds of scope rules 2 kinds of scope rules Lexical scope rules Lexical scope rules Dynamic scope rules Dynamic scope rules

20 Lexical and Dynamic Scope Lexical scope rules, the binding of name occurrences to declarations can be done statically, at compile time. Lexical scope rules, the binding of name occurrences to declarations can be done statically, at compile time. Dynamic scope rules, the binding of name occurrences to declarations is done dynamiclly, at run time. Dynamic scope rules, the binding of name occurrences to declarations is done dynamiclly, at run time.

21 Lexical and Dynamic Scope program L; var n : char; { n declared in L } procedure W; begin writeln(n); { occurrence of n in W } end; procedure D; var n : char; { n redeclared in D } begin n := ‘ D ’ ; { W called within D } W; end; Begin {program L} n := ‘ L ’ ; W; { W called from the main program L} D; End. LLLL LDLD Dynamic Lexical

22 Lexical Scope and the Renaming of Locals Consider procedure D; rename n to r. Consider procedure D; rename n to r. Only one declaration of n in program, then every time procedure W is called, it write out the value n in L. Only one declaration of n in program, then every time procedure W is called, it write out the value n in L. procedure D; var r:char; begin r:= ‘ D ’ ; W; end; Renaming of local variables. Consistent renaming of local names in the source text has no effect on the computation set up by a program.

23 Macro Expansion and Dynamic Scope If a procedure body is simply copied or substituted at the point of call, we get dynamic scope. A macro processor does: If a procedure body is simply copied or substituted at the point of call, we get dynamic scope. A macro processor does: Actual parameters are textually substituted for the formals. Actual parameters are textually substituted for the formals. The resulting procedure body is textually substituted for the call. The resulting procedure body is textually substituted for the call.

24 Textual substituation Example : C used a macro preprocessor Example : C used a macro preprocessor #define MAXBUF 4 Every occurrence of MAXBUF is replaced by 4 before the program is compiled.

25 Macro expanding Naming Conflicts Naming Conflicts Parameter Passing Parameter Passing procedure D; var n:char; begin n := ‘ D ’ ; W; end; procedure W; begin writeln(n); end; procedure D; var n:char; begin n := ‘ D ’ ; begin writeln(n); end; end; (a) Call of W (b) After macro expansion

26 Nested Scope Binding occurrence : x,y Binding occurrence : x,y Bound occurrence : z Bound occurrence : z procedure swap(var x,y:T); var z : T; begin z:=x; x:=y; y:=z; end;

27 Nested Scopes : Variable Declarations in C Variable declarations can appear within any grouping of statements in C. Compound statements are grouped within braces { and } Variable declarations can appear within any grouping of statements in C. Compound statements are grouped within braces { and } { } { int I=0; While (I<=limit ) {… I++; }

28 Nested compound statements int main(…) { int i; for(…) { int c; if(…) { int i; … } … } …}}…}} int main(…) { int i 1 ; for(…) { int c; if(…) { int i 2 ; … } … } …}}…}}

29 Procedure declaration procedure A1; procedure A2; Procedure A; procedure C1; procedure C11; Procedure C; Program mymain1() Procedure B; Program mymain2()

30 Activition Records Activation of a procedure is storage for the variables declared in the procedure. Activation of a procedure is storage for the variables declared in the procedure. The storage associated with a activation is called an activation record. The storage associated with a activation is called an activation record.

31 Activation binding In a lexically scoped language 3 mappings Compile time. The binding of name occurrences to declarations is defined in terms of the source. Compile time. The binding of name occurrences to declarations is defined in terms of the source. Activation time. The binding of declarations to locations in done at activation time Activation time. The binding of declarations to locations in done at activation time Run time. The binding of locations to values is done dynamically at run time Run time. The binding of locations to values is done dynamically at run time

32 Elements of an Activation Record Data needed for an activation of a procedure is collected in a record called an activation record or frame. Data needed for an activation of a procedure is collected in a record called an activation record or frame. The record contains The record contains Control link Access link Saved state Parameters Function result Local variables


Download ppt "Procedure Activations Programming Language. Exploration Name ocurrenceDeclarationLocationValue scopeactivationstate a.From names to their declarations."

Similar presentations


Ads by Google