Pass by Value. Pass by Value. Our programs are littered with function calls like f (x, 5).

Similar documents
CS 345. Functions. Vitaly Shmatikov. slide 1

CS558 Programming Languages

References and pointers

Design Issues. Subroutines and Control Abstraction. Subroutines and Control Abstraction. CSC 4101: Programming Languages 1. Textbook, Chapter 8

CSE 303: Concepts and Tools for Software Development

G Programming Languages - Fall 2012

CSI33 Data Structures

CS558 Programming Languages Winter 2018 Lecture 4a. Andrew Tolmach Portland State University

Array Basics: Outline. Creating and Accessing Arrays. Creating and Accessing Arrays. Arrays (Savitch, Chapter 7)

Types and Type Inference

Operator overloading

Computational Expression

COSC 2P95. Procedural Abstraction. Week 3. Brock University. Brock University (Week 3) Procedural Abstraction 1 / 26

Lecture Notes CPSC 224 (Spring 2012) Today... Java basics. S. Bowers 1 of 8

Names, Scopes, and Bindings II. Hwansoo Han

COP4020 Programming Languages. Subroutines and Parameter Passing Prof. Robert van Engelen

CSc 328, Spring 2004 Final Examination May 12, 2004

the gamedesigninitiative at cornell university Lecture 7 C++ Overview

22c:111 Programming Language Concepts. Fall Functions

CS-140 Fall 2017 Test 1 Version Practice Practie for Sept. 27, Name:

Chapter 4 Defining Classes I

known as non-void functions/methods in C/C++/Java called from within an expression.

Types, Type Inference and Unification

CSE 374 Programming Concepts & Tools. Hal Perkins Spring 2010

COMP322 - Introduction to C++ Lecture 02 - Basics of C++

Principles of Programming Languages

Types and Type Inference

COSC 2P91. Introduction Part Deux. Week 1b. Brock University. Brock University (Week 1b) Introduction Part Deux 1 / 14

PROCEDURES, METHODS AND FUNCTIONS

Welcome Back. CSCI 262 Data Structures. Hello, Let s Review. Hello, Let s Review. How to Review 1/9/ Review. Here s a simple C++ program:

Week 7. Statically-typed OO languages: C++ Closer look at subtyping

G Programming Languages Spring 2010 Lecture 4. Robert Grimm, New York University

Program development plan

6.037 Lecture 7B. Scheme Variants Normal Order Lazy Evaluation Streams

Final CSE 131B Spring 2004

for (i=1; i<=100000; i++) { x = sqrt (y); // square root function cout << x+i << endl; }

Chapter 1. Fundamentals of Higher Order Programming

Object-Oriented Programming, Iouliia Skliarova

QUIZ. What is wrong with this code that uses default arguments?

Subroutines. Subroutine. Subroutine design. Control abstraction. If a subroutine does not fit on the screen, it is too long

REVIEW. The C++ Programming Language. CS 151 Review #2

Operating Systems CMPSCI 377, Lec 2 Intro to C/C++ Prashant Shenoy University of Massachusetts Amherst

CS-140 Fall 2017 Test 1 Version Practice Practie for Sept. 27, Name:

Separate Compilation Model

Absolute C++ Walter Savitch

ENERGY 211 / CME 211. Functions

Welcome Back. CSCI 262 Data Structures. Hello, Let s Review. Hello, Let s Review. How to Review 8/19/ Review. Here s a simple C++ program:

Weeks 6&7: Procedures and Parameter Passing

a data type is Types

CSE 374 Programming Concepts & Tools. Hal Perkins Fall 2015 Lecture 19 Introduction to C++

Introduction to Programming in C Department of Computer Science and Engineering. Lecture No. #54. Organizing Code in multiple files

CS558 Programming Languages

Data Types. Every program uses data, either explicitly or implicitly to arrive at a result.

Sir Muhammad Naveed. Arslan Ahmed Shaad ( ) Muhammad Bilal ( )

Chapter 11 Introduction to Programming in C

Have examined process Creating program Have developed program Written in C Source code

Outline. 1 Function calls and parameter passing. 2 Pointers, arrays, and references. 5 Declarations, scope, and lifetimes 6 I/O

Chapter 9 :: Subroutines and Control Abstraction

CSc 520 final exam Wednesday 13 December 2000 TIME = 2 hours

C++ Important Questions with Answers

Operational Semantics. One-Slide Summary. Lecture Outline

Introduction to Programming Using Java (98-388)

Chapter 2. Procedural Programming

Program Abstractions, Language Paradigms. CS152. Chris Pollett. Aug. 27, 2008.

6. Pointers, Structs, and Arrays. March 14 & 15, 2011

for (i=1; i<=100000; i++) { x = sqrt (y); // square root function cout << x+i << endl; }

Polymorphism. Zimmer CSCI 330

Programmiersprachen (Programming Languages)

Programming Languages

AP Computer Science Chapter 10 Implementing and Using Classes Study Guide

Arrays in C++ Instructor: Andy Abreu

CS2141 Software Development using C/C++ C++ Basics

CS 360: Programming Languages Lecture 12: More Haskell

Short Notes of CS201

Chap. 8 :: Subroutines and Control Abstraction

Programming Languages

Harvard School of Engineering and Applied Sciences CS 152: Programming Languages

Chapter 11 Introduction to Programming in C

UNIT II Structuring the Data, Computations and Program. Kainjan Sanghavi

6. Names, Scopes, and Bindings

THE CATHOLIC UNIVERSITY OF EASTERN AFRICA A. M. E. C. E. A

C++ Crash Kurs. Polymorphism. Dr. Dennis Pfisterer Institut für Telematik, Universität zu Lübeck

Chapter 11 Introduction to Programming in C

CS201 - Introduction to Programming Glossary By

COMP 181. Agenda. Midterm topics. Today: type checking. Purpose of types. Type errors. Type checking

Strict Inheritance. Object-Oriented Programming Spring 2008

CS 112 Introduction to Computing II. Wayne Snyder Computer Science Department Boston University

Discussion 1H Notes (Week 3, April 14) TA: Brian Choi Section Webpage:

CS 251 Intermediate Programming Methods and Classes

Starting to Program in C++ (Basics & I/O)

CS 251 Intermediate Programming Methods and More

11/29/17. Outline. Subprograms. Subroutine. Subroutine. Parameters. Characteristics of Subroutines/ Subprograms

Topic 5 Polymorphism. " Inheritance is new code that reuses old code. Polymorphism is old code that reuses new code.

These are notes for the third lecture; if statements and loops.

Computer Science II CSci 1200 Lecture 18 Operators and Friends

COMP322 - Introduction to C++

6. Pointers, Structs, and Arrays. 1. Juli 2011

Chapter 9 Subprograms

Assumptions. History

Review of the C Programming Language

Transcription:

Our programs are littered with function calls like f (x, 5). This is a way of passing information from the call site (where the code f(x,5) appears) to the function itself. The parameter passing mode tells us how the information about the arguments (e.g. x 5) is communicated from the call site to the function. Pass by Value C/C++ use pass by value by default. Java uses it for primitive types (int, boolean, etc.). void f( int a) { a = 2* a; print (a); int main () { int x = 5; f(x); print (x); return 0; What does this C++ program print? Pass by Value In this scheme, the function recieves copies of the actual parameters. The function cannot modify the originals, only its copies, which are destroyed when the function returns. Function arguments represent one-way communication from call site to function. (How can the function communicate back?)

C++ supports reference parameters. Perl VB use this mode by default. Pass by Reference sub foo { $_ [0] = haha changed by f o o ; my $y = t h i s i s mine! ; foo ($y ); print $y, \n ; You can guess what this Perl program prints... Similar behavior happens if we wrote void f(int &a) {... in the previous C++ example. Pass by Reference The formal parameters of the function become aliases for the actual parameters! Normally the actual parameters must be variable names (or more generally, l-values... ). Function arguments now represent two-way communication. Most common reasons to use reference parameters: Variations Pass by Value/Result The initial value is passed in as a copy, the final value on return is copied back out to the actual parameter. Behaves like pass-by-reference, unless the actual parameter is accessed during the function call.. Pass by Sharing This is what happens with objects in Java. Actual formal parameters both reference some shared data (i.e., the object itself). But they are not aliases; functions can change the object that is referenced, but cannot set which object is referenced.

Pass by Value/Result This is the default in Fortran, for in out parameters in Ada: --in file f.adb procedure f( x : in out Integer ) is begin x := 10; end f; --in file test.adb procedure test is y : Integer := 5; begin f(y); Ada. Integer_Text_IO. Put (y); end test ; Calling test prints 10, not 5! Pass by Sharing This is the default in languages like Java, for non-primitive types: class Share { static class Small { public int x; public Small ( int thex ) { x = thex ; public static void test ( Small s) { s.x = 10; s = new Small (20); public static void main ( String [] args ) { Small mainsmall = new Small (5); test ( mainsmall ); System. out. println ( mainsmall.x); Does this program print 5, 10, or 20? Passing in Functional Languages Why haven t we talked about parameter passing in Haskell, Scheme, etc.?

Argument evaluation Question: When are function arguments evaluated? There are three common options: Applicative order: Arguments are evaluated just before the function body is executed. This is what we get in C, C++, Java, even SPL. Call by name: Arguments are evaluated every time they are used. (If they aren t used, they aren t evaluated!) Normal order: next slide... Lazy (A.K.A. normal order evaluation) Combines the best of both worlds: Arguments are not evaluated until they are used. Arguments are only evaluated at most once. (Related idea to memoization.) Lazy Examples Note: lazy evaluation is great for functional languages (why?). Haskell uses lazy evaluation for everything, by default. Allows wonderful things like infinite arrays! Scheme lets us do it manually with delayed evaluation, using she built-in special forms delay force.

Method calls in objects What does a call like obj.foo(x) do in an OOP language such as C++ or Java? foo must be a method defined in the class of obj. The method also has access to what object it was called on (called this in C++ Java). This is syntactic sugar for having a globally-defined method foo, with an extra argument for this. So we can think of obj.foo(x) as foo(obj,x). Function overloading: one name, many functions. Which function to call is determined by the types of the arguments. class A { void print () { cout << i n A << endl ; ; class B { void print () { cout << i n B << endl ; ; void foo ( int a) { cout << a << i s an i n t \n ; void foo ( double a) { cout << a << i s a double \n ; int main () { cout << (5 << 3) << endl ; A x; B y; x. print (); y. print (); foo (5); foo (5.0); How does overloading occur in this C++ example? Subtype polymorphism is like overloading, but the called function depends on the object s actual type, not its declared type! Each object stores a virtual methods table (vtable) containing the address of every virtual function. This is inspected at run-time when a call is made. See section 9.4 in your book if you want the details.

example in C++ class Base { virtual void aha () = 0; ; class A : public Base { void aha () { cout << I m an A! << endl ; ; class B : public Base { void aha () { cout << I m a B! << endl ; int main ( int argc ) { Base * x; if ( argc == 1 ) // can t know this at compile-time! x = new A; else x = new B; x. aha (); // Which one will it call? Different kinds of functions The code f(5) here is definitely a function call: int f( int x) { return x + 6; int main () { cout << f (5) << endl ; return 0; What else is a function call? Say we have the following C++ code: int mod ( int a, int b) { return a - (a/b)*b; What is the difference between 23 % 5 mod(23, 5) Operators

It s language dependent! Are Operators Functions? Scheme: Every operator is clearly just like any other function. Yes, they can be re-defined at will. C/C++: Operators are functions, but they have a special syntax. The call x + y is syntactic sugar for either operator+(x, y) or x.operator+(y). Java: Can t redefine operators; they only exist for some built-in types. So are they still function calls? A built-in function looks like a normal function call, but instead makes something special happen in the compiler/interpreter. Usually system calls are this way. C/C++ are an important exception! What is the difference between a built-in a library function? Recall that C/C++ has a preprocessor stage that occurs before compilation. These are the comms like #include, #ifndef, etc. #define defines a macro. It corresponds to textual substitution before compilation.

Constant Here s an example of a basic macro that you might see somewhere: The program # define PI 3.14159 double circum ( double radius ) { return 2* PI* radius ; gets directly translated by the preprocessor to double circum ( double radius ) { return 2*3.14159* radius ; before compilation! Macro Issues #1 What if we wrote the last example differently: # define PI 3.14159 # define TWOPI PI + PI double circum ( double radius ) { return TWOPI * radius ; We can also do things like this in C++: Function-like # define CIRCUM ( radius ) 2*3.14159* radius... cout << CIRCUM (1.5) + CIRCUM (2.5) << endl ;... gets translated to... cout << 2*3.14159*1.5 + 2*3.14159*2.5 << endl ;... (still prior to compilation)

Macro Issues #2 What if we made the following function to print out the larger number: # define PRINTMAX (a, b) \ if (a >= b) { cout << a << endl ; \ else { cout << b << endl ; This will work fine for PRINTMAX(5,10), but what happens with the following: int x = 5; PRINTMAX (++x, 2) Thoughts on The advantage is SPEED - pre-compilation! Notice: no types, syntactic checks, etc. lots of potential for nastiness! The literal text of the arguments is pasted into the function wherever the parameters appear. This is called... The inline keyword in C++ is a compiler suggestion that may offer a compromise. Scheme has a very sophisticated macro definition mechanism allows one to define special forms. You should know: Class Outcomes The way parameter passing works in pass by value, by reference, by value/result, by sharing Relative advantages of those parameter passing modes The way arguments are evaluated under applicative order, normal order, call by name Why lazy evaluation (normal order) can be terrific What function overloading is where it gets used What subtype polymorphism is how virtual tables are used to implement it Differences between operators, built-ins, library routines, user-defined functions What constant macros function-like macros are, what are their advantages drawbacks.