COP4020 Programming Languages Subroutines and Parameter Passing Prof. Xin Yuan.

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

1) Scope a] Ada scope rules b] C scope rules 2) Parameter passing a] Ada parameter modes b) Parameter passing mechanisms COMP205 IMPERATIVE LANGUAGES 13.
COP4020 Programming Languages Names, Scopes, and Bindings Prof. Xin Yuan.
Programming Languages and Paradigms
What is a pointer? First of all, it is a variable, just like other variables you studied So it has type, storage etc. Difference: it can only store the.
Computer Architecture CSCE 350
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.
COP4020 Programming Languages Subroutines and Parameter Passing Prof. Xin Yuan.
PZ09B Programming Language design and Implementation -4th Edition Copyright©Prentice Hall, PZ09B - Parameter transmission Programming Language Design.
Subprograms A subprogram allows process abstraction (as opposed to data abstraction). Characteristics –single entry point –caller suspended until control.
1 Parameter Passing (Section 8.3) CSCI 431 Programming Languages Fall 2003 A compilation of material developed by Felix Hernandez-Campos and Michael Scott.
CSCI 330: Programming Language Concepts Instructor: Pranava K. Jha
Procedures and Control Flow CS351 – Programming Paradigms.
Lecture 16 Subroutine Calls and Parameter Passing Semantics Dragon: Sec. 7.5 Fischer: Sec Procedure declaration procedure p( a, b : integer, f :
Principles of programming languages 4: Parameter passing, Scope rules Department of Information Science and Engineering Isao Sasano.
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.
1 Storage Registers vs. memory Access to registers is much faster than access to memory Goal: store as much data as possible in registers Limitations/considerations:
Accessing parameters from the stack and calling functions.
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.
The environment of the computation Declarations introduce names that denote entities. At execution-time, entities are bound to values or to locations:
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.
Overview Parameter passing modes.
1 The first step in understanding pointers is visualizing what they represent at the machine level. In most modern computers, main memory is divided into.
ISBN Chapter 9 Subprograms and Functions –Design Issues –Local Referencing Environments –Parameter-Passing Methods –Parameters that are Subprogram.
CSC321: Programming Languages1 Programming Languages Tucker and Noonan Chapter 9: Functions 9.1 Basic Terminology 9.2 Function Call and Return 9.3 Parameters.
Imperative Programming. Heart of program is assignment statements Aware that memory contains instructions and data values Commands: variable declarations,
© 2003 G. Drew Kessler and William M. Pottenger1 Subroutines (Part 1) CSE 262, Spring 2003.
Chapter 8 :: Subroutines and Control Abstraction
Names Variables Type Checking Strong Typing Type Compatibility 1.
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,
Runtime Environments Compiler Construction Chapter 7.
Programming Language Principles Lecture 24 Prepared by Manuel E. Bermúdez, Ph.D. Associate Professor University of Florida Subroutines.
Compiler Construction
CSc 453 Runtime Environments Saumya Debray The University of Arizona Tucson.
Chapter 9 Functions It is better to have 100 functions operate on one data structure than 10 functions on 10 data structures. A. Perlis.
Copyright © 2005 Elsevier Chapter 8 :: Subroutines and Control Abstraction Programming Language Pragmatics Michael L. Scott.
COP4020 Programming Languages Subroutines and Parameter Passing Prof. Xin Yuan.
Programming Languages and Paradigms Imperative Programming.
COP4020 Programming Languages Names, Scopes, and Bindings Prof. Xin Yuan.
Chapter 8: Arrays and Functions Department of Computer Science Foundation Year Program Umm Alqura University, Makkah Computer Programming Skills
C Functions Three major differences between C and Java functions: –Functions are stand-alone entities, not part of objects they can be defined in a file.
RUN-Time Organization Compiler phase— Before writing a code generator, we must decide how to marshal the resources of the target machine (instructions,
Copyright © 2006 The McGraw-Hill Companies, Inc. Basic Terminology Value-returning functions: –known as “non-void functions/methods” in C/C++/Java –called.
Structure of Programming Languages Names, Bindings, Type Checking, and Scopes.
Concepts of programming languages Chapter 5 Names, Bindings, and Scopes Lec. 12 Lecturer: Dr. Emad Nabil 1-1.
Lecture 19: Control Abstraction (Section )
LECTURE 20 Parameter Passing. PARAMETER PASSING Parameterized subroutines accept arguments which control certain aspects of their behavior or act as data.
Names, Scope, and Bindings Programming Languages and Paradigms.
1 CSC 533: Programming Languages Spring 2014 Subprogram implementation  subprograms (procedures/functions/subroutines)  subprogram linkage  parameter.
1 Compiler Construction Run-time Environments,. 2 Run-Time Environments (Chapter 7) Continued: Access to No-local Names.
LECTURE 19 Subroutines and Parameter Passing. ABSTRACTION Recall: Abstraction is the process by which we can hide larger or more complex code fragments.
LECTURE 20 Parameter Passing. PARAMETER PASSING Parameterized subroutines accept arguments which control certain aspects of their behavior or act as data.
Run-Time Environments Chapter 7
Functions.
Parameter passing Module 12.3 COP4020 – Programming Language Concepts Dr. Manuel E. Bermudez.
CS 3304 Comparative Languages
Chapter 9 :: Subroutines and Control Abstraction
CS 3304 Comparative Languages
Chap. 8 :: Subroutines and Control Abstraction
Chap. 8 :: Subroutines and Control Abstraction
CSC 533: Programming Languages Spring 2015
Topic 3-a Calling Convention 1/10/2019.
COP4020 Programming Languages
COP4020 Programming Languages
CSC 533: Programming Languages Spring 2018
CSC 533: Programming Languages Spring 2019
Presentation transcript:

COP4020 Programming Languages Subroutines and Parameter Passing Prof. Xin Yuan

COP4020 Spring /8/2015 Today’s topics Parameter passing related issues

COP4020 Spring /8/2015 Parameter passing Issues Call-by-name problem: Hard to write a “swap” routine that works: procedure swap(a, b) integer a, b, t; begin t := a; a := b; b := t end swap Consider swap(i, a[i]), which executes: t := i i := a[i] this changes i a[i] := t assigns t to wrong array element

COP4020 Spring /8/2015 Parameter Passing Issue (cont’d) Call by value/result behaves differently compared to call by reference in the presence of aliases (that’s why Ada forbids it) For example: procedure shift(a:out integer, b:in out integer, c:in integer) is begin a := b; b := c; end shift; When shift(x,x,0) is called by reference the resulting value of x is 0 When shift(x,x,0) is called by value/result the resulting value of x is either unchanged or 0 (because the order of copying out mode parameters is unspecified)

COP4020 Spring /8/2015 Conformant Array parameters Array parameters whose bounds are symbolic names rather than constants.  function sum(A : array[low..high : integer] of real) : real  Used in Ada, Standard Pascal, Modula-2 Needs some special treatment in compilation  E.g. Separate stack frame into fixed-size part and variable-size part. The fixed-size frame has a pointer to the conformant array

COP4020 Spring /8/2015 Default Parameters Ada, C++, Common Lisp, and Fortran 90 support default parameters A default parameter is a formal parameter with a default value When the actual parameter value is omitted in a subroutine call, the user-specified default value is used  Example in C++: void print_num(int n, int base = 10)...  A call to print_num(35) uses default value 10 for base as if print_num(35,10) was called  Example in Ada: procedure put(item : in integer; width : in field := default_width; base : in number_base := 10) is...  A call to put(35) uses default values for the width and base parameters

COP4020 Spring /8/2015 Positional Versus Named Parameters Positional parameters: the order of formal and actual arguments is fixed  All programming languages adopt this natural convention Named parameter: (also called keyword parameter) explicitly binds the actual parameter to the formal parameter  Ada, Modula-3, Common Lisp, and Fortran 90  For example in Ada: put(item => 35, base => 8); this "assigns" 35 to item and 8 to base, which is the same as: put(base => 8, item => 35); and we can mix positional and name parameters as well: put(35, base => 8);  Pro: documentation of parameter purpose  Pro: allows default parameters anywhere in formal parameter list, whereas with positional parameters the use of default parameters is restricted to the last parameter(s) only, because the compiler cannot tell which parameter is optional in a subroutine invocation

COP4020 Spring /8/2015 Variable Argument Lists C,C++, and Common Lisp are unusual in that they allow defining subroutines that take a variable number of arguments  Example in C: #include int plus(int num,...) { int sum; va_list args; // declare list of arguments va_start(args, num); // initialize list of arguments for (int i=0; i<num; i++) sum += va_arg(args, int); // get next argument (assumed to be int) va_end(args); // clean up list of arguments return sum; } Function plus adds a set of integers, where the number of integers is the first parameter to the function: plus(4,3,2,1,4) is 10  Used in the printf and scanf text formatting functions in C Variable number of arguments in C and C++ is not type safe as parameter types are not checked In Common Lisp, one can write ( ) to add the integers

COP4020 Spring /8/2015 Function Returns Some programming languages allow a function to return any type of data structure, except maybe a subroutine (requires first-class subroutines)  Modula-3 and Ada allow a function to return a subroutine as a closure  C and C++ allow functions to return pointers to functions (no closures) Some languages use special variable to hold function return value  Example in Pascal: function max(a : integer; b : integer) : integer; begin if a>b then max := a else max := b end  There is no return statement, instead the function returns with the value of max when it reaches the end  The subroutine frame reserves a slot for the return value anyway, so these languages make the slot explicitly available

COP4020 Spring /8/2015 Function Returns (cont’d) Ada, C, C++, Java, and other more modern languages typically use an explicit return statement to return a value from a function  Example in C: int max(int a, int b) { if (a>b) return a; else return b; } Programmers may need a temporary variable for incremental operations on the return value  For example: int fac(int n) { int ret = 1; for (i = 2; i <= n; i++) ret *= i; return ret; }  Wastes time copying the temporary variable value into return slot of subroutine frame on the stack

Gcc calling sequence for the X86 architecture cdecl (C declaration) calling sequence  Subroutine arguments are passed on the stack, in the reverse order.  Stack are aligned to a 16 byte boundary  Calling function cleans the stack after the function call returns This allows for variable number of parameters  Integer values and memory address are returned in EAX  EAX, ECX, EDX are caller-saved, the rest are callee saved. See callingsequence.cpp and callingsequence.s COP4020 Spring /8/2015