Chapter 9 Subprograms Sections 1-5 and 9. Copyright © 2007 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 Subprogram Control Consider program as a tree- –Each parent calls (transfers control to) child –Parent resumes when child completes –Copy rule.
Subprograms A subprogram allows process (as opposed to data) abstraction. Characteristics –single entry point –caller suspended until control returns from.
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 © 2006 Addison-Wesley. All rights reserved.1-2 Introduction Two fundamental abstraction facilities.
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.
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.
Programming Languages and Design Lecture 7 Subroutines and Control Abstraction Instructor: Li Ma Department of Computer Science Texas Southern University,
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
Chapter 5 Names, Bindings, and Scopes. Copyright © 2012 Addison-Wesley. All rights reserved.1-2 Chapter 5 Topics Introduction Names Variables The Concept.
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.
ISBN Chapter 5 Names, Bindings, and Scopes.
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 5 Names, Bindings, and Scopes.
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.
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.
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
Names, Bindings, and Scopes
Chapter 9 Subprograms.
Chapter 9 Subprograms.
Presentation transcript:

Chapter 9 Subprograms Sections 1-5 and 9

Copyright © 2007 Addison-Wesley. All rights reserved. 1–2 Introduction Two fundamental abstraction facilities –Process abstraction - subprograms Emphasized from the beginning –Data abstraction - classes Emphasized in the 1980s

Copyright © 2007 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 © 2007 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 © 2007 Addison-Wesley. All rights reserved. 1–5 Basic Definitions Subprogram definition describes interface to and the actions of the subprogram abstraction Subprogram call is explicit request that the subprogram be executed Subprogram header is the first part of the definition, including the name, the kind of subprogram, and the formal parameters Parameter profile (aka signature) is the number, order, and types of the parameters Protocol is parameter profile plus return type for a function

Copyright © 2007 Addison-Wesley. All rights reserved. 1–6 Basic Definitions (continued) Subprogram declaration provides the protocol of the subprogram –Function declarations in C and C++ are often called prototypes Formal parameter is a variable listed in the header and used to access the argument in the subprogram Actual parameter (argument) represents a value or address used in the subprogram call statement

Copyright © 2007 Addison-Wesley. All rights reserved. 1–7 Correspondence between Parameters and Arguments Positional –Actual parameters are bound to formal parameters 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 © 2007 Addison-Wesley. All rights reserved. 1–8 Keyword Parameters Supported in Ada, Fortran 95, Python Python –A function defined by def foo( a, b, c): print a, b, c –can be called with positional arguments foo( 1, 2, 3) –or with keyword arguments foo( b=2, a=1, c=3)

Copyright © 2007 Addison-Wesley. All rights reserved. 1–9 Default Parameter Values Python –A function defined by def foo( a, b=2, c=3): print a, b, c –can be called with 1, 2 or 3 arguments a must be supplied foo(1) foo(2, 3) foo( a=1, c=3)

Copyright © 2007 Addison-Wesley. All rights reserved. 1–10 Defaults without Keyword Parameters C++ –Keyword parameters are not supported –Default values are supported –Parameters with default values must come at end of parameter list –The argument list cannot skip positions It must contain arguments 0 to i where i goes from the last parameter with no default to the last possible argument

Copyright © 2007 Addison-Wesley. All rights reserved. 1–11 Variatic Subprograms Sometimes you need subprograms with a variable number of arguments –printf in C and Java Python –a parameter with a * in front of the name is a list

Copyright © 2007 Addison-Wesley. All rights reserved. 1–12 Local Referencing Environments Local variables can be stack-dynamic (bound to storage at run-time) –Support for recursion –Requires less memory for storage of locals –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 © 2007 Addison-Wesley. All rights reserved. 1–13 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 © 2007 Addison-Wesley. All rights reserved. 1–14 Models of Parameter Passing

Copyright © 2007 Addison-Wesley. All rights reserved. 1–15 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 © 2007 Addison-Wesley. All rights reserved. 1–16 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 © 2007 Addison-Wesley. All rights reserved. 1–17 Pass-by-Result (Out Mode) 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 © 2007 Addison-Wesley. All rights reserved. 1–18 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 © 2007 Addison-Wesley. All rights reserved. 1–19 Pass-by-Reference (Inout Mode) Pass an access path 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 © 2007 Addison-Wesley. All rights reserved. 1–20 Pass-by-Name 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 Macros are a form of pass by name

Copyright © 2007 Addison-Wesley. All rights reserved. 1–21 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)} Pass by Value Result Pass by Reference Pass by value DCBAParameter mode

Copyright © 2007 Addison-Wesley. All rights reserved. 1–22 Implementation of Parameter-Passing In most languages 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 © 2007 Addison-Wesley. All rights reserved. 1–23 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; 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 © 2007 Addison-Wesley. All rights reserved. 1–24 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 Scripting languages (Perl, JavaScript, PHP) do not require type checking

Copyright © 2007 Addison-Wesley. All rights reserved. 1–25 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 Also provides more flexibility

Copyright © 2007 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 © 2007 Addison-Wesley. All rights reserved. 1–27 Ruby Blocks In Ruby, a block is a chunk of code that can be passed to a method –often used with iterators Two forms do end {| | <statements } In method that uses the block, it is executed by typing yield Blocks are closures –retain the environment in which they were called

Copyright © 2007 Addison-Wesley. All rights reserved. 1–28 Thunks