Passing Arguments Suppose you’re seated in class, which doesn’t begin for another 20 minutes… …and your friend, who missed the last two classes due to.

Slides:



Advertisements
Similar presentations
CSE202: Lecture 12The Ohio State University1 Function Calling.
Advertisements

Overloading Operators Overloading operators Unary operators Binary operators Member, non-member operators Friend functions and classes Function templates.
CS201 – Introduction to Computing – Sabancı University 1 First Midterm Exam l November 22, 2008, Saturday, 10:40 – 12:20, max 100 minutes l One A4 size.
This Time Pointers (declaration and operations) Passing Pointers to Functions Const Pointers Bubble Sort Using Pass-by-Reference Pointer Arithmetic Arrays.
CSCE 121: Introduction to Program Design and Concepts Dr. J. Michael Moore Spring 2015 Set 5: Functions 1 Based on slides created by Bjarne Stroustrup.
Q and A for Chapter 3.4 – 3.14 CS 104 Victor Norman.
Methods Liang, Chapter 4. What is a method? A method is a way of running an ‘encapsulated’ series of commands. System.out.println(“ Whazzup ”); JOptionPane.showMessageDialog(null,
Pointers Ethan Cerami Fundamentals of Computer New York University.
CS 106 Introduction to Computer Science I 02 / 28 / 2007 Instructor: Michael Eckmann.
CS 106 Introduction to Computer Science I 03 / 03 / 2008 Instructor: Michael Eckmann.
CS 106 Introduction to Computer Science I 03 / 08 / 2010 Instructor: Michael Eckmann.
ARRAYS AND POINTERS Although pointer types are not integer types, some integer arithmetic operators can be applied to pointers. The affect of this arithmetic.
Review on pointers and dynamic objects. Memory Management  Static Memory Allocation  Memory is allocated at compiling time  Dynamic Memory  Memory.
Bellevue University CIS 205: Introduction to Programming Using C++ Lecture 9: Pass-by-Value.
 2003 Prentice Hall, Inc. All rights reserved. 1 Functions Modules: functions and classes Programs use new and “prepackaged” modules –New: programmer-defined.
CS 106 Introduction to Computer Science I 10 / 15 / 2007 Instructor: Michael Eckmann.
Computer Science 1620 Reference Parameters. Parameters – Pass by Value recall that the parameter of a function is assigned the value of its corresponding.
CS 106 Introduction to Computer Science I 10 / 16 / 2006 Instructor: Michael Eckmann.
1 Functions Modules: functions and classes Programs use new and “prepackaged” modules –New: programmer-defined functions, classes –Prepackaged: from the.
Functions in C. Function Terminology Identifier scope Function declaration, definition, and use Parameters and arguments Parameter order, number, and.
Computer Organization CS345 David Monismith Based upon notes by Dr. Bill Siever and notes from the Patterson and Hennessy Text.
Variables, Functions & Parameter Passing CSci 588 Fall 2013 All material not from online sources copyright © Travis Desell, 2011.
CS212: Object Oriented Analysis and Design Lecture 10: Copy constructor.
1 CS161 Introduction to Computer Science Topic #10.
CSIS 113A Lecture 8 Parameters.  Two methods of passing arguments as parameters  Call-by-value  ‘copy’ of value is passed  Call-by-reference  ‘address.
1 Command-Line Processing In many operating systems, command-line options are allowed to input parameters to the program SomeProgram Param1 Param2 Param3.
Reference parameters (6.2.3) We might need a function to return more than one value Find roots of a quadratic 2 return values. What are they? Get first.
CMSC 202, Version 3/02 1 Copy Constructors and Overloaded Assignment.
1 Lecture 14 Functions Functions with Empty Parameter Lists Empty parameter lists  void or leave parameter list empty  Indicates function takes.
Engineering H192 - Computer Programming Gateway Engineering Education Coalition Lect 12P. 1Winter Quarter User-Written Functions Lecture 12.
References and Pointers CS 244 Connect Speakers for this Presentation Brent M. Dingle, Ph.D. Game Design and Development Program Department of Mathematics,
A Macro to Exchange the Values of Arbitrary Cells.
Method Parameters and Overloading Version 1.0. Topics The run-time stack Pass-by-value Pass-by-reference Method overloading Stub and driver methods.
Liang, Introduction to C++ Programming, (c) 2007 Pearson Education, Inc. All rights reserved X 1 Chapter Array Basics.
CS 106 Introduction to Computer Science I 03 / 02 / 2007 Instructor: Michael Eckmann.
Lecture on Set! And Local CS 2135 Copyright Kathi Fisler, 2002 This material requires Advanced Language Level.
Object-Oriented Programming in C++ Lecture 4 Constants References Operator overloading.
 2003 Prentice Hall, Inc. All rights reserved. 1 Chapter 3 - Functions Outline 3.15Functions with Empty Parameter Lists 3.16Inline Functions 3.17References.
1 Project 2: Using Variables and Expressions. 222 Project 2 Overview For this project you will work with three programs Circle Paint Ideal_Weight What.
EC-111 Algorithms & Computing Lecture #6 Instructor: Jahan Zeb Department of Computer Engineering (DCE) College of E&ME NUST.
1 2/2/05CS250 Introduction to Computer Science II Pointers.
1 FUNCTIONS - II Chapter 9 and Today we will continue with functions covering…. Passing by Reference  Scope Sorting variables Function Structure.
1 Reference Variables Chapter 8 Page Reference Variables Safer version of C/C++ pointer. "Refers to" a variable. Like a pointer. Effectively.
CIS162AD – C# Call-by-Reference Methods 06_methods_by_ref.ppt.
CMSC 202 Lesson 6 Functions II. Warmup Correctly implement a swap function such that the following code will work: int a = 7; int b = 8; Swap(a, b); cout.
The const Keyword Extreme Encapsulation. Humble Beginnings There are often cases in coding where it is helpful to use a const variable in a method or.
Reference Parameters There are two ways to pass arguments to functions: pass- by-value and pass-by-reference. pass-by-value –A copy of the arguments’svalue.
CS162 - Topic #6 Lecture: Pointers and Dynamic Memory –Review –Dynamically allocating structures –Combining the notion of classes and pointers –Destructors.
Arrays Chapter 12. One-Dimensional Arrays If you wanted to read in 1000 ints and print them in reverse order, it would take a program that’s over 3000.
FUNCTIONS (C) KHAERONI, M.SI. OBJECTIVE After this topic, students will be able to understand basic concept of user defined function in C++ to declare.
CCSA 221 Programming in C CHAPTER 3 COMPILING AND RUNNING YOUR FIRST PROGRAM 1 ALHANOUF ALAMR.
Function Parameters and Overloading Version 1.0. Topics Call-by-value Call-by-reference Call-by-address Constant parameters Function overloading Default.
User-Written Functions
CSE 220 – C Programming Pointers.
Chapter 10: Void Functions
Stack Lesson xx   This module shows you the basic elements of a type of linked list called a stack.
Object Oriented Programming COP3330 / CGS5409
Chapter 15 Pointers, Dynamic Data, and Reference Types
One-Dimensional Array Introduction Lesson xx
Arrays And Functions.
Return by Reference CSCE 121 J. Michael Moore.
The const Keyword The answer is that, yes, we don’t want the function to change the parameter, but neither do we want to use up time and memory creating.
Reference Parameters.
Parameter Passing in Java
Simulating Reference Parameters in C
Guidelines for Writing Functions
CMSC 202 Lesson 6 Functions II.
Intro to Programming Week # 8 Functions II Lecture # 13
Functions By Anand George.
Copy Constructors and Overloaded Assignment
Presentation transcript:

Passing Arguments Suppose you’re seated in class, which doesn’t begin for another 20 minutes… …and your friend, who missed the last two classes due to emergency, asks to look at your workbook. You tell him, “Sure”, but instead of lending him your workbook, you run and make copies of the pages he needs. You do this to prevent him from making any changes to your workbook.

In the programming world, this situation mirrors Passing By Value. When an argument is passed by value to a function, this means the function will receive a copy of the original object. For example, suppose I create a function that computes the square of an argument: int square( int n ) { return n*n; } Square() is only interested in the value, not the original object. So we perform pass-by-value by giving only the type and the name, int n. The function then creates its own copy of the variable.

Now suppose it’s the next day in class, and your friend needs to take another look at your workbook. Class is only a minute away from starting, so you lend him your book this time. Upon getting it back, you notice a bunch of markings left in it by him.

This situation mirrors Passing by Reference. When an argument is passed by reference to a function, the function receives the original object, and thus can modify it. For example, suppose I have a function whose job is to swap values: void swap( int& m, int& n ) { int temp = m; m = n; n = temp; } In this case, Swap() needs the original variables to do its job. So I add a ‘ & ’ symbol to indicate ‘pass-by-reference’.

Now suppose it’s the last day of class, and your friend needs one last look at your workbook. You respond ‘OK’, but also mention that you did not appreciate the markings he left in it last time. He understands, and agrees to not leave any markings on it this time.

This situation mirrors Passing by Const-Reference, where ‘const’ is short for ‘constant’. This is done when you don’t want the argument to be modified, but you also don’t want to waste memory by creating a copy of it. For example, suppose I create a function that receives a Bank data structure as an argument: void printClients( const Bank& B ) { // Code goes in here } Assume this Bank object is very large in terms of memory. Passing it by value potentially causes a Core Dump, and passing it by reference leaves open the risk of it getting modified. So we kill two birds with one stone by passing it by const-reference.