Parameter passing Module 12.3 COP4020 – Programming Language Concepts Dr. Manuel E. Bermudez.

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.
Programming Languages and Paradigms
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.
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.
1 Parameter Passing (Section 8.3) CSCI 431 Programming Languages Fall 2003 A compilation of material developed by Felix Hernandez-Campos and Michael Scott.
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 :
Principles of programming languages 4: Parameter passing, Scope rules Department of Information Science and Engineering Isao Sasano.
Gary MarsdenSlide 1University of Cape Town Statements & Expressions Gary Marsden Semester 2 – 2000.
Chapter 9 Subprogram Control Consider program as a tree- –Each parent calls (transfers control to) child –Parent resumes when child completes –Copy rule.
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.
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.
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
1 Subroutines and Control Abstraction Aaron Bloomfield CS 415 Fall 2005.
CS 2104 Prog. Lang. Concepts Subprograms
COP4020 Programming Languages Subroutines and Parameter Passing Prof. Xin Yuan.
Programming Languages and Design Lecture 7 Subroutines and Control Abstraction Instructor: Li Ma Department of Computer Science Texas Southern University,
C++ function call by value The call by value method of passing arguments to a function copies the actual value of an argument into the formal parameter.
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.
Scope Rules Prepared by Manuel E. Bermúdez, Ph.D. Associate Professor University of Florida Programming Language Concepts Lecture 11.
Subprograms subroutines, procedures, functions, methods.
1 CSE1301 Computer Programming Lecture 12 Functions (Part 1)
1 Subprograms In Text: Chapter 8. 2 Chapter 8: Subprograms Outline Definitions Referencing environments Parameter passing modes and mechanisms Independent.
BEGINNING PROGRAMMING.  Literally – giving instructions to a computer so that it does what you want  Practically – using a programming language (such.
Programming Language Principles Lecture 25 Prepared by Manuel E. Bermúdez, Ph.D. Associate Professor University of Florida Subroutines (Part 2)
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.
Chapter 9: Subprograms Introduction Fundamentals of Subprograms
LECTURE 20 Parameter Passing. PARAMETER PASSING Parameterized subroutines accept arguments which control certain aspects of their behavior or act as data.
Structured Programming Dr. Atif Alhejali Lecture 4 Modifiers Parameters passing 1Structured Programming.
FUNCTIONS (METHODS) Pascal C, C++ Java Scripting Languages Passing by value, reference Void and non-void return types.
1 CSC 533: Programming Languages Spring 2014 Subprogram implementation  subprograms (procedures/functions/subroutines)  subprogram linkage  parameter.
LECTURE 20 Parameter Passing. PARAMETER PASSING Parameterized subroutines accept arguments which control certain aspects of their behavior or act as data.
Definition of the Programming Language CPRL
Functions (Methods) Pascal C, C++ Java Scripting Languages
Functions.
Programming Language Concepts
Structure of Programming Languages
Principles of programming languages 4: Parameter passing, Scope rules
Comp 205: Comparative Programming Languages
Subprograms The basic abstraction mechanism.
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.
Chapter 9 Subprograms.
CSCI 3370: Principles of Programming Languages Chapter 9 Subprograms
Passing Functions as Parameters
Chap. 8 :: Subroutines and Control Abstraction
Chap. 8 :: Subroutines and Control Abstraction
CSC 533: Programming Languages Spring 2015
Subprograms In Text: Chapter 9.
Subroutines – parameter passing
Parameter Passing in Java
CSE 3302 Programming Languages
Why did the programmer quit his job?
COP4020 Programming Languages
COP4020 Programming Languages
CSC 533: Programming Languages Spring 2018
CSC 533: Programming Languages Spring 2019
Chapter 9 Subprograms.
Presentation transcript:

Parameter passing Module 12.3 COP4020 – Programming Language Concepts Dr. Manuel E. Bermudez

Topics Parameter passing by Parameter passing in Value Reference Result Value-result Name Parameter passing in Pascal, C, C++, Java, Ada

Parameter Passing Formal parameter (a.k.a parameter): parameter that appears in the declaration of a subroutine. Actual parameter (a.k.a. argument): expression actually used in procedure call. Some languages allow only ONE parameter passing mode: C, Fortran, ML, Lisp, Java. Other languages allow multiple parameter passing modes: Pascal, Modula, Ada.

Parameter Passing Modes 1) Call by value: Pass the value of the argument. C, C++, Pascal, Java, RPAL. 2) Call by reference: Pass the address of the argument. Smalltalk, Lisp, ML, Clu, C++, Pascal. 3) Call by result: Pass the value, on return only. Ada. 4) Call by value-result: Pass the value (a.k.a. copy-in, copy-out) 5) Call by name: Pass the text of the argument. C pre-processor (#define), Algol 60.

Choosing Parameter Passing Mode Many languages provide a choice: Pass by value: Intent is to not modify the argument. Copying large objects can get expensive. Pass by reference: Intent is to modify the argument. Often used to avoid copying a large object, even if there’s no intent to modify it. In some languages (C, C++) modifying the argument can be explicitly forbidden (const)

Parameter Passing Modes: Example Pass by value: Output is (5,6,5) var y:integer; procedure A(x:integer); begin write(x); x := 1; write(y+x); end; y := 5; A(y); write(y);

Parameter Passing Modes: Example Pass by reference: Output is (5,2,1) var y:integer; procedure A(x:integer); begin write(x); x := 1; write(y+x); end; y := 5; A(y); write(y);

Parameter Passing Modes: Example Pass by result: Output is (??,6,1) var y:integer; procedure A(x:integer); begin write(x); x := 1; write(y+x); end; y := 5; A(y); write(y);

Parameter Passing Modes: Example Pass by value/result: Output is (5,6,1) var y:integer; procedure A(x:integer); begin write(x); x := 1; write(y+x); end; y := 5; A(y); write(y);

Parameter Passing Modes: Example Pass by name: Output is (5,2,1) (same as pass by reference) var y:integer; procedure A(x:integer); begin write(x); x := 1; write(y+x); end; y := 5; A(y); write(y);

Pass by name Pass by name literally substitutes n for a, and m for b. procedure swap(a,b:integer); var t:integer: begin t := a: a := b: b := t; end; ... var n,m:integer; n := 3; m := 4; swap(n,m); Pass by name literally substitutes n for a, and m for b. Result: t := n; n := m; m := t; Swap is achieved.

Pass by name However, attempting to swap i and A[i] results in t := i; procedure swap(a,b:integer); var t:integer: begin t := a: a := b: b := t; end; ... var i:integer; var A: array[1..10] of integer; i := 3; A[3] := 17; swap(i,A[i]); However, attempting to swap i and A[i] results in t := i; i := A[i]; A[i] := t; 17 assigned to i, but 3 assigned to A[17] (out of range) Swap not achieved.

Parameter Passing in pascal Use keyword var to effect pass by reference. Without var, it’s pass by value. With var , output is (5,2,1). Without var, output is (5,6,5). var y:integer; procedure A(var x:integer); begin write(x); x := 1; write(y+x); end; y := 5; A(y); write(y);

Parameter Passing in C int t; t = *a; *a = *b; *b = t; } ... All parameters passed by value. However, can use pointers (which are passed by value): void swap (int *a, int *b) { int t; t = *a; *a = *b; *b = t; } ... swap(&p,&q);

Parameter Passing in C++ C++ has pass by value and by reference. Use the & prefix for pass by reference. void swap (int &a, int &b) { int t; t = a; a = b; b = t; } swap (p,q); Parameters can be declared const.

Parameter Passing in java “primitives passed by value, non-primitives passed by reference”. NOT true. All parameters passed by value in Java. A primitive (int, float, etc.) has no reference, so the value is passed. public void swap (int a, int b) { int t; t = a; a = b; b = t; } int p=3; q=4; swap(p,q); // no swap achieved.

Parameter Passing in java Non-primitives are handled by reference, so any reference is passed by value. Not the same as pass by reference. public void tricky(Point a, Point b) { a.x = 100; a.y = 100; Point temp; temp = a; a = b; b = temp; } public static void main(String [] args) { Point x = new Point(0,0); Point y = new Point(0,0); tricky(x,y); tricky changes a, but swap is not achieved.

Parameter Passing in Ada ADA provides 3 modes: in (value), out (result), in out (value-result). Default mode is in. procedure Add (V:Integer; To:in out Integer; Limited_To:Integer) is begin -- Check that the result won’t be too large. if V + To > Limited_To then To := Limited_To; else To := V + To; end if; end Add;

summary Parameter passing by Parameter passing in Value Reference Result Value-result Name Parameter passing in Pascal, C, C++, Java, Ada