Cop3530sp12. Parameter passing call by value- appropriate for small objects that should not be altered by the function call by constant reference- appropriate.

Slides:



Advertisements
Similar presentations
C++ crash course Class 10 practice problems. Pointers The following function is trying to swap the contents of two variables. Why isnt it working? void.
Advertisements

Pass by Value. COMP104 Pass by Value / Slide 2 Passing Parameters by Value * A function returns a single result (assuming the function is not a void function)
Chapter 6 Advanced Function Features Pass by Value Pass by Reference Const parameters Overloaded functions.
Week 8 Arrays Part 2 String & Pointer
Pointers Typedef Pointer Arithmetic Pointers and Arrays.
Lecture 3 Feb 4 summary of last week’s topics and review questions (handout) Today’s goals: Chapter 1 overview (sections 1.4 to 1.6) c++ classes constructors,
1 Pointers A pointer variable holds an address We may add or subtract an integer to get a different address. Adding an integer k to a pointer p with base.
More on Functions Programming. COMP104 Lecture 19 / Slide 2 Passing Parameters by Reference l To have a function with multiple outputs, we have to use.
Pointers and dynamic objects COMP171 Fall Pointers and dynamic objects/ Slide 2 Topics * Pointers n Memory addresses n Declaration n Dereferencing.
Rossella Lau Lecture 8, DCO10105, Semester B, DCO10105 Object-Oriented Programming and Design  Lecture 8: Polymorphism & C++ pointer  Inheritance.
Review of pointers and dynamic objects. Memory Management  Static Memory Allocation  Memory is allocated at compiling time  Dynamic Memory  Memory.
Introduction to C Programming CE
Pointers. Topics Pointers Pointer Arithmetic Pointers and Arrays.
Computer Science 1620 Function Scope & Global Variables.
Pointers: Part I. Why pointers? - low-level, but efficient manipulation of memory - dynamic objects  Objects whose memory is allocated during program.
Review on pointers and dynamic objects. Memory Management  Static Memory Allocation  Memory is allocated at compiling time  Dynamic Memory  Memory.
1 Pointers, Dynamic Data, and Reference Types Review on Pointers Reference Variables Dynamic Memory Allocation –The new operator –The delete operator –Dynamic.
1 Arrays & functions Each element of an array acts just like an ordinary variable: Like any ordinary variable, you can pass a single array element to a.
Programming Pointers. COMP104 Lecture 32 / Slide 2 Pointers l Pointers are objects whose values are the locations of other objects l Pointers are memory.
Computer Science 1620 Reference Parameters. Parameters – Pass by Value recall that the parameter of a function is assigned the value of its corresponding.
Pass by Reference. COMP104 Pass by Reference / Slide 2 Passing Parameters by Reference * To have a function with multiple outputs, we have to use pass.
1 Procedural Concept The main program coordinates calls to procedures and hands over appropriate data as parameters.
Lecture 2 Arrays, Pointers, and Structures. Objective In this chapter, we will discuss several concepts: Arrays (first-class arrays, using vector) Strings.
Variables, Functions & Parameter Passing CSci 588 Fall 2013 All material not from online sources copyright © Travis Desell, 2011.
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.
CSE 332: C++ Type Programming: Associated Types, Typedefs and Traits A General Look at Type Programming in C++ Associated types (the idea) –Let you associate.
COMPUTER PROGRAMMING. Functions What is a function? A function is a group of statements that is executed when it is called from some point of the program.
A Computer Science Tapestry 12.1 Tools for programming l Syntatic details of control and variables  If, while, switch, function calls  Scope and lifetime.
Chapter 1 Arrays, Pointers, and Structures Saurav Karmakar Spring 2007.
Pointers, Variables, and Memory. Variables and Pointers When you declare a variable, memory is allocated to store a value. A pointer can be used to hold.
Pointers and Dynamic Memory Allocation Copyright Kip Irvine 2003, all rights reserved. Revised 10/28/2003.
Object-Oriented Programming in C++
Current Assignments Start Reading Chapter 6 Project 3 – Due Thursday, July 24 Contact List Program Homework 6 – Due Sunday, July 20 First part easy true/false.
C ++ Basics by Bindra Shrestha sce.uhcl.edu/shresthab CSCI 3333 Data Structures.
Tracing through E01, question 9 – step 1 // p02.cc P. Conrad, for CISC181 07S // Exam question for E01 #include using namespace std; void mysteryFunction(int.
Chapter 7 Pointers: Java does not have pointers. Used for dynamic memory allocation.
Lecture 3 Classes, Structs, Enums Passing by reference and value Arrays.
Week 2. Functions: int max3(int num1, int num2, int num3) {int result; result = max(max(num1,num2),num3); return result; } //max3.
1 Chapter 1 C++ Basics Review Reading: Sections 1.4 and 1.5.
1 Cannon_Chapter9 Strings and the string Class. 2 Overview  Standards for Strings  String Declarations and Assignment  I/O with string Variables 
Functions Illustration of: Pass by value, reference Scope Allocation Reference: See your CS115/215 textbook.
The This Pointer Programming in C++ Fall 2008 Dr. David A. Gaitros
Object-Oriented Programming in C++ Lecture 4 Constants References Operator overloading.
Variables and memory addresses
CSE 332: C++ template examples Today: Using Class and Function Templates Two examples –Function template for printing different types –Class template for.
1 Reference Variables Chapter 8 Page Reference Variables Safer version of C/C++ pointer. "Refers to" a variable. Like a pointer. Effectively.
Dr. Yang, QingXiong (with slides borrowed from Dr. Yuen, Joe) LT9: Pointer I CS2311 Computer Programming.
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.
Chapter 1 C++ Basics Review (Section 1.4). Classes Defines the organization of a data user-defined type. Members can be  Data  Functions/Methods Information.
Java & C++ Comparisons How important are classes and objects?? What mechanisms exist for input and output?? Are references and pointers the same thing??
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.
1 Parameter passing Call by value The caller evaluates the actual parameters and passes copies of their values to the called function. Changes to the copies.
Nirmalya Roy School of Electrical Engineering and Computer Science Washington State University Cpt S 223 – Advanced Data Structures C++ Review 2.
1 Chapter 1 C++ Templates Readings: Sections 1.6 and 1.7.
Lecture 9 – Array (Part 2) FTMK, UTeM – Sem /2014.
Functions CMSC 202. Topics Covered Function review More on variable scope Call-by-reference parameters Call-by-value parameters Function overloading Default.
Prepared by Andrew Jung. Accessing Pointer Data Pointer can be used to access the contents of an array Look at the following syntax: /* Declaration and.
Chapter 1 C++ Basics Review
A Lecture for the c++ Course
Pointers and Pointer-Based Strings
Pointers, Dynamic Data, and Reference Types
Pointers & Functions.
Chapter 4 Implementing Free Functions
Simulating Reference Parameters in C
Pointers and Pointer-Based Strings
CMSC 341 C++ and OOP.
Pointers and dynamic objects
Pointers & Functions.
CMSC 202 Lesson 6 Functions II.
CMSC 341 C++ and OOP.
Presentation transcript:

cop3530sp12

Parameter passing call by value- appropriate for small objects that should not be altered by the function call by constant reference- appropriate for large objects that should not be altered by the function call by reference - appropriate for all objects that should be altered by the function using pointers -- see the swap method explained in class We discussed this example in class. We don’t want the contents of of the vector arr to change so we pass it by constant reference. The value of errorFlag is passed by reference because we want that the change in its value should be refelected in the calling function. double avg (const vector & arr, int n, bool & errorFlag) (// source: Mark Allen Weiss' DSA book) Also see the three swap functions described in class.

Return Passing Return by Value Return by constant reference return by reference (occasionally) using pointers (see the 2d dynamic array allocation example).

// note that in the correct version we are returning a string by constant reference which was not declared inside the function, so when the function returns the tsring doesn’t go out of scope. In the incorrect version, terrible things may happen as the string maxValue is declared inside the function and so returning by reference or constant reference is not correct. Return by value would have been correct. #include using namespace std; const string & findMax( const vector & arr ) { int maxIndex = 0; for( int i = 1; i < arr.size( ); i++ ) if( arr[ maxIndex ] < arr[ i ] ) maxIndex = i; return arr[ maxIndex ]; } const string & findMaxWrong( const vector & arr ) { string maxValue = arr[ 0 ]; for( int i = 1; i < arr.size( ); i++ ) if( maxValue < arr[ i ] ) maxValue = arr[ i ]; return maxValue; } // continued on next slide

//... continued from previous slide int main( ) { vector arr; arr.push_back( "hello" ); arr.push_back( "there" ); arr.push_back( "world" ); cout << findMax( arr ) << endl; cout << findMaxWrong( arr ) << endl; return 0; }

// using pass by value – doesn’t work void swap (int a, int b) { int temp = a ; a = b ; b = temp ; } int main () { int p = 6 ; int q = 7 ; swap(p, q) ; cout << p ; //outputs 6 cout << q ; //outputs 7 ; //This is called call by value and no change happens. // this doesn't work. The change that happens inside the function is local to tha function and is not reflected in the calling function main.

// pass by reference -- works void swap (int & a, int & b) { int temp = a ; a = b ; b = temp ; } int main() { int p = 6 ; int q = 7 ; swap (p, q) ; cout << p ; // prints 7 cout << q ; // prints 6 //call by reference // this works

void swap (int *a, int *b) { temp = *a ; *a = *b ; *b = temp ; } int main() { int p = 6 ; int q = 7 ; swap(&p, &q) ; cout << p ; //prints 7 cout << q ; // prints 6 // Using pointers. This is actually a Cstyle swap code. IN C always passby value happens and so the address of p and q gets copied into a and b. So, even though it is pass by value we are still able to accomplish the swapping using pointers.