Chapter 5 ( ) of Programming Languages by Ravi Sethi

Slides:



Advertisements
Similar presentations
1) Scope a] Ada scope rules b] C scope rules 2) Parameter passing a] Ada parameter modes b) Parameter passing mechanisms COMP205 IMPERATIVE LANGUAGES 13.
Advertisements

Programming Languages and Paradigms
CSI 3120, Implementing subprograms, page 1 Implementing subprograms The environment in block-structured languages The structure of the activation stack.
Recap 1.Programmer enters expression 2.ML checks if expression is “well-typed” Using a precise set of rules, ML tries to find a unique type for the expression.
C++ Programming: Program Design Including Data Structures, Third Edition Chapter 7: User-Defined Functions II.
C++ Programming: From Problem Analysis to Program Design, Third Edition Chapter 7: User-Defined Functions II.
Chapter 7: User-Defined Functions II
Lecture 16 Subroutine Calls and Parameter Passing Semantics Dragon: Sec. 7.5 Fischer: Sec Procedure declaration procedure p( a, b : integer, f :
C++ Programming: Program Design Including Data Structures, Third Edition Chapter 7: User-Defined Functions II.
Chapter 7: User-Defined Functions II Instructor: Mohammad Mojaddam.
Principles of programming languages 4: Parameter passing, Scope rules Department of Information Science and Engineering Isao Sasano.
Fall 2008Programming Development Techniques 1 Topic 2 Scheme and Procedures and Processes September 2008.
ISBN Chapter 10 Implementing Subprograms.
ALGOL 60 Design by committee of computer scientists: Naur, Backus, Bauer, McCarthy, van Wijngaarden, Landin, etc. Design by committee of computer scientists:
1 Chapter 7: Runtime Environments. int * larger (int a, int b) { if (a > b) return &a; //wrong else return &b; //wrong } int * larger (int *a, int *b)
1 Pertemuan 20 Run-Time Environment Matakuliah: T0174 / Teknik Kompilasi Tahun: 2005 Versi: 1/6.
Chapter 9: Subprogram Control
CSC321: Programming Languages1 Programming Languages Tucker and Noonan Chapter 9: Functions 9.1 Basic Terminology 9.2 Function Call and Return 9.3 Parameters.
Chapter 7: Runtime Environment –Run time memory organization. We need to use memory to store: –code –static data (global variables) –dynamic data objects.
CS 2104 Prog. Lang. Concepts Subprograms
Bindings and scope  Bindings and environments  Scope and block structure  Declarations Programming Languages 3 © 2012 David A Watt, University.
Names and Scope. Scope Suppose that a name is used many times for different entities in text of the program, or in the course of execution. When the name.
Compiler Construction
Chapter 5: Programming Languages and Constructs by Ravi Sethi Activation Records Dolores Zage.
Chapter 7 Runtime Environments. Relationships between names and data objects As execution proceeds, the same name can denote different data objects Procedures,
Chapter 6: User-Defined Functions
C++ Programming: From Problem Analysis to Program Design, Fifth Edition, Fifth Edition Chapter 7: User-Defined Functions II.
1 Run-Time Environments. 2 Procedure Activation and Lifetime A procedure is activated when called The lifetime of an activation of a procedure is the.
Basic Semantics Associating meaning with language entities.
Chapter 7 Selection Dept of Computer Engineering Khon Kaen University.
BİL 744 Derleyici Gerçekleştirimi (Compiler Design)1 Run-Time Environments How do we allocate the space for the generated target code and the data object.
CCSA 221 Programming in C CHAPTER 8 – PART 1 WORKING WITH FUNCTIONS 1.
Copyright © 2006 The McGraw-Hill Companies, Inc. Basic Terminology Value-returning functions: –known as “non-void functions/methods” in C/C++/Java –called.
User Defined Methods Methods are used to divide complicated programs into manageable pieces. There are predefined methods (methods that are already provided.
Chapter 9: Subprograms Introduction Fundamentals of Subprograms
Functions Math library functions Function definition Function invocation Argument passing Scope of an variable Programming 1 DCT 1033.
1 MORE ON MODULAR DESIGN: MODULE COMMUNICATIONS. 2 WHEN A FUNCTION IS INVOKED, MEMORY IS ALLOCATED LOCALLY FOR THE FORMAL PARAMETERS AND THE VALUE OF.
7. Runtime Environments Zhang Zhizheng
Chapter 6 Methods Chapter 6 - Methods.
1 Run-Time Environments Chapter 7 COP5621 Compiler Construction Copyright Robert van Engelen, Florida State University, 2005.
10-1 Chapter 10: Implementing Subprograms The General Semantics of Calls and Returns Implementing “Simple” Subprograms Implementing Subprograms with Stack-Dynamic.
ISBN Chapter 10 Implementing Subprograms.
Implementing Subprograms
Constructs for Data Organization and Program Control, Scope, Binding, and Parameter Passing. Expression Evaluation.
Procedure Definitions and Semantics Procedures support control abstraction in programming languages. In most programming languages, a procedure is defined.
Procedure Activations Programming Language. Exploration Name ocurrenceDeclarationLocationValue scopeactivationstate a.From names to their declarations.
1 Compiler Construction Run-time Environments,. 2 Run-Time Environments (Chapter 7) Continued: Access to No-local Names.
Run-Time Environments Presented By: Seema Gupta 09MCA102.
Compilers Principles, Techniques, & Tools Taught by Jing Zhang
Implementing Subprograms
Functions + Overloading + Scope
Run-Time Environments Chapter 7
Functions.
Data Type and Function Prepared for CSB210 Pemrograman Berorientasi Objek By Indriani Noor Hapsari, ST, MT Source: study.
Chapter 7: User-Defined Functions II
Subject Name Compiler Design Subject Code: 10CS63
CS 326 Programming Languages, Concepts and Implementation
Principles of programming languages 4: Parameter passing, Scope rules
Run-Time Storage Organization
Run-Time Storage Organization
User-Defined Functions
Run-Time Environments
Implementing Subprograms
Lecture 15 (Notes by P. N. Hilfinger and R. Bodik)
Run-Time Environments
Chapter 7: User-Defined Functions II
UNIT V Run Time Environments.
Compilers Principles, Techniques, & Tools Taught by Jing Zhang
Implementing Subprograms
Presentation transcript:

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

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

INTRODUCTION TO PROCEDURES 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

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

ELEMENTS OF A PROCEDURE 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; }

RECURSION : MULTIPLE ACTIVATION 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

5.2 PARAMETER PASSING METHODS 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

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

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]

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.

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 AND DYNAMIC SCOPES 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)

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.

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 }

NESTED SCOPES- PROCEDURE DECLARATION IN PASCAL 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.