Presentation is loading. Please wait.

Presentation is loading. Please wait.

Chapter 5 ( ) of Programming Languages by Ravi Sethi

Similar presentations

Presentation on theme: "Chapter 5 ( ) of Programming Languages by Ravi Sethi"— Presentation transcript:

1 Chapter 5 (5.1 - 5.4) of Programming Languages by Ravi Sethi

2 PROCEDURES Parameter Passing Methods Scope rules for Names
Introduction to Procedures Parameter Passing Methods Scope rules for Names Nested Scope in the Source Text

Procedures are constructs for giving a name to a piece of coding(body) When the name is called , the body is executed. Function Procedures Functions Proper Procedures Procedures Functions Return a single value Procedures have only a side effect such as setting variables or performing output and returns no value

4 Procedure Calls Use of a Procedure is referred to as a call of Procedure < Procedure - name > ( < parameters> ) The parenthesis around parameters are a syntactic cue to a call Functions are called from within expressions example: r * sin( angle ) Procedures are treated as Atomic statements example : read(ch) ; Actual parameter

A name for the declared Procedure A body consisting of local declaration and statements The formal parameters which are place holders for actuals An optional result type Example function square ( x : integer): integer (pascal) begin square := x * x end ; Example int square ( int x) ( C) { int sq; sq = x * x; return sq; }

Activation - Each execution of a procedure body is referred to as an activation of the procedure Recursion - A procedure is recursive if it can be activated from within its own procedure body Example- Factorial function function f( n : integer) : integer; begin if n = 0 then f := 1 else f := n * f ( n - 1 ) end ; f(n) is computed in terms of f(n-1), f(n-1) in terms of f(n-2) and so on for n = 3 the sequence of activation is a s follows 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

If communication is desired between the caller and the callee , arrangements must be made for passing values back and forth through the procedures parameters. Parameter passing refers to the matching of actuals with formals when a Procedure call occurs Different interpretations of what a parameter stands for leads to different parameter passing methods. Call by Value Call by Reference

8 copied into parameter variables
Value Parameter Gets Own Memory location Gets initial value from corresponding actual position Uses but does not change the actual parameter Actual parameter s can be variables or expressions of a return type Example b = future_value(total/2, rate, year2-year1). Float future_value(float initial_balance, float p, int nyear) { p = 1 + p/12/100; int n = 12 * nyear; float b = initial_balance* pow(p, n) return b; } main future_value total total 1/2 initial_balance rate p rate year1 nyear year 2 year2-year1 b Values are copied into parameter variables expressions

9 Reference Parameters changes the value of the actual Parameter Shares the memory location of the actual Parameter Must match in type The Actual Reference Parameter must have Location Example procedure swap(var x : integer; var y : integer ); var z : integer; begin z := x; x := y; y := z; end A Call swap(i, A[i]) does the following make the location of x the same as that of i; make the location of y the same as that of A[i]; if i=2 and A[i] = 99 z := 2; i := 99; A[2] := z Thus these assignments exchange values in i and A[i]

10 Program execution always begins in the main
OBSERVATIONS Program execution always begins in the main Formal Parameters(function definition) and actual Parameters (function call) are matched by position. There names need not agree Data types of parameters do not appear in the function call When a function completes the flow of control returns to the place that called it.

11 SCOPE RULES FOR NAMES The Scope rules of a language determine which declaration of a name x applies to an occurrence of x in a program . There are two kinds of scope rules, called lexical and dynamic scope rules. Binding and Scope Consider the following Pascal Procedure procedure swap(var x, y: T) var z : T; begin z := x; x := y; y := z end Binding Occurrence of z Scope of z Bound Occurrence of z The Procedure declaration also contains binding occurrences of the procedure name swap,the formal parameters x and y .The Scopes of the formal parameters x and y and the scope of the variable z consists of the procedure body.

Lexical Scope Also called Static Scope Binding of name occurrences to declarations done statically, at compile time A variable that is free in a procedure gets its value from the environment in which the procedure is defined, rather than from where the procedure is called binding of variable is defined by the structure of the program and not by what happens at the run time. V,W,X (block A) V,Y (block B) V,W,Z (block C)

13 Dynamic Scope The binding of name occurrences to declarations is done dynamically at run time A free variable gets its value from the environment from which it is called , rather than from the environment in which it is defined. Dynamic binding should not be confused with dynamic variables which are either reference variables or local variables.

14 Program L; var n : char procedure W; begin writeln(n) end; procedure D; var n : char; n := ‘D’ ; W begin { L } n := ‘L’ ; W; D end. { n declared in L } { Occurrence of n in W } { n redeclared in D } { W called within D } { W called from the main program L }

Program nested (Input, Output); var X,Y : Real ; Scope of Y Procedure Outer (var X : Real); Scope of M var M,N : Integer ; Procedure Inner ( Z : Real); Scope of Z var N,O : Integer ; begin { Inner} ……….. end : { Inner} begin { outer} end { outer } begin { Nested } end Nested.

Download ppt "Chapter 5 ( ) of Programming Languages by Ravi Sethi"

Similar presentations

Ads by Google