Programming Languages and Design Lecture 7 Subroutines and Control Abstraction Instructor: Li Ma Department of Computer Science Texas Southern University,

Slides:



Advertisements
Similar presentations
Semantic Analysis Chapter 6. Two Flavors  Static (done during compile time) –C –Ada  Dynamic (done during run time) –LISP –Smalltalk  Optimization.
Advertisements

Programming Languages and Paradigms
Programming Languages Marjan Sirjani 2 2. Language Design Issues Design to Run efficiently : early languages Easy to write correctly : new languages.
Parameter Passing. Variables: lvalues and rvalues In the assignment statement “X=Y”, variables X and Y are being used in different ways. Y is being used.
Slide 1 Vitaly Shmatikov CS 345 Functions. slide 2 Reading Assignment uMitchell, Chapter 7 uC Reference Manual, Chapters 4 and 9.
COP4020 Programming Languages Subroutines and Parameter Passing Prof. Xin Yuan.
PZ09B Programming Language design and Implementation -4th Edition Copyright©Prentice Hall, PZ09B - Parameter transmission Programming Language Design.
Subprograms A subprogram allows process abstraction (as opposed to data abstraction). Characteristics –single entry point –caller suspended until control.
Chapter 9 Subprograms Specification: name, signature, actions Signature: number and types of input arguments, number and types of output results –Book.
1 Parameter Passing (Section 8.3) CSCI 431 Programming Languages Fall 2003 A compilation of material developed by Felix Hernandez-Campos and Michael Scott.
Subprogram Control - Data sharing Mechanisms to exchange data Arguments - data objects sent to a subprogram to be processed. Obtained through  parameters.
CSCI 330: Programming Language Concepts Instructor: Pranava K. Jha
Lecture 16 Subroutine Calls and Parameter Passing Semantics Dragon: Sec. 7.5 Fischer: Sec Procedure declaration procedure p( a, b : integer, f :
Principles of programming languages 4: Parameter passing, Scope rules Department of Information Science and Engineering Isao Sasano.
CS 326 Programming Languages, Concepts and Implementation Instructor: Mircea Nicolescu Lecture 18.
Chapter 9 Subprogram Control Consider program as a tree- –Each parent calls (transfers control to) child –Parent resumes when child completes –Copy rule.
1 Languages and Compilers (SProg og Oversættere) Sequence control and Subprogram Control.
Chapter 9 Subprograms Sections 1-5 and 9. Copyright © 2007 Addison-Wesley. All rights reserved. 1–2 Introduction Two fundamental abstraction facilities.
ISBN Chapter 9 Subprograms. Copyright © 2006 Addison-Wesley. All rights reserved.1-2 Introduction Two fundamental abstraction facilities.
Run-Time Storage Organization
PSUCS322 HM 1 Languages and Compiler Design II Parameter Passing Material provided by Prof. Jingke Li Stolen with pride and modified by Herb Mayer PSU.
The environment of the computation Declarations introduce names that denote entities. At execution-time, entities are bound to values or to locations:
Parameter Passing. Expressions with lvalues and rvalues An expression has an lvalue/rvalue if it can be placed on the left/right side of an assignment.
Overview Parameter passing modes.
ISBN Chapter 9 Subprograms and Functions –Design Issues –Local Referencing Environments –Parameter-Passing Methods –Parameters that are Subprogram.
Copyright © 2006 The McGraw-Hill Companies, Inc. Programming Languages 2nd edition Tucker and Noonan Chapter 9 Functions It is better to have 100 functions.
Imperative Programming. Heart of program is assignment statements Aware that memory contains instructions and data values Commands: variable declarations,
© 2003 G. Drew Kessler and William M. Pottenger1 Subroutines (Part 1) CSE 262, Spring 2003.
Chapter 8 :: Subroutines and Control Abstraction
Chapter 7: Runtime Environment –Run time memory organization. We need to use memory to store: –code –static data (global variables) –dynamic data objects.
1 Subroutines and Control Abstraction Aaron Bloomfield CS 415 Fall 2005.
CS 2104 Prog. Lang. Concepts Subprograms
COP4020 Programming Languages Subroutines and Parameter Passing Prof. Xin Yuan.
Subprograms Support process abstraction and modularity –characteristics of subprograms are that they have a single entry point the calling entity is suspended.
Copyright © 2005 Elsevier Chapter 8 :: Subroutines and Control Abstraction Programming Language Pragmatics Michael L. Scott.
CSC3315 (Spring 2008)1 CSC 3315 Subprograms Hamid Harroud School of Science and Engineering, Akhawayn University
Subprograms subroutines, procedures, functions, methods.
Programming Languages and Paradigms Imperative Programming.
COP4020 Programming Languages Names, Scopes, and Bindings Prof. Xin Yuan.
1 Languages and Compilers (SProg og Oversættere) Bent Thomsen Department of Computer Science Aalborg University With acknowledgement to Angela Guercio.
1 Subprograms In Text: Chapter 8. 2 Chapter 8: Subprograms Outline Definitions Referencing environments Parameter passing modes and mechanisms Independent.
RUN-Time Organization Compiler phase— Before writing a code generator, we must decide how to marshal the resources of the target machine (instructions,
Copyright © 2006 The McGraw-Hill Companies, Inc. Basic Terminology Value-returning functions: –known as “non-void functions/methods” in C/C++/Java –called.
Slide 1 Dr. Mohammad El-Ramly Fall 2010 Set 7- II - Functions Slides by Vitaly Shmatikov Cairo University Faculty of Computers and Information CS317 Concepts.
Lecture 19: Control Abstraction (Section )
Programming Languages and Design Lecture 6 Names, Scopes and Binding Instructor: Li Ma Department of Computer Science Texas Southern University, Houston.
Chapter 9: Subprograms Introduction Fundamentals of Subprograms
Parameter Passing Mechanisms CS308 Compiler Theory.
CSE 3302 Programming Languages
Procedures and Functions Procedures and Functions – subprograms – are named fragments of program they can be called from numerous places  within a main.
Constructs for Data Organization and Program Control, Scope, Binding, and Parameter Passing. Expression Evaluation.
UNIVERSITY OF SOUTH CAROLINA Department of Computer Science and Engineering CSCE 330 Programming Language Structures Operational Semantics (Slides mainly.
1 CSC 533: Programming Languages Spring 2014 Subprogram implementation  subprograms (procedures/functions/subroutines)  subprogram linkage  parameter.
LECTURE 19 Subroutines and Parameter Passing. ABSTRACTION Recall: Abstraction is the process by which we can hide larger or more complex code fragments.
Arvind Computer Science and Artificial Intelligence Laboratory M.I.T. L05-1 September 21, 2006http:// Types and Simple Type.
Run-Time Environments Presented By: Seema Gupta 09MCA102.
Chapter 9 Subprograms.
Parameter passing Module 12.3 COP4020 – Programming Language Concepts Dr. Manuel E. Bermudez.
Principles of programming languages 4: Parameter passing, Scope rules
Subprograms The basic abstraction mechanism.
CSCI 3370: Principles of Programming Languages Chapter 9 Subprograms
Chapter 9 :: Subroutines and Control Abstraction
Chap. 8 :: Subroutines and Control Abstraction
Chap. 8 :: Subroutines and Control Abstraction
CSC 533: Programming Languages Spring 2015
CSE 3302 Programming Languages
Chapter 9 Subprograms Fundamentals of Subprograms
CSC 533: Programming Languages Spring 2018
CSC 533: Programming Languages Spring 2019
Presentation transcript:

Programming Languages and Design Lecture 7 Subroutines and Control Abstraction Instructor: Li Ma Department of Computer Science Texas Southern University, Houston April, 2008

2 Review of Previous Lectures Introduction to programming languages Syntax specifications of programming languages Semantic specifications of programming languages Functional Programming  Scheme Object-Oriented Programming  C++ & Java Names, binding, and scopes  Static vs. Dynamic

3 Today’s Lecture Parameter passing mechanisms  By constant-value  By result  By value-result  By reference  By name References  “Foundations of Programming Languages: Design and Implementation”, S. H. Roosta, Chapter 3 (§3.2)  “Programming Language Pragmatics”, Michael Scott, Chapter 8 (§8.1 – 8.3)

4 Name is Abstraction Names enable programmers to refer to variables, constants, operations, and types using identifier names Names are control abstractions and data abstractions for program fragments and data structures  Data abstraction: Object-oriented classes hide data representation details behind a set of operations  Control abstraction: Subroutines (procedures and functions) allow programmers to focus on manageable subset of program text Subroutine interface hides implementation details

5 Mechanisms for Processing Control Abstraction Single entry (except FORTRAN, PL/I) Caller is suspended Control returns to caller

6 Subroutine Design Issues Syntax, type checking Parameter passing mechanisms Static or dynamic allocation of locals Static or dynamic scope Environment of parameter functions Overloading Generics Separate compilation

7 Parameters in Subroutine Parameter transmission is the major alternative method for sharing data objects among subroutines Formal parameter, or parameter  Local data object within a subprogram  Specification of the parameter in the invoked subprogram Actual parameter, or argument  Specification of the parameter in the invoking subprogram  A data object shared with the caller program

8 Call by Value Allocate memory for parameter Initialize parameter with the argument’s value Call procedure Nothing happens on return Used in C, C++, Pascal, Lisp, ML, etc. Accesses are usually more efficient with this method But additional storage is required for the formal parameters in the called subprogram

9 Call-by-Value Example int a = 1; void foo (int x) { // a and x have same value, // changes to a or x don’t // affect each other } // argument can be expression foo (a + a); // no modifications to a

10 Call by Result Argument must be variable Allocate memory for parameter Don’t initialize parameter Call Procedure... (within here, parameter will get a value) Copy parameter into argument variable Return from procedure Additional storage is required for the formal parameters in the called subprogram

11 Call-by-Result Example int a = 2; void foo (int x) { // x is not initialized, // changes to a or x don’t // affect each other } // argument must be variable foo (a); // a will be modified

12 Call by Value-Result Copy-In-Copy-Out, or passing by copy Combination of the first two Copy argument value on call Copy result on return Used by Ada for parameters of primitive type in in-out mode Additional storage is required for the formal parameters and time for coping values

13 Call-by-Value-Result Example int a = 3; void foo (int x) { // a and x have same value // changes to a or x don’t // affect each other } // argument must be variable foo (a); // a might be modified

14 Call by Reference A pointer to the memory location of the data object is made available to the called subprogram  Evaluate argument to get the location of the variable Call procedure Nothing happens on return Used by FORTRAN before 1977, Pascal var parameter Passing process is efficient in both time and space But access to the formal parameter is slower due to one more level of indirect addressing

15 Call-by-Reference Example int a = 4; void foo (int x) { // a and x reference same location // changes to a and x // affect each other } // argument can be an expression foo (a); // a might be modified

16 Call by Name Don’t evaluate argument Create closure to evaluate argument Call procedure Evaluate argument by calling parameter closure Nothing happens on return Used by ALGOL-60, Simula-67 Similar to macro expansion (e.g, TeX)

17 Call-by-Name Example int a = 5; void foo (int x) { // x is a function // to get value of argument, // evaluate x() when value is needed } // argument can be an expression foo (a + a); // no modifications to a

18 Languages Comparison in Parameter Passing In Pascal, parameters are passed by value  The key word var is needed if passing by reference Fortran passes all parameters by reference  If the parameter is not an l-value (it is an expression), then the compiler will create a temporary variable to hold the value, and pass this variable by reference In Java, parameters of primitive types are passed by value; Object parameters are passed by sharing  Choose by value or by reference according to the argument (implemented as value or address)

19 Languages Comparison in Parameter Passing (cont’) Parameters in C are always passed by value, it must pass the address of variable explicitly int v1, v2; void swap (int *a, int *b) { int t=*a; *a=*b; *b=t; } … swap (&v1, &v2); C++ introduces an explicit notion of a reference void swap (int &a, int &b) { int t=a; a=b; b=t; }

20 Choose Passing Parameter Method In a language that provides both value and reference parameters (Pascal, Modula), it needs rules to make decision Concern about the efficiency and safety  If need to change the value of an argument – call by reference – efficient  If do not want to change the value of the argument – call by value – safe Concern about the potential cost  Copying the large value from actuals to formals is potentially time-consuming  Accessing a parameter that is passed by reference requires an extra level of indirection

21 Choose Passing Parameter Method (cont’) To combine the efficiency of reference parameters and the safety of value parameters  Modula-3 uses READONLY parameter mode For small parameters, pass a value For large parameters, pass an address  C uses const parameter void append_to_log (const huge_record *r) { … } … append_to_log (&my_record); const applies to the record to which r points The caller passes the address of its record explicitly, the callee will not change the record’s content