1 Subroutines Structure of Programming Language. 2 Procedures and Functions There are two categories of subprograms Function: subroutine that returns.

Slides:



Advertisements
Similar presentations
PZ09B Programming Language design and Implementation -4th Edition Copyright©Prentice Hall, PZ09B - Parameter transmission Programming Language Design.
Advertisements

PZ09B Programming Language design and Implementation -4th Edition Copyright©Prentice Hall, PZ09B - Parameter transmission Programming Language Design.
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.
Subprogram Control - Data sharing Mechanisms to exchange data Arguments - data objects sent to a subprogram to be processed. Obtained through  parameters.
1 CHAPTER 9 SUBPROGRAM. 2 SUBPROGRAM Topics: b Definitions of subprogram b general subprogram characteristics b parameters b Functions and procedures.
Chapter 9 Subprograms.
1 Chapter 8 – Subroutines and Control Abstractions.
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.
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.
The environment of the computation Declarations introduce names that denote entities. At execution-time, entities are bound to values or to locations:
C SC 520 Principles of Programming Languages 1 Principles of Programming Languages Lecture 06 Parameters.
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.
© 2003 G. Drew Kessler and William M. Pottenger1 Subroutines (Part 1) CSE 262, Spring 2003.
Chapter 8 :: Subroutines and Control Abstraction
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.
1 Subroutines and Control Abstraction Aaron Bloomfield CS 415 Fall 2005.
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
Copyright © 2005 Elsevier Chapter 8 :: Subroutines and Control Abstraction Programming Language Pragmatics Michael L. Scott.
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.
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.
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.
C HAPTER 9 Subprograms. CCSB314 Programming Language C HAPTER 9 T OPICS Introduction Fundamentals of Subprograms Design Issues for Subprograms Local Referencing.
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
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.
Procedure Definitions and Semantics Procedures support control abstraction in programming languages. In most programming languages, a procedure is defined.
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.
CSCI 3370: Principles of Programming Languages Chapter 9 Subprograms
CSC 533: Programming Languages Spring 2015
Chapter 9: Subprograms Sangho Ha
Subprograms In Text: Chapter 9.
Subject : T0152 – Programming Language Concept
Parameter transmission
PZ09B - Parameter transmission
Parameter transmission
Parameter transmission
CSC 533: Programming Languages Spring 2018
Parameter transmission
CSC 533: Programming Languages Spring 2019
Parameter transmission
Presentation transcript:

1 Subroutines Structure of Programming Language

2 Procedures and Functions There are two categories of subprograms Function: subroutine that returns a value Procedure: subroutine that does not return a value

3 Basic Definitions 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 of a subprogram is the number, order, and types of its parameters

4 Basic Definitions (continued) Function declarations in C and C++ are often called prototypes A subprogram declaration provides the protocol, but not the body, of the subprogram A formal parameter is the 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

5 Semantics of Calls and Returns General semantics of subprogram calls –Pass parameters –Allocate storage of local variables and bind them –Save the execution status of calling subprogram –Transfer of control and arrange for the return General semantics of subprogram returns: –Return values of out- and inout-mode parameters to the corresponding actual parameters –Deallocate storage of local variables –Restore the execution status –Return control to the caller

6 Contents of Activation Record What data are needed for the function to run? int AddTwo(int x, y) { int sum; sum = x + y; return sum; } Parameters: x, y Local variable: sum Return address Saved registers sum y x return addr Size can be determined at compile time “state”

7 Stack Layout (cont’d) Static chain - Each stack frame contains a reference to the lexically surrounding subroutine

8 Calling Sequences Tasks on the way in (prologue) –Passing parameters, saving return address, changing program counter, changing stack pointer, saving registers, changing frame pointer Tasks on the way out (epilogue) –Passing return parameters, executing finalization code, deal locating stack frame, restoring saved registers and pc

9 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

10 Models of Parameter Passing

11 Call by Value Provides with a copy of variable’s value Actual parameter is assigned to the corresponding formal parameter when subroutine is called, and the two are independent from then on Like creating a local or temporary variable When copies are used, additional storage is required

12 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 will represent the current value of p1

13 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

14 Pass-by-Reference (Inout Mode) Pass an access path. P rovide P with the address of X 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

15 Call by name Pretty much only in Algol. By textual substitution Re-evaluates the actual parameter on every use –For actual parameters that are simple variables, it’s the same as call by reference –For actual parameters that are expressions, the expression is re-evaluated on each access No other language ever used call by name…

16 Call by name Invocation: P(A, B+2, 27+3) Definition: procedure P(X,Y,Z) {int I; I=7; X = I + (7/Y)*Z;} Meaning: P(X,Y,Z) {int I; I=7; A=I+(7/(B+2))*(27+3);}

17 Examples of Call by Name 1. P(x) {x = x + x;} Seems simple enough … Y = 2;P(Y); write(Y)  means Y = Y+Y write(Y)  prints 4 2. int A[10]; for(I=0; I<10; I++) {A[I]=I;}; I=1; P(A[I])  A[1] = A[1] + A[1]  A[1] set to 2

18 Examples of Call by Name But: F {I = I + 1; return I;} What is: P(A[F])? P(A[F])  A[F] = A[F]+A[F]  A[I++] = A[I++]+A[I++]  A[2] = A[3]+A[4]

19 Examples of Call by Name Write a program to exchange values of X and Y: (swap(X,Y)) Usual way: swap(x,y) {t=x; x=y; y=t;} Cannot do it with call by name. Cannot handle both of following: swap(I, A[I]) swap(A[I],I) One of these must fail.

20 Example of parameter passing 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) }

Example Program { local i,j local a[1..12] proc P( X, Y ) { local j j = 2 i = i + 1 print X X = X + 2 print Y j--; print Y } a[1]=1; a[2]=2; … ; a[12]=12; i = 1 j = 3 P( i, a[i*j] ) print i print a[9] }

Call by name—implemented by thunk i = 1 j = 3 P( i, a[i*j] ) X, Y j = 2 i = i + 1 print X X = X + 2 print Y j--; print Y print i print a[9] thunk: 0-ary function encapsulating argument text int& X(){return &i} int& Y(){return &a[i*j]} j = 2 // j’ == 2 j == 3 i=i+1 // i == 2 print *X() 2 X()=*X()+2 //i == 4 print *Y() // a[i*j] == a[4*3] 12 j--; print *Y()8 ! print i4 print a[9]9

Call by reference i = 1 j = 3 P( i, a[i*j] ) X, Y j = 2 i = i + 1 print X X = X + 2 print Y j--; print Y print i print a[9] lval (X) == lval (i) lval (Y) == lval (a[1*3]) j = 2 //j’ == 2 j == 3 i=i+1 //i == 2 print i2 i = i+2 //i == 4 print a[1*3] 3 j--; print a[1*3] 3 print i4 print a[9]9 aliased

Call by value (copy-in) i = 1 j = 3 P( i, a[i*j] ) X, Y j = 2 i = i + 1 print X X = X + 2 print Y j--; print Y print i print a[9] X = i //X == 1 Y = a[i*j])//Y == a[1*3] j = 2 //j’ == 2 j == 3 i=i+1 //i == 2 print X1 X = X+2 //i == 2 X == 3 print Y 3 j--; print Y3 print i2 print a[9]9

Call by value–result (Algol W) i = 1 j = 3 P( i, a[i*j] ) X, Y j = 2 i = i + 1 print X X = X + 2 print Y j--; print Y print i print a[9] X = i Y = a[i*j])//Y == a[1*3] j = 2 // j’ == 2 j == 3 i=i+1 // i == 2 print X1 X = X+2 //i == 2 X == 3 print Y 3 j--; print Y3 Use names of actuals for copy-out i = X //i == 3 a[i*j] = Y //a[3*3] == 3 print i3 print a[9]3

Modes in Programming Languages Call as constantAlgol 68, C/C++ const Call by referencePascal var, Fortran, Cobol, PL/I, Ada arrays, C arrays Call by valueAlgol 60, Algol 68 (has ref x ), Simula, C, C++ (has & x), Pascal, PL/I, APL, LISP, Snobol4, Ada (scalar in variables) Call by copyAda (scalar out or in-out ) Call by value-resultAlgol W Call by textLISP Macros Call by nameAlgol 60, Simula Call by need, R-valueHaskell, Scheme (delay x) &(force x) Call by need, L-valueimperative + lazy eval?

27 Language dependent ALGOL - name, value Fortran –Always used the inout semantics model –Before Fortran 77: pass-by-reference –Fortran 77 and later: scalar variables are often passed by value- result

28 Parameter Passing Methods C –Pass-by-value –Pass-by-reference is achieved by using pointers as parameters C++ –A special pointer type called reference type for pass-by- reference Java –All parameters are passed by value –Object parameters are passed by reference

29 Parameter Passing Methods of Ada –Three semantics modes of parameter transmission: in, out, in out; in is the default mode –Formal parameters declared out can be assigned but not referenced; those declared in can be referenced but not assigned; in out parameters can be referenced and assigned C# –Default method: pass-by-value –Pass-by-reference is specified by preceding both a formal parameter and its actual parameter with ref PHP: very similar to C# Perl: all actual parameters are implicitly placed in a predefined array

30 Language Specific Variations Pascal: Call by Value is the default, the keyword VAR denotes Call by Reference Smalltalk, Lisp: Actual Parameter is already a reference to the object

31 Type Checking Parameters 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

32 Multidimensional Arrays as Parameters If a multidimensional array is passed to a subprogram and the subprogram is separately compiled, the compiler needs to know the declared size of that array to build the storage mapping function

33 Multidimensional Arrays as Parameters: C and C++ Programmer is required to include the declared sizes of all but the first subscript in the actual parameter Solution: pass a pointer to the array and the sizes of the dimensions as other parameters; the user must include the storage mapping function in terms of the size parameters

34 Multidimensional Arrays as Parameters: Pascal and Ada Pascal –Not a problem; declared size is part of the array’s type Ada –Constrained arrays - like Pascal –Unconstrained arrays - declared size is part of the object declaration

35 Multidimensional Arrays as Parameters: Java and C# Similar to Ada Arrays are objects; they are all single- dimensioned, but the elements can be arrays Each array inherits a named constant ( length in Java, Length in C#) that is set to the length of the array when the array object is created

36 Function Returns Some languages restrict Return types –Algol 60, Fortran: scalars only –Pascal, Modula: scalars or pointers only –Most imperative languages are flexible Return statements specify a value and also cause the immediate termination of the subroutine

37 Parameters that are Subprogram Names It is sometimes convenient to pass subprogram names as parameters Are parameter types checked?

38 Parameters that are Subprogram Names: Parameter Type Checking C and C++: functions cannot be passed as parameters but pointers to functions can be passed; parameters can be type checked FORTRAN 95 type checks Later versions of Pascal and Ada does not allow subprogram parameters; a similar alternative is provided via Ada’s generic facility

39 Overloaded Subprograms Ada, Java, C++, and C# allow users to write multiple versions of subprograms with the same name –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)

40 Lifetime The lifetime of a variable is that period during program execution when the storage for the declared variable exists in some activation record (i.e., from the time the activation record for the declaring procedure is created until that activation record is destroyed). The scope of a variable is the portion of the source program where the data item can be accessed (i.e., the name of the item is a legal reference at that point in the program).

41 Lifetimes of variables Lifetime of variable X is the period when the activation record for P exists. Note that we can change the scope, but the not lifetime of a variable by nesting procedures: