ISBN 0-321-33025-0 Chapter 9 Subprograms. Copyright © 2006 Addison-Wesley. All rights reserved.1-2 Introduction Two fundamental abstraction facilities.

Slides:



Advertisements
Similar presentations
ICE1341 Programming Languages Spring 2005 Lecture #16 Lecture #16 In-Young Ko iko.AT. icu.ac.kr iko.AT. icu.ac.kr Information and Communications University.
Advertisements

Programming Languages and Paradigms
Names and Bindings.
Overview Fundamentals of Subprograms (Sec )
Chapter 9 Subprograms Specification: name, signature, actions Signature: number and types of input arguments, number and types of output results –Book.
ISBN Chapter 9 Subprograms. Copyright © 2007 Addison-Wesley. All rights reserved.1-2 Chapter 9 Topics Introduction Fundamentals of Subprograms.
Chapter 9 Subprograms.
1 CHAPTER 9 SUBPROGRAM. 2 SUBPROGRAM Topics: b Definitions of subprogram b general subprogram characteristics b parameters b Functions and procedures.
Chapter 9 Subprograms.
Chapter 9 Subprograms.
Chapter 9 Subprograms Sections 1-5 and 9. Copyright © 2007 Addison-Wesley. All rights reserved. 1–2 Introduction Two fundamental abstraction facilities.
Subprograms A subprogram allows process (as opposed to data) abstraction. Characteristics –single entry point –caller suspended until control returns from.
ISBN Chapter 10 Implementing Subprograms.
ISBN Chapter 5 Names, Bindings, Type Checking, and Scopes Names Variables The Concept of Binding Type Checking Strong Typing Type Compatibility.
ISBN Chapter 9 Subprograms. Copyright © 2004 Pearson Addison-Wesley. All rights reserved.9-2 Chapter 9 Topics Introduction Fundamentals.
ISBN Chapter 9 Subprograms CE2004 Principles of Programming Languages.
ISBN Chapter 9 Subprograms and Functions –Design Issues –Local Referencing Environments –Parameter-Passing Methods –Parameters that are Subprogram.
© 2003 G. Drew Kessler and William M. Pottenger1 Subroutines (Part 1) CSE 262, Spring 2003.
1 Subprograms Fundamentals of subprograms Design issues for subprograms –Parameter-passing methods –Type checking of parameters –Static or dynamic storage.
Chapter 9 Subprograms.
Chapter 9 Subprograms. Copyright © 2012 Addison-Wesley. All rights reserved.1-2 Chapter 9 Topics Introduction Fundamentals of Subprograms Design Issues.
ISBN Chapter 9 Subprograms. Copyright © 2007 Addison-Wesley. All rights reserved.1-2 Chapter 9 Topics Introduction Fundamentals of Subprograms.
ISBN Chapter 9 Subprograms. Copyright © 2009 Addison-Wesley. All rights reserved.1-2 Chapter 9 Topics Introduction Fundamentals of Subprograms.
Chapter Nine: Subprograms Lesson 09. What are they  Modularized code  Might return a value  Functions  Or not  Procedures  Subroutines  In object.
5-1 Chapter 5: Names, Bindings, Type Checking, and Scopes Variables The Concept of Binding Type Checking Strong Typing Type Compatibility Scope and Lifetime.
March 12, ICE 1341 – Programming Languages (Lecture #6) In-Young Ko Programming Languages (ICE 1341) Lecture #6 Programming Languages (ICE 1341)
Subprograms Support process abstraction and modularity –characteristics of subprograms are that they have a single entry point the calling entity is suspended.
Chapter 9 Subprograms Fundamentals of Subprograms
1 Chapter 9 © 2002 by Addison Wesley Longman, Inc. 9.2 Fundamentals of Subprograms - General characteristics of subprograms: 1. A subprogram has a single.
1 Copyright © 1998 by Addison Wesley Longman, Inc. Chapter 8 Fundamental Characteristics of Subprograms 1. A subprogram has a single entry point 2. The.
Introduction A variable can be characterized by a collection of properties, or attributes, the most important of which is type, a fundamental concept in.
ISBN Chapter 9 Subprograms. 1-2 Chapter 9 Topics Introduction Fundamentals of Subprograms Design Issues for Subprograms Local Referencing.
CSC3315 (Spring 2008)1 CSC 3315 Subprograms Hamid Harroud School of Science and Engineering, Akhawayn University
Week 7 Subprograms. Copyright © 2006 Addison-Wesley. All rights reserved.1-2 Chapter 9 Topics Introduction Fundamentals of Subprograms Design Issues for.
Subprograms subroutines, procedures, functions, methods.
ISBN Chapter 9 Subprograms. Copyright © 2015 Pearson. All rights reserved.1-2 Chapter 9 Topics Introduction Fundamentals of Subprograms.
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.
Chapter 9 Subprograms. 2 Fundamentals of Subprograms Each subprogram has a single entry point The calling program is suspended during execution of the.
C HAPTER 9 Subprograms. CCSB314 Programming Language C HAPTER 9 T OPICS Introduction Fundamentals of Subprograms Design Issues for Subprograms Local Referencing.
1 Subroutines Structure of Programming Language. 2 Procedures and Functions There are two categories of subprograms Function: subroutine that returns.
ISBN Chapter 9 Subprograms Sections
Names, Bindings, and Scope Session 3 Course : T Programming Language Concept Year : February 2011.
CSI 3125, Subprograms, page 1 Subprograms General concepts Parameter passing Functions Subprograms as parameters.
Concepts of programming languages Chapter 5 Names, Bindings, and Scopes Lec. 12 Lecturer: Dr. Emad Nabil 1-1.
Chapter 9 Subprograms. Copyright © 2006 Addison-Wesley. All rights reserved.1-2 Chapter 9 Topics Introduction Fundamentals of Subprograms Design Issues.
Chapter 9: Subprograms Introduction Fundamentals of Subprograms
ISBN Chapter 9 Subprograms. Corrected and improved by Assoc. Prof. Zeki Bayram, EMU, North Cyprus. Original Copyright © 2007 Addison-Wesley.
9-1 Chapter 9 Subprograms Topics Fundamentals of Subprograms Design Issues for Subprograms Local Referencing Environments Parameter-Passing Methods Parameters.
ISBN Variables, Names, Scope and Lifetime ICOM 4036 Lecture 9.
1 CSC 533: Programming Languages Spring 2014 Subprogram implementation  subprograms (procedures/functions/subroutines)  subprogram linkage  parameter.
Chapter 9 Chapter 9 Subprograms. The Structure of Run-Time Memory.
ISBN Chapter 9 Subprograms. Copyright © 2007 Addison-Wesley. All rights reserved.1-2 Chapter 9 Topics Introduction Fundamentals of Subprograms.
Chapter 9 Subprograms. Chapter 9 Topics Introduction Fundamentals of Subprograms Design Issues for Subprograms Local Referencing Environments Parameter-Passing.
ISBN Chapter 12 Support for Object-Oriented Programming.
Chapter 9 Subprograms.
Chapter 9 Subprograms.
Chapter 9 Subprograms.
CSCI 3370: Principles of Programming Languages Chapter 9 Subprograms
Chapter 9 Subprograms.
Chapter 9 Subprograms.
Chapter 9: Subprograms Sangho Ha
Subprograms In Text: Chapter 9.
Subject : T0152 – Programming Language Concept
Chapter 9 Subprograms Fundamentals of Subprograms
Chapter 9 Subprograms.
Chapter 9 Subprograms.
Chapter 9 Subprograms Subprograms are the fundamental building blocks of programs and are therefore the most important concepts in programming language.
Presentation transcript:

ISBN Chapter 9 Subprograms

Copyright © 2006 Addison-Wesley. All rights reserved.1-2 Introduction Two fundamental abstraction facilities –Process abstraction - subprograms Emphasized from early days –Data abstraction - classes Emphasized in the1980s

Copyright © 2006 Addison-Wesley. All rights reserved.1-3 Design Issues for Subprograms What parameter passing methods are provided? Are parameter types checked? Are local variables static or dynamic? Can subprogram definitions appear in other subprogram definitions? Can subprograms be overloaded? Can subprogram be generic? Any limits on parameter and return types?

Copyright © 2006 Addison-Wesley. All rights reserved.1-4 Subprograms A subprogram is a collection of statements that can be executed from multiple points within a program –Procedures are collection of statements that define parameterized operations void methods –Functions structurally resemble procedures but are semantically modeled on mathematical functions Return values They are expected to produce no side effects In practice, program functions have side effects

Copyright © 2006 Addison-Wesley. All rights reserved.1-5 Basic Definitions A subprogram definition describes the interface to and the actions of the subprogram abstraction A subprogram call is an explicit request that the subprogram be executed A subprogram header is the first part of the definition, including the name, the kind of subprogram, and the formal parameters The parameter profile (aka signature) of a subprogram is the number, order, and types of its parameters The protocol is a subprogram’s parameter profile and, if it is a function, its return type

Copyright © 2006 Addison-Wesley. All rights reserved.1-6 Basic Definitions (continued) A subprogram declaration provides the protocol, but not the body, of the subprogram –Function declarations in C and C++ are often called prototypes A formal parameter is a dummy variable listed in the subprogram header and used in the subprogram An actual parameter represents a value or address used in the subprogram call statement

Copyright © 2006 Addison-Wesley. All rights reserved.1-7 Correspondence between Parameters and Arguments Positional –The binding of actual parameters to formal parameters is by position: the first actual parameter is bound to the first formal parameter and so forth –Safe and effective Keyword –The name of the formal parameter to which an actual parameter is to be bound is specified with the actual parameter –Parameters can appear in any order

Copyright © 2006 Addison-Wesley. All rights reserved.1-8 Local Referencing Environments Local variables can be stack-dynamic (bound to storage at run-time) –Advantages Support for recursion Storage for locals is shared among some subprograms –Disadvantages Allocation/de-allocation, initialization time Indirect addressing Subprograms cannot be history sensitive Local variables can be static –More efficient (no indirection) –No run-time overhead –Cannot support recursion

Copyright © 2006 Addison-Wesley. All rights reserved.1-9 Parameter Passing Methods We discuss these at several different levels: –Semantic models in mode out mode inout mode –Conceptual models of transfer: 1. Physically move a value 2. Move an access path

Copyright © 2006 Addison-Wesley. All rights reserved.1-10 Models of Parameter Passing

Copyright © 2006 Addison-Wesley. All rights reserved.1-11 Parameter Passing Methods Ways in which parameters are transmitted to and/or from called subprograms –Pass-by-value –Pass-by-result –Pass-by-value-result –Pass-by-reference –Pass-by-name

Copyright © 2006 Addison-Wesley. All rights reserved.1-12 Pass-by-Value (In Mode) The value of the actual parameter is used to initialize the corresponding formal parameter –Normally implemented by copying –Can be implemented by transmitting an access path but not recommended (enforcing write protection is not easy) –When copies are used, additional storage is required –Storage and copy operations can be costly

Copyright © 2006 Addison-Wesley. All rights reserved.1-13 Pass-by-Result (Out Mode) When a parameter is passed by result, no value is transmitted to the subprogram; the corresponding formal parameter acts as a local variable; its value is transmitted to caller’s actual parameter when control is returned to the caller –Require extra storage location and copy operation Potential problem: sub(p1, p1); whichever formal parameter is copied back last will represent the current value of p1

Copyright © 2006 Addison-Wesley. All rights reserved.1-14 Pass-by-Value-Result (inout Mode) A combination of pass-by-value and pass-by-result Sometimes called pass-by-copy Formal parameters have local storage Disadvantages: –Those of pass-by-result –Those of pass-by-value

Copyright © 2006 Addison-Wesley. All rights reserved.1-15 Pass-by-Reference (Inout Mode) Pass an access path Also called pass-by-sharing Passing process is efficient (no copying and no duplicated storage) Disadvantages –Slower accesses (compared to pass-by- value) to formal parameters –Potentials for un-wanted side effects –Un-wanted aliases (access broadened)

Copyright © 2006 Addison-Wesley. All rights reserved.1-16 Pass-by-Name (Inout Mode) By textual substitution Formals are bound to an access method at the time of the call, but actual binding to a value or address takes place at the time of a reference or assignment Allows flexibility in late binding

Copyright © 2006 Addison-Wesley. All rights reserved.1-17 Implementing Parameter-Passing Methods In most language parameter communication takes place thru the run- time stack Pass-by-reference are the simplest to implement; only an address is placed in the stack A subtle but fatal error can occur with pass-by-reference and pass-by-value- result: a formal parameter corresponding to a constant can mistakenly be changed

Copyright © 2006 Addison-Wesley. All rights reserved.1-18 In Practice FORTRAN started out using pass-by-reference, later allowed pass-by-value-result ALGOL 60 used pass-by-name by default with pass-by- value optional C uses pass-by-value; can get pass-by-reference using pointers Pascal, Modula-2 and C++ use pass-by-value by default; pass-by-reference is optional Ada provides all three semantic modes Java uses pass-by-value exclusively but use of object references results in pass-by-reference behavior of objects

Copyright © 2006 Addison-Wesley. All rights reserved.1-19 Type Checking Parameters Considered very important for reliability FORTRAN 77 and original C: none Pascal, FORTRAN 90, Java, and Ada: it is always required ANSI C and C++: choice is made by the user –Prototypes Relatively new languages Perl, JavaScript, and PHP do not require type checking

Copyright © 2006 Addison-Wesley. All rights reserved.1-20 Design Considerations for Parameter Passing Two important considerations –Efficiency –One-way or two-way data transfer But the above considerations are in conflict –Good programming suggest limited access to variables, which means one-way whenever possible –But pass-by-reference is more efficient to pass structures of significant size

Copyright © 2006 Addison-Wesley. All rights reserved.1-21 Overloaded Subprograms An overloaded subprogram is one that has the same name as another subprogram in the same referencing environment –Every version of an overloaded subprogram has a unique protocol C++, Java, C#, and Ada include predefined overloaded subprograms In Ada, the return type of an overloaded function can be used to disambiguate calls (thus two overloaded functions can have the same parameters) Ada, Java, C++, and C# allow users to write multiple versions of subprograms with the same name

Copyright © 2006 Addison-Wesley. All rights reserved.1-22 User-Defined Overloaded Operators Most languages overload operators to work for different types Operators can be overloaded in Ada and C++

Copyright © 2006 Addison-Wesley. All rights reserved.1-23 Generic Subprograms A generic or polymorphic subprogram takes parameters of different types on different activations Overloaded subprograms provide ad hoc polymorphism A subprogram that takes a generic parameter that is used in a type expression that describes the type of the parameters of the subprogram provides parametric polymorphism

Copyright © 2006 Addison-Wesley. All rights reserved.1-24 Templates in C++ In C++, you can define template functions template Type max(Type first, Type second) { return first > second ? first : second; } The above template can be instantiated for any type for which operator > is defined max(23, 56) Causes the compiler to generate the definition int max (int first, int second) { return first > second? first : second; } You can also define template classes

Copyright © 2006 Addison-Wesley. All rights reserved.1-25 Java Generics Starting with Java 1.5, you can define generic classes in Java All the collection classes have been redefined as generics Hashmap symbolTable = new Hashmap ();

Copyright © 2006 Addison-Wesley. All rights reserved.1-26 Return Values for Functions What types of return values are allowed? –Most imperative languages restrict the return types –C allows any type except arrays and functions –C++ is like C but also allows user-defined types –Ada allows any type –Java and C# methods can return any type –Scheme can return functions

Copyright © 2006 Addison-Wesley. All rights reserved.1-27 Parameter Passing Example Main { A = 2; B = 5; C = 8; D = 9; P(A, B, C, D); write(A, B, C, D);} P(U, V, W, X) { V = U+A; W = A+B; A = A+1; X = A+2; write(U, V, W, X)} Parameter modeABCD Pass by value Pass by Reference Pass by Value Result Pass by Result