© 2003 G. Drew Kessler and William M. Pottenger1 Subroutines (Part 1) CSE 262, Spring 2003.

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

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.
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.
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.
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 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.
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.
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.
1 Subprograms Fundamentals of subprograms Design issues for subprograms –Parameter-passing methods –Type checking of parameters –Static or dynamic storage.
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.
CS 2104 Prog. Lang. Concepts Subprograms
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 9 Functions It is better to have 100 functions operate on one data structure than 10 functions on 10 data structures. A. Perlis.
April 23, ICE 1341 – Programming Languages (Lecture #16) In-Young Ko Programming Languages (ICE 1341) Lecture #16 Programming Languages (ICE 1341)
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.
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.
Implementing Subprograms What actions must take place when subprograms are called and when they terminate? –calling a subprogram has several associated.
1 Subroutines Structure of Programming Language. 2 Procedures and Functions There are two categories of subprograms Function: subroutine that returns.
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.
ISBN Chapter 9 Subprograms Sections
Structure of Programming Languages Names, Bindings, Type Checking, and Scopes.
CSI 3125, Subprograms, page 1 Subprograms General concepts Parameter passing Functions Subprograms as parameters.
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.
Chapter 10 Implementing Subprograms. Copyright © 2012 Addison-Wesley. All rights reserved.1-2 Chapter 10 Topics The General Semantics of Calls and Returns.
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.
CSE 3302 Programming Languages
© G. Drew Kessler and William M. Pottenger1 Subroutines, Part 2 CSE 262, Spring 2003.
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.
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.
CSCI 3370: Principles of Programming Languages Chapter 9 Subprograms
Scope, Visibility, and Lifetime
Chapter 9 Subprograms.
CSC 533: Programming Languages Spring 2015
Subprograms In Text: Chapter 9.
Chapter 9 Subprograms Fundamentals of Subprograms
CSC 533: Programming Languages Spring 2018
CSC 533: Programming Languages Spring 2019
Presentation transcript:

© 2003 G. Drew Kessler and William M. Pottenger1 Subroutines (Part 1) CSE 262, Spring 2003

© 2003 G. Drew Kessler and William M. Pottenger2 Subroutines Method of process abstraction Supports modular design Improves readability Allows code reuse Conserves memory Saves coding time Characteristics: Single Entry Calling program blocks during subroutine call Control always returns to caller Except: co-routines, concurrent execution

© 2003 G. Drew Kessler and William M. Pottenger3 Design Issues & Mechanisms Method of parameter passing Function vs. procedure Mathematical function vs. program statement Local and non-local referencing environments Overloaded subroutines Generic subroutines Separate compilation, independent compilation Require info for type checking separately compiled subroutines? Aliasing and side effect problems

© 2003 G. Drew Kessler and William M. Pottenger4 Subroutine Parameters Parameters Provide access to data outside of subroutine  ‘Safer’ than using global variables Allows for specialization of computational tasks Actual parameters: values given at subprogram call site Formal parameters are given in subprogram definition

© 2003 G. Drew Kessler and William M. Pottenger5 Subprogram Definition Subprogram header: first line of definition Provides name, list of parameters (0 or more) May use special word (“procedure”, “function”, “subroutine”), or context (as in C) Parameter profile: provides #, order, and types of formal parameters Parameter protocol: profile plus subroutine return type Subprogram declaration or prototype: provides info needed by compiler to do type-checking, before definition (some languages need forward qualifier)

© 2003 G. Drew Kessler and William M. Pottenger6 Parameter Designs Association between formal and actual parameters: Positional parameters void sort(int list[], int size, bool ascending); sort(numbers, n, true); // call to sort() Keyword parameters sort(size=>n, list=>numbers, ascending=>true); //call Default parameters? void sort(int list[], int size=100, bool ascending=true); sort(numbers); // call to sort() Parameter list length fixed? (In C++, “...” is 0 or more) void sort(int list[], …); // avoid type checking of … sort(numbers, size); // call to sort() sort(…); // Will this work? What does it mean? What commonly used C function employs this ellipsis operator? How does it work? Parameters (& subroutine parameters) type-checked?

© 2003 G. Drew Kessler and William M. Pottenger7 Local and Non-Local Referencing Environments Local variables defined inside subroutine Formal parameters are usually local variables Non-local variables are either global or are variables that are in scope but local to another subroutine Local variable storage Static  Allows direct access  Provides for history sensitive subroutines Stack-dynamic  Allows recursion  Provides memory savings

© 2003 G. Drew Kessler and William M. Pottenger8 Parameter Passing Methods Semantic modes: In, out, in out Implementation methods of data transfer: value, access path (a.k.a. reference) Methods: Pass-by-value (in) Pass-by-result (out)  We’re not referring to the subroutine’s return value here! Pass-by-value-result (in out, value)  (a.k.a. pass-by-copy) Pass-by-reference (in out, reference) Pass-by-name (in out, other)

© 2003 G. Drew Kessler and William M. Pottenger9 Parameter Passing Examples CallerSubroutine Pass-by-value Pass-by-result Pass-by-value- result Pass-by-reference Actual parameterFormal parameter

© 2003 G. Drew Kessler and William M. Pottenger10 Parameter Passing in Various Languages Wide variety of implementations Fortran: always by reference Pascal: programmers choice (var) C: by value (except for arrays/explicit pointers) C++: by value, C++ ‘reference’ or as with C Java: ‘built-ins’ by value; others by reference

© 2003 G. Drew Kessler and William M. Pottenger11 Parameter Passing in Ada In, In/Out, Out: Ada parameter passing can be implemented by value or reference…  Morgan Kaufmann (Figure reproduced by permission of author/publisher)

© 2003 G. Drew Kessler and William M. Pottenger12 Call-by-Name Example (ALGOL 60) real procedure sum ( k, l, u, ak) value l, u; integer k, l, u; real ak; begin real s; s := 0; for k := l step 1 until u do s := s + ak end Use: x := sum (i, 1, n, V[i]) x := sum (i, 1, m, sum(j, 1, n, A[i, j])) (Jensen’s Device)

© 2003 G. Drew Kessler and William M. Pottenger13 Call-by-Name Problem Consider a swap routine: procedure swap (j, k) integer j, k; begin integer t; t := j; j := k; k := t; end; And, consider this: i := 1, A[1]:=2, A[2]:=8 swap(i, A[i]) Do other methods have a problem with this?

© 2003 G. Drew Kessler and William M. Pottenger14 Overloaded subprograms System and user-defined Same name, different operation Right operation identified by parameter and return types Return type distinction not available if mixed-mode expressions allowed For example: int f (int i) {…;} float f (int i) {…;} int i = j = 7; float result = f(i) + f(j); // Which f() is called?

© 2003 G. Drew Kessler and William M. Pottenger15 Generic subroutines Provide routine, where parameter and/or variable types are defined at a later time. New instance of routine with specific type created when needed or explicitly Ada generic units C++ templates template Type findMax(Type list[], int size) { Type max = list[0]; for (int i=1; i<size; i++) if (list[i]<max) max = list[i]; return max; }

© 2003 G. Drew Kessler and William M. Pottenger16 Generic Subroutines/Modules  Morgan Kaufmann (Figure reproduced by permission of author/publisher) Is it wise to return item by value?

© 2003 G. Drew Kessler and William M. Pottenger17 Inline Expansion C++ and Ada support ‘inline expansion’ C++: inline int max (int a, int b) { return a > b ? a : b; } Ada: pragma inline (max); Avoids potential problems with macros: e.g., –#define MAX(a,b) ((a) > (b) ? (a) : (b)) –Doesn’t work when ‘calling’ MAX(x++,y++) Costs more – increase in code size/compile time

© 2003 G. Drew Kessler and William M. Pottenger18 Co- Routines