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.

Slides:



Advertisements
Similar presentations
Etter/Ingber Engineering Problem Solving with C Fundamental Concepts Chapter 4 Modular Programming with Functions.
Advertisements

1 Storage Duration and Scope –Local and global variables Storage classes –automatic, static, external, register Todays Material.
Subroutines – parameter passing passing data to/from a subroutine can be done through the parameters and through the return value of a function subroutine.
Programming Languages and Paradigms
Programming C/C++ on Eclipe Trình bày : Ths HungNM C/C++ Training.
C++ Programming: From Problem Analysis to Program Design, Third Edition Chapter 7: User-Defined Functions II.
Chapter 5 ( ) of Programming Languages by Ravi Sethi
Subprogram Control - Data sharing Mechanisms to exchange data Arguments - data objects sent to a subprogram to be processed. Obtained through  parameters.
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 :
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.
Subprograms The basic abstraction mechanism. Functions correspond to the mathematical notion of computation input output procedures affect the environment,
(1) ICS 313: Programming Language Theory Chapter 10: Implementing Subprograms.
Chapter 9 Subprogram Control Consider program as a tree- –Each parent calls (transfers control to) child –Parent resumes when child completes –Copy rule.
C++ Programming: From Problem Analysis to Program Design, Second Edition Chapter 7: User-Defined Functions II.
ISBN Chapter 10 Implementing Subprograms.
Names and Scopes CS 351. Program Binding We should be familiar with this notion. A variable is bound to a method or current block e.g in C++: namespace.
1 ) Data Attributes 2) Declarations, Assignment and Instantiation 3) Global and Local Data 4) Variables 5) Constants 6) Example programs COMP205 IMPERATIVE.
Chapter 6. 2 Objectives You should be able to describe: Function and Parameter Declarations Returning a Single Value Pass by Reference Variable Scope.
The environment of the computation Declarations introduce names that denote entities. At execution-time, entities are bound to values or to locations:
1 CSCI 360 Survey Of Programming Languages 9 – Implementing Subprograms Spring, 2008 Doug L Hoffman, PhD.
1) Causes of errors 2) Classification of errors 3) Signals and exceptions 1) Program hierarchy 2) Blocks 3) Routines 4) Procedures and functions COMP205.
Shallow Versus Deep Copy and Pointers Shallow copy: when two or more pointers of the same types point to the same memory – They point to the same data.
© 2003 G. Drew Kessler and William M. Pottenger1 Subroutines (Part 1) CSE 262, Spring 2003.
Chapter 8 :: Subroutines and Control Abstraction
Names Variables Type Checking Strong Typing Type Compatibility 1.
5-1 Chapter 5: Names, Bindings, Type Checking, and Scopes Variables The Concept of Binding Type Checking Strong Typing Type Compatibility Scope and Lifetime.
CS 2104 Prog. Lang. Concepts Subprograms
1 Names, Scopes and Bindings Aaron Bloomfield CS 415 Fall
Procedures and Functions Computing Module 1. What is modular programming? Most programs written for companies will have thousands of lines of code. Most.
C++ Programming: From Problem Analysis to Program Design, Fifth Edition, Fifth Edition Chapter 7: User-Defined Functions II.
1 Scope Rules (Section 3.3) CSCI 431 Programming Languages Fall 2003 A compilation of material developed by Felix Hernandez-Campos and Michael Scott.
Basic Semantics Associating meaning with language entities.
1 Scope Scope describes the region where an identifier is known, and semantic rules for this.
1 Type Checking Type checking ensures that the operands and the operator are of compatible types Generalized to include subprograms and assignments Compatible.
Implementing Subprograms What actions must take place when subprograms are called and when they terminate? –calling a subprogram has several associated.
Structure of Programming Languages Names, Bindings, Type Checking, and Scopes.
Concepts of programming languages Chapter 5 Names, Bindings, and Scopes Lec. 12 Lecturer: Dr. Emad Nabil 1-1.
Chapter Functions 6. Modular Programming 6.1 Modular Programming Modular programming: breaking a program up into smaller, manageable functions or modules.
1 Bindings. 2 Outline Preliminaries Scope  Block structure  Visibility Static vs. dynamic binding Declarations and definitions More about blocks The.
1 Structure of Compilers Lexical Analyzer (scanner) Modified Source Program Parser Tokens Semantic Analysis Syntactic Structure Optimizer Code Generator.
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
Department of Electronic & Electrical Engineering Functions Parameters Arguments Pointers/dereference & * Scope Global/Local Storage Static/Automatic.
Names, Scope, and Bindings Programming Languages and Paradigms.
CHAPTER 8 Scope, Lifetime, and More on Functions.
Building Programs from Existing Information Solutions for programs often can be developed from previously solved problems. Data requirements and solution.
ISBN Chapter 10 Implementing Subprograms.
Advanced Programming in C
Implementing Subprograms
Implementing Subprograms
Chapter 7: User-Defined Functions II
CS 326 Programming Languages, Concepts and Implementation
C Functions -Continue…-.
Type Checking, and Scopes
Principles of programming languages 4: Parameter passing, Scope rules
Subprograms The basic abstraction mechanism.
C-language Lecture By B.S.S.Tejesh, S.Neeraja Asst.Prof.
Implementing Subprograms
Scope, Visibility, and Lifetime
Implementing Subprograms
Subroutines – parameter passing
Classes and Objects.
Names and Binding In Text: Chapter 5.
Submitted By : Veenu Saini Lecturer (IT)
Implementing Subprograms
Scope Rules.
Presentation transcript:

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. PROGRAM COMPOSITION II

Scope rules govern the visibility and life time of data items (i.e. the parts of a program where they can be used). They also bind names to types. 1) Where this is determined at compile time this is called static binding. The opposite is dynamic binding. 2) Dynamic scope is a feature of logic languages such as PROLOG and functional languages such as LISP. The advantages of static scoping is that it allows type checking to be carried out at compile time. In most imperative languages the scope of a declaration starts at the end of the declaration and extends to the end of the block. SCOPE RULES

ADA SCOPE RULES

procedure SUM_AND_PROD is X_ITEM : constant := 2; Y_ITEM : constant := X_ITEM*2; begin PUT(X_ITEM+Y_ITEM); NEW_LINE; PUT(X_ITEM*Y_ITEM); NEW_LINE; end SUM_AND_PROD; [A] A data item is visible from where it is declared to the end of the block in which the declaration is made.

[B] A data item declared within a block cannot be seen from outside that block, and is referred to as local data item, i.e. local to a block.

procedure SUM(P_ITEM, Q_ITEM : INTEGER) is TOTAL : INTEGER ; begin TOTAL := P_ITEM + Q_ITEM; PUT(TOTAL); NEW_LINE; end SUM; procedure PRODUCT(S_ITEM, T_ITEM : INTEGER) is PROD : INTEGER ; begin PROD := S_ITEM * T_ITEM; PUT(PROD); NEW_LINE; end PRODUCT;

[C] Anything declared in a block is also visible in all enclosed blocks within it (in which the data item is not redeclared).

procedure SUM_AND_PRODUCT(P_ITEM, Q_ITEM: INTEGER) is ANSWER : INTEGER; procedure SUM(A_ITEM,B_ITEM: INTEGER) is begin ANSWER := A_ITEM + B_ITEM; end SUM; procedure PRODUCT(C_ITEM,D_ITEM: INTEGER) is begin ANSWER := C_ITEM * D_ITEM; end PRODUCT; begin SUM(P_ITEM,Q_ITEM); PUT(ANSWER); NEW_LINE; PRODUCT(P_ITEM,Q_ITEM); PUT(ANSWER); NEW_LINE; end SUM_AND_PRODUCT;

[D] Several data items with the same name cannot be used in the same block, however several items with the same name can be declared in "different” blocks. In this case the declared quantities have nothing to do with one another (other than sharing the same name).

procedure SUM(P_ITEM, Q_ITEM : INTEGER) is TOTAL : INTEGER; begin TOTAL := P_ITEM + Q_ITEM; PUT(TOTAL); NEW_LINE; end SUM; procedure PRODUCT(P_ITEM, Q_ITEM : INTEGER) is TOTAL : INTEGER; begin TOTAL := P_ITEM * Q_ITEM; PUT(TOTAL); NEW_LINE; end PRODUCT;

[E] Where a name is used in a block and reused in a sub-block nested within it, the sub -block declaration will override the super-block declaration during the life time of the sub-block. This is referred to as occlusion. The identifier which is hidden because of the inner declaration is said to be occluded.

procedure SUM_AND_PROD is X_ITEM : constant := 2; Y_ITEM : constant := X_ITEM*2; procedure SUM(P_ITEM, Q_ITEM : INTEGER) is X_ITEM : INTEGER; begin X_ITEM := P_ITEM + Q_ITEM; PUT(X_ITEM); NEW_LINE; end SUM; begin SUM(X_ITEM,Y_ITEM); end SUM_AND_PROD;

1 ) Scope of declaration extends from where it is made to the end of the block in which it is made. 2) Anything declared in a block is not visible outside of that block. 3) Anything declared in a block is visible to all enclosed blocks within it. 4) Several items with the same name cannot be used in the same block, however several items with the same name can be declared in different blocks. 5) Where a name is used in a block and reused in a sub- block nested within it, the sub-block declaration will override (occlude) the super-block declaration during the life time of the sub-block. ADA SCOPE RULES SUMMARY

C SCOPE RULES

The scope of a data item in C is governed by its storage class. Generally data items belong to one of two storage classes: –1) extern ( external) - used to define global variables visible throughout a program. –2) auto (automatic) - used to define local variables (including formal parameters to functions) visible only inside a block. They exist only while the block of code in which they are declared is executing. Note that a local variable overrides a global variable of the same name. C also supports two other storage classes static and register. C SCOPE RULES

PARAMETER PASSING a) Ada parameter modes b) Parameter passing mechanisms

Parameters can be classified into three groups or modes. They can be used to: 1) Pass information to a routine. 2) Receive information from a routine. 3) Pass information to a routine where it is updated before being returned. Ada identifies these as the in, out and in out parameter modes. PARAMETER PASSING

procedure EXAMPLE is X: integer:= 1; Y: integer:= 2; Z: integer:= 3; procedure DO_IT (A: in integer; B: in out integer; C: out integer) is begin C:= B; B:= B+A; end DO_IT; begin put(X);put(Y); put(Z); new_line; DO_IT(X,Y,Z); put(X);put(Y);put(Z); new_line; end EXAMPLE; ADA PARAMETER MODES Note that by default a parameter is an in parameter

We can identify a number of types of parameter passing mechanism: 1) Call by value 2) Call by constant value. 3) Call by reference. 4) Call by reference-value. 5) Call by result. 6) Call by copy-restore or value-result. PARAMETER PASSING MECHANISMS

Call by value is the most common and is used by languages such as C, Pascal, Modula-2, ALGOL 60. The formal parameter acts as a local variable which is initialised with the value of the actual parameter and may then be changed. However any changes made to the formal parameter will no effect the value of the actual parameter. CALL BY VALUE

void main(void) { int a = 1, b = 2, c = 3; printf("Before doit: a=%d, b=%d, c=%d\n",a,b,c); doit(a,b,c); printf("After doit: a=%d, b=%d, c=%d\n",a,b,c); }... void doit(int a, int b, int c) { printf("Start of doit: a=%d, b=%d, c=%d\n",a,b,c); a = b*100; b = b+c; printf("End of doit: a=%d, b=%d, c=%d\n",a,b,c); } Before doit: a = 1, b = 2, c = 3 Start of doit: a = 1, b = 2, c = 3 End of doit: a = 200, b = 5, c = 3 After doit: a = 1, b = 2, c = 3

In languages such as Ada (also ALGOL 68) the formal parameter is a local constant rather than a local variable and thus can not be changed. The latter mechanism is thus referred to as call by constant value. CALL BY CONSTANT VALUE procedure EXAMPLE is N1: float:= 4.2; N2: float:= 9.6; procedure MEAN_VALUE (X1, X2: in float) is begin put((X1+X2)/2.0); new_line; end MEAN_VALUE; begin MEAN_VALUE(N1, N2); end EXAMPLE;

The disadvantage of call by value (and call by constant value) is that a copy is always made of the actual parameter to obtain the formal parameter. The need to make a copy can be avoided using the call be reference mechanism. Found in languages such as ALGOL 68, MODULA II and Pascal using VAR parameter mode. In call be reference the formal parameters become "aliases" for the actual parameters. Consequently everything that happens to the formal parameters also happens to the actual parameters. Approach has all the dangers of aliasing. CALL BY REFERENCE

program PASSING (input, output); var VALUE_1, VALUE_2: real; procedure MEAN_VALUE (NUM_1: real; var NUM_2: real); begin NUM_2 := (NUM_1+NUM_2)/2; end; begin readln(VALUE_1, VALUE_2); write('VALUE_1 = '); write(VALUE_1); write(', VALUE_2 = '); write(VALUE_2); writeln; MEAN_VALUE(VALUE_1, VALUE_2); write('MEAN VALUE_2 = '); write(VALUE_2); writeln; end.

Supported by C In call by reference value the address of the actual parameter is passed. This can then be used by the called routine to access the actual parameters. Consequently, in C, we can simulate the effect of call be reference using the call by reference value mechanism. CALL BY REFERENCE VALUE

void main(void) { float n1 = 4.2, n2 = 9.6; meanValue(&n1, n2); printf("Mean = %f\n",n1); } void meanValue(float *n1, float n2) { printf("n1 = %f, n2 = %f\\n",*n1,n2); *n1 = (*n1+n2)/2.0; } When used elsewhere it is interpreted as "the value contained at the address pointed at" (i.e. the pointer is dereferenced). Remember that: The * operator when used in a declaration defines a "pointer to a data type". The operator & is interpreted as "the address of...". n1 = , n2 = Mean =

Call by result is used in Ada to implement out mode parameters. The formal parameter acts as an uninitialised local variable which is given a value during execution of the procedure. The value of the formal parameter is then assigned to the actual parameter on returning from the routine. CALL BY RESULT

procedure EXAMPLE is N1: float:= 4.2; N2: float:= 9.6; MEAN: float; procedure MEAN_VALUE (X1, X2: in float; X3: out float) is begin X3:= (X1+X2)/2.0; end MEAN_VALUE; begin MEAN_VALUE(N1, N2, MEAN); put(MEAN); new_line; end EXAMPLE;

Call by copy restore is an amalgamation of call by value and call by result. The formal parameter acts as a local variable which is initialised to the value of the actual parameter. Within the routine, changes to the formal parameter only affect the local copy. On returning from the routine the final value of the formal parameter is assigned to the actual parameter. Call by copy restore is supported by Ada to achieve "in- out" parameter operation. Has the same disadvantages as those associated with call by value. CALL BY COPY RESTORE

procedure EXAMPLE is N1: float:= 4.2; N2: float:= 9.6; procedure MEAN_VALUE (X1: in out float; X2: in float) is begin X1:= (X1+X2)/2.0; end MEAN_VALUE; begin MEAN_VALUE(N1, N2); put(N1); new_line; end EXAMPLE;

SUMMARY 1) Scope a] Ada scope rules b] C scope rules 2) Parameter passing a] Ada parameter modes b) Parameter passing mechanisms