CSE 3302 Programming Languages

Slides:



Advertisements
Similar presentations
Semantic Analysis Chapter 6. Two Flavors  Static (done during compile time) –C –Ada  Dynamic (done during run time) –LISP –Smalltalk  Optimization.
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.
PZ09B Programming Language design and Implementation -4th Edition Copyright©Prentice Hall, PZ09B - Parameter transmission Programming Language Design.
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.
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.
CPSC 388 – Compiler Design and Construction Parameter Passing.
Advanced Programming Parameter Passing Giuseppe Attardi Università di Pisa.
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 :
Chapter EighteenModern Programming Languages1 Parameters.
Runtime Environments Source language issues Storage organization
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.
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.
Pointers Example Use int main() { int *x; int y; int z; y = 10; x = &y; y = 11; *x = 12; z = 15; x = &z; *x = 5; z = 8; printf(“%d %d %d\n”, *x, y, z);
CSC321: Programming Languages1 Programming Languages Tucker and Noonan Chapter 9: Functions 9.1 Basic Terminology 9.2 Function Call and Return 9.3 Parameters.
Copyright © 2006 The McGraw-Hill Companies, Inc. Programming Languages 2nd edition Tucker and Noonan Chapter 9 Functions It is better to have 100 functions.
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
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.
Semantics CSE 340 – Principles of Programming Languages Fall 2015 Adam Doupé Arizona State University
1 CSE1301 Computer Programming Lecture 12 Functions (Part 1)
1 Languages and Compilers (SProg og Oversættere) Bent Thomsen Department of Computer Science Aalborg University With acknowledgement to Angela Guercio.
RUN-Time Organization Compiler phase— Before writing a code generator, we must decide how to marshal the resources of the target machine (instructions,
CS 261 – Data Structures Introduction to C Programming.
Semantics CSE 340 – Principles of Programming Languages Fall 2015 Adam Doupé Arizona State University
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.
CS 536 Parameter Passing 1. Roadmap Last Time – Storing variables Locals, non-locals, globals This Time – Propagating values from one function to another.
Chapter EighteenModern Programming Languages1 Parameters.
Chapter 9: Subprograms Introduction Fundamentals of Subprograms
CSE 3302 Programming Languages
C arrays (Reek, Ch. 8) CS 3090: Safety Critical Programming in C.
Data Types In Text: Chapter 6.
Functions.
Computer Science 210 Computer Organization
CSE 3302 Programming Languages
Expressions and Assignment
Parameter passing Module 12.3 COP4020 – Programming Language Concepts Dr. Manuel E. Bermudez.
Principles of programming languages 4: Parameter passing, Scope rules
CSE 3302 Programming Languages
Pointers.
CSE 3302 Programming Languages
Computer Science 210 Computer Organization
Semantics CSE 340 – Principles of Programming Languages Spring 2016
Chapter 9 :: Subroutines and Control Abstraction
Parameter Passing (Ch 18)
Chap. 8 :: Subroutines and Control Abstraction
Chap. 8 :: Subroutines and Control Abstraction
CSE 3302 Programming Languages
CSC 533: Programming Languages Spring 2015
Subroutines – parameter passing
Topic 3-a Calling Convention 1/10/2019.
CSE 3302 Programming Languages
COP4020 Programming Languages
CSE 3302 Programming Languages
Parameter transmission
PZ09B - Parameter transmission
Parameter transmission
CSC 533: Programming Languages Spring 2018
CSC 533: Programming Languages Spring 2019
Subprograms General concepts Parameter passing Functions
Parameter transmission
CSE 3302 Programming Languages
CSE 3302 Programming Languages
Presentation transcript:

CSE 3302 Programming Languages Control II Procedures and Environments (cont.) Chengkai Li Fall 2007 Lecture 11 – Control II, Fall 2007 CSE3302 Programming Languages, UT-Arlington ©Chengkai Li, 2007

Activation Record for Procedures int x; //global void B(void) { int i; i = x/2; } void A(void) { int x,y; x = y*10; B(); } main() { A(); return 0; } i: local variable in called environment x: global variable in defining environment x x y x,y: local variable in calling environment i Lecture 10 – Control I, Fall 2007 CSE3302 Programming Languages, UT-Arlington ©Chengkai Li, 2007

Activation Record for Procedures int x; //global void B(void) { int i; i = x/2; } void A(void) { int x,y; x = y*10; B(); } main() { A(); return 0; } Can only access global variables in defining environment No direct access to the local variables in the calling environment (Need to communicate through parameters) x x y i Lecture 10 – Control I, Fall 2007 CSE3302 Programming Languages, UT-Arlington ©Chengkai Li, 2007

CSE3302 Programming Languages, UT-Arlington ©Chengkai Li, 2007 Procedure Call Caller: Callee: … int f(int a){ f(i); ...; … ...a...; ...; } actual parameter / argument formal parameter / argument Parameter Passing Mechanisms: When and how to evaluate parameters How actual parameter values are passed to formal parameters How formal parameter values are passed back to actual parameters Lecture 10 – Control I, Fall 2007 CSE3302 Programming Languages, UT-Arlington ©Chengkai Li, 2007

Parameter Passing Mechanisms Pass/Call by Value Pass/Call by Reference Pass/Call by Value-Result Pass/Call by Name Lecture 11 – Control II, Fall 2007 CSE3302 Programming Languages, UT-Arlington ©Chengkai Li, 2007

CSE3302 Programming Languages, UT-Arlington ©Chengkai Li, 2007 Example What is the result? void swap(int a, int b) { int temp; temp = a; a = b; b = temp; } main(){ int i=1, j=2; swap(i,j); printf(“i=%d, j=%d\n”, i, j); } It depends… Lecture 11 – Control II, Fall 2007 CSE3302 Programming Languages, UT-Arlington ©Chengkai Li, 2007

CSE3302 Programming Languages, UT-Arlington ©Chengkai Li, 2007 Pass by Value Caller: Callee: … int f(int a){ f(i); ...a...; … } i a Most common one Replace formal parameters by the values of actual parameters Actual parameters: No change Formal parameters: Constant (Ada, “in”) Local variables (C, C++, Java, Pascal) Lecture 10 – Control I, Fall 2007 CSE3302 Programming Languages, UT-Arlington ©Chengkai Li, 2007

CSE3302 Programming Languages, UT-Arlington ©Chengkai Li, 2007 Example: Pass By Value void swap(int a, int b) { int temp; temp = a; a = b; b = temp; } main(){ int i=1, j=2; swap(i,j); printf(“i=%d, j=%d\n”, i, j); i 1 j 2 a b i 1 j 2 a b i 1 j 2 a b Lecture 11 – Control II, Fall 2007 CSE3302 Programming Languages, UT-Arlington ©Chengkai Li, 2007

Are these Pass-by-Value? C: void f(int *p) { *p = 0; } void f(int a[]) { a[0]=0; } Java: void f(Vector v) { v.removeAll(); } Yes! Lecture 11 – Control II, Fall 2007 CSE3302 Programming Languages, UT-Arlington ©Chengkai Li, 2007

Pass-by-Value: Pointers C: void f(int *p) { *p = 0; } main() { int *q; q = (int *) malloc(sizeof(int)); *q = 1; f(q); printf("%d\n", q[0]); } q ? q 1 p q ? 1 p q 1 p q Lecture 11 – Control II, Fall 2007 CSE3302 Programming Languages, UT-Arlington ©Chengkai Li, 2007

Pass-by-Value: Pointers C: void f(int *p) { p = (int *) malloc(sizeof(int)); *p = 0; } main() { int *q; q = (int *) malloc(sizeof(int)); *q = 1; f(q); printf("%d\n", q[0]); } What happens here? Lecture 11 – Control II, Fall 2007 CSE3302 Programming Languages, UT-Arlington ©Chengkai Li, 2007

Pass-by-Value: Arrays C: void f(int p[]) { p[0] = 0;} main() { int q[10]; q[0]=1; f(q); printf("%d\n", q[0]); } What happens here? Lecture 11 – Control II, Fall 2007 CSE3302 Programming Languages, UT-Arlington ©Chengkai Li, 2007

Pass-by-Value: Arrays C: void f(int p[]) { p=(int *) malloc(sizeof(int)); p[0] = 0; } main() { int q[10]; q[0]=1; f(q); printf("%d\n", q[0]); } What happens here? Lecture 11 – Control II, Fall 2007 CSE3302 Programming Languages, UT-Arlington ©Chengkai Li, 2007

Pass-by-Value: Java Objects void f(Vector v) { v.removeAll(); } main() { Vector vec; vec.addElement(new Integer(1)); f(vec); System.out.println(vec.size()); } What happens here? Lecture 11 – Control II, Fall 2007 CSE3302 Programming Languages, UT-Arlington ©Chengkai Li, 2007

Pass-by-Value: Java Objects void f(Vector v) { v = new Vector(); v.removeAll(); } main() { Vector vec; vec.addElement(new Integer(1)); f(vec); System.out.println(vec.size()); } What happens here? Lecture 11 – Control II, Fall 2007 CSE3302 Programming Languages, UT-Arlington ©Chengkai Li, 2007

CSE3302 Programming Languages, UT-Arlington ©Chengkai Li, 2007 Pass by Reference Caller: Callee: … int f(int a){ f(i); ...a...; … } i a Formal parameters become alias of actual parameters Actual parameters: changed by changes to formal parameters Examples: Fortran: the only parameter passing mechanism C++ (reference type, &) /Pascal (var) Lecture 10 – Control I, Fall 2007 CSE3302 Programming Languages, UT-Arlington ©Chengkai Li, 2007

Example: Pass By Reference C++ syntax. Not valid in C void swap(int &a, int &b) { int temp; temp = a; a = b; b = temp; } main(){ int i=1, j=2; swap(i,j); printf(“i=%d, j=%d\n”, i, j); i 1 j 2 a b i 1 j 2 a b i 2 j 1 a b Lecture 11 – Control II, Fall 2007 CSE3302 Programming Languages, UT-Arlington ©Chengkai Li, 2007

Pass-by-Reference: How to minic it in C? void f(int *p) { *p = 0; } main() { int q; q = 1; f(&q); printf(“%d\n”, q); } It is really pass-by-value. Why? Lecture 11 – Control II, Fall 2007 CSE3302 Programming Languages, UT-Arlington ©Chengkai Li, 2007

It is really pass-by-value C: void f(int *p) { p = (int *) malloc(sizeof(int)); *p = 0; } main() { int q; q = 1; f(&q); printf(“%d\n”, q); } Lecture 11 – Control II, Fall 2007 CSE3302 Programming Languages, UT-Arlington ©Chengkai Li, 2007

Pass-by-Reference: C++ Constant Reference void f(const int & p) { int a = p; p = 0; } main(){ int q; q = 1; f(q); printf("%d\n", q); What happens here? Lecture 11 – Control II, Fall 2007 CSE3302 Programming Languages, UT-Arlington ©Chengkai Li, 2007

Pass-by-Reference: C++ Reference-to-Pointer void f(int * &p) { *p = 0; } main(){ int *q; int a[10]; a[0]=1; q=a; f(q); printf("%d, %d\n", q[0], a[0]); } What happens here? Lecture 11 – Control II, Fall 2007 CSE3302 Programming Languages, UT-Arlington ©Chengkai Li, 2007

Pass-by-Reference: C++ Reference-to-Pointer void f(int * &p) { p = new int; *p = 0; } main(){ int *q; int a[10]; a[0]=1; q=a; f(q); printf("%d, %d\n", q[0], a[0]); } What happens here? Lecture 11 – Control II, Fall 2007 CSE3302 Programming Languages, UT-Arlington ©Chengkai Li, 2007

Pass-by-Reference: C++ Reference-to-Array void f(int (&p)[10]) { p[0]=0; } main(){ int *q; int a[10]; a[0]=1; q = a; f(a); printf("%d, %d\n", q[0], a[0]); What happens here? Lecture 11 – Control II, Fall 2007 CSE3302 Programming Languages, UT-Arlington ©Chengkai Li, 2007

CSE3302 Programming Languages, UT-Arlington ©Chengkai Li, 2007 Pass by Value-Result Caller: Callee: … int f(int a){ f(i); ...a...; … } i a i a Combination of Pass-by-Value and Pass-by-Reference (Pass-by-Reference without aliasing) Replace formal parameters by the values of actual parameters Value of formal parameters are copied back to actual parameters Algol W, Ada (in out) Lecture 10 – Control I, Fall 2007 CSE3302 Programming Languages, UT-Arlington ©Chengkai Li, 2007

Example: Pass By Value-Result void swap(int a, int b) { int temp; temp = a; a = b; b = temp; } main(){ int i=1, j=2; swap(i,j); printf(“i=%d, j=%d\n”, i, j); i 1 j 2 a b i 1 j 2 a b i 2 j 1 a b Lecture 11 – Control II, Fall 2007 CSE3302 Programming Languages, UT-Arlington ©Chengkai Li, 2007

CSE3302 Programming Languages, UT-Arlington ©Chengkai Li, 2007 Unspecified Issues void f(int a, int b) { a = 1; b = 2; } main(){ int i=0; f(i,i); printf(“i=%d\n”, i); i a b i a b 1 ? 2 Lecture 11 – Control II, Fall 2007 CSE3302 Programming Languages, UT-Arlington ©Chengkai Li, 2007

CSE3302 Programming Languages, UT-Arlington ©Chengkai Li, 2007 Pass by Name Caller: Callee: … int f(int a){ f(i); ...a...; … } Actual parameters only evaluated when they are needed The same parameter can be evaluated multiple times Evaluated in calling environment Essentially equivalent to normal order evaluation Example: Algol 60 Not adopted by any major languages due to implementation difficulty Lecture 10 – Control I, Fall 2007 CSE3302 Programming Languages, UT-Arlington ©Chengkai Li, 2007

CSE3302 Programming Languages, UT-Arlington ©Chengkai Li, 2007 Example: Pass By Name void swap(int a, int b) { int temp; temp = a; a = b; b = temp; } main(){ int i=1, j=2; swap(i,j); printf(“i=%d, j=%d\n”, i, j); a (i) 1 j 2 temp a (i) 1 b (j) 2 temp a (i) 2 b (j) temp 1 Lecture 11 – Control II, Fall 2007 CSE3302 Programming Languages, UT-Arlington ©Chengkai Li, 2007

Pass-by-Name: Side Effects int p[3]={1,2,3}; int i; void swap(int a, int b) { int temp; temp = a; a = b; b = temp; } main(){ i = 1; swap(i, a[i]); printf(“%d, %d\n”, i, a[i]); What happens here? Lecture 11 – Control II, Fall 2007 CSE3302 Programming Languages, UT-Arlington ©Chengkai Li, 2007

CSE3302 Programming Languages, UT-Arlington ©Chengkai Li, 2007 Some Variants Pass by Name Evaluated at every use, in the calling environment Pass by Need Evaluated once, memorized for future use Pass by Text (Macro) Evaluated using the called environment. All belong to Non-strict evaluation (lazy evaluation) Lecture 11 – Control II, Fall 2007 CSE3302 Programming Languages, UT-Arlington ©Chengkai Li, 2007

CSE3302 Programming Languages, UT-Arlington ©Chengkai Li, 2007 Comparions Call by Value Efficient. No additional level of indirection. Less flexible and less efficient without pointer. (array, struct, union as parameters) Call by Reference Require one additional level of indirection (explicit dereferencing) If a parameter is not variable (e.g., constant), a memory space must be allocated for it, in order to get a reference. Easiest to implement. Call by Value-Result You may not want to change actual parameter values when facing exceptions. Call by Name Lazy evaluation Difficult to implement Lecture 11 – Control II, Fall 2007 CSE3302 Programming Languages, UT-Arlington ©Chengkai Li, 2007