Subprograms subroutines, procedures, functions, methods.

Slides:



Advertisements
Similar presentations
Subroutines – parameter passing passing data to/from a subroutine can be done through the parameters and through the return value of a function subroutine.
Advertisements

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.
Programming Languages and Paradigms
Overview Fundamentals of Subprograms (Sec )
ISBN Chapter 9 Subprograms. Copyright © 2007 Addison-Wesley. All rights reserved.1-2 Chapter 9 Topics Introduction Fundamentals of Subprograms.
Lecture 16 Subroutine Calls and Parameter Passing Semantics Dragon: Sec. 7.5 Fischer: Sec Procedure declaration procedure p( a, b : integer, f :
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.
Variables Names Bindings Type Scope. L-Value versus R-Value Not complicated Associated with assignment statements Left hand side represents an address.
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 9 Subprograms. Copyright © 2006 Addison-Wesley. All rights reserved.1-2 Introduction Two fundamental abstraction facilities.
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.
ISBN Chapter 9 Subprograms. Copyright © 2004 Pearson Addison-Wesley. All rights reserved.9-2 Chapter 9 Topics Introduction Fundamentals.
Overview Parameter passing modes.
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.
Chapter Nine: Subprograms Lesson 09. What are they  Modularized code  Might return a value  Functions  Or not  Procedures  Subroutines  In object.
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
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.
C++ History C++ was designed at AT&T Bell Labs by Bjarne Stroustrup in the early 80's Based on the ‘C’ programming language C++ language standardised in.
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.
ISBN Chapter 9 Subprograms. Copyright © 2015 Pearson. All rights reserved.1-2 Chapter 9 Topics Introduction Fundamentals of Subprograms.
Programming Languages and Paradigms Imperative Programming.
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.
ISBN Chapter 9 Subprograms Sections
Subprograms - implementation
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.
Functions Math library functions Function definition Function invocation Argument passing Scope of an variable Programming 1 DCT 1033.
Chapter 6 Methods Chapter 6 - Methods.
ISBN Chapter 10 Implementing Subprograms.
CSE 3302 Programming Languages
Names, Scope, and Bindings Programming Languages and Paradigms.
1 CSC 533: Programming Languages Spring 2014 Subprogram implementation  subprograms (procedures/functions/subroutines)  subprogram linkage  parameter.
Chapter 10 Chapter 10 Implementing Subprograms. Implementing Subprograms  The subprogram call and return operations are together called subprogram linkage.
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.
C++ History C++ was designed at AT&T Bell Labs by Bjarne Stroustrup in the early 80's Based on the ‘C’ programming language C++ language standardised in.
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
Chapter 9 Subprograms.
CSC 533: Programming Languages Spring 2015
Subprograms In Text: Chapter 9.
Subject : T0152 – Programming Language Concept
Chapter 9 Subprograms Fundamentals of Subprograms
CSC 533: Programming Languages Spring 2018
CSC 533: Programming Languages Spring 2019
Presentation transcript:

Subprograms subroutines, procedures, functions, methods

Vocabulary float average(float list[], int n) { … } av = average(marks,20); call header / prototype protocol formal parameters actual (arguments) parameter profile

Functions vs Procedures  procedures are statements - do not return a value Collections.sort(marks, 20);  functions are expressions - return a value double x = Math.exp(10.0);  in c, c++, Java, all methods are functions; can be used as procedures; ‘void’s must be used as procedures

Parameters keyword positional call call - return return pass-by- value reference name example Java: all parameters are positional primitive parameters are call, pass by value object parameters are call-return, pass by reference

Keyword parameter passing  order of actual parameters can be changed if each parameter is identified by keyword (FORTRAN, Ada) av = average(n=>20,list=>marks)

Direction of data passing call - data goes into subprogram only at beginning of execution return - data goes from subprogram back to caller at end of execution call-return - data passes in both directions: into subroutine at call and back to caller at end of execution

Direction of data passing call - protects caller’s data from effects of subprogram return - useful for initializing data in caller; alternative route for returning data call-return - gives subprogram access to change caller data caller subprogram caller subprogram caller subprogram

Implementing parameters (1)  Pass-by-value pass-by-value (call) pass-by-result (return) pass-by-value-result (call - return)  Data is copied between actual and formal parameters a = 10proc sub(int x) sub (a) { print a print x x = 1000 print x } a x

Implementing parameters (1)  Pass-by-value pass-by-value (call) pass-by-result (return) pass-by-value-result (call - return)  What happens when actual parameters are not variables? (constants or expressions)

Implementing parameters (1)  Pass by value, result, value-result a = 10proc sub(int x) sub (a) { print a print x x = 1000 print x } a x stack sub

Implementing parameters (2)  Pass-by-reference (call - return OR call if actual parameters are made read- only)  Formal parameter refers to same memory cell as actual parameter a = 10proc sub(int x) sub (a) { print a print x x = 1000 print x } a x

Implementing parameters (2)  Pass-by-reference a = 10proc sub(int x) sub (a) { print a print x x = 1000 print x } a x stack sub address of a

Implementing parameters (2)  Pass-by-reference  Formal parameter refers to same memory cell as actual parameter  What happens when actual parameters are not variables? (constants or expressions) java example jButton.addActionListener(new ButtonListener());

Implementing parameters (3)  Pass-by-name  formal is bound to actual only when accessed or assigned (i.e., not at call)  effect depends on form of actual parameter: constant - same as pass-by-value variable - same as pass-by-reference expression - like reference but reference may change

Implementing parameters (3) - pass-by-name example procedure BIGSUB p. 364 Sebesta integer GLOBAL, LIST[1:2]; procedure SUB(PARAM) begin PARAM := 3; GLOBAL := GLOBAL + 1; // note scope! PARAM := 5; end; begin LIST[1] := 2; LIST[2] := 2; GLOBAL := 1; SUB(LIST[GLOBAL]); end;

Implementing Parameters (3)  Pass-by-name x j=4 x thunk sub(x) j=2 sub(list[j]) list[j] j list

Parameters - design issues (1)  type checking no checking - mismatched types -> reinterpretation of data type checking - control of type match or legal coercion

Parameters - design issues (2)  array parameters - for 2 or more dimensions, length of 2nd, 3rd, etc dimensions is needed to computer offset  tradeoff compile-time (efficient) offset expression forces fixed array dimensions call-time (less efficient) offset expression handles arrays of any size (java)

Parameters - design issues (3) security / efficiency tradeoff  security: design according to minimum access requirements: call, return, call-return  efficiency: reference is time and space efficient for large data objects but implies call-return  compromises allow programmer choice ‘read-only’ references (C++) what does java offer?

Passing procedures as parameters  example of procedure passing procedure m() var integer[3] arr = {1,2,3}; procedure s (integer[3] k) begin print (k[1]+k[2]+k[3]); end procedure sub (integer[3] cc, procedure w) begin w(cc); end begin sub(arr, s); end

Issues unique to procedure parameters  Does protocol of actual parameter procedure match protocol of formal? 1.- early Pascal unchecked 2.- later Pascal – parameters in formal procedure parameter 3.- c pointers to procedures are passed; their type identifies protocol

Function design  extreme definition: NO SIDE-EFFECTS no access to global variables only call parameters only effect through return value  return values: single primitive value – math model single value including reference/pointer any one value including record, array or procedure

Coroutines – preview to concurrency  subroutines in symmetric control model (not caller-callee relationship)  goal: virtual parallelism  Simula – process simulation  java – threads  classic example – large file copy read and write coroutines with shared buffer

Sharing data space  global identifiers by scoping rules  instructions to compiler to share space (Fortran COMMON)  explicit specification of external units needed (Fortran 90, java)

Reusability with subprograms  OVERLOADING: reusing a subprogram name in different scopes with different protocols (like operators * ) conflict with default parameters  POLYMORPHIC: different parameter types in different activations dynamic typing type templates C++ * C++ allows overloading of operators

C++ templates template void Swap (Etype & First, Etype & Second) { Etype Temp = First; First = Second; Second = Temp; }... in calling program int X = 5, Y = 6, Z = 7; double A = 3.5, B = 9.4; Swap(X,Y);//instantiate int version Swap(X,Z);//re-use int version Swap(A,B);//instantiate double version Swap(A,Z);// illegal – type mismatch

Reusability and compilation  problem: variables and procedures defined in one unit and accessed in another – type checking 1.separate compilation (Ada) compile time check of compatibility 2.independent compilation (c) no type checking – run time type errors 3.no compilation except complete programs (original Fortran II)

Separate Compilation  compilation checks library of exported entities for types and protocols of external references function int max(int,int) unit A A’s compilation unit int max(int,int) library i = max(j,k) unit B compile A compile B ?