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.

Slides:



Advertisements
Similar presentations
Chapter 6 Advanced Function Features Pass by Value Pass by Reference Const parameters Overloaded functions.
Advertisements

1 Arrays Chapter 9. 2 Outline  The array structure (Section 9.1)  Array declaration  Array initialization  Array subscripts  Sequential access to.
Passing Arrays to Functions Programming. COMP102 Prog. Fundamentals I: Passing Arrays to Function / Slide 2 Passing Arrays as Parameters l Arrays are.
Programming Functions: Passing Parameters by Reference.
Arrays Programming COMP102 Prog. Fundamentals I: Arrays / Slide 2 Arrays l An array is a collection of data elements that are of the same type (e.g.,
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.
1 11/05/07CS150 Introduction to Computer Science 1 Functions Chapter 6, page 303.
Arrays An array is a sequence of objects all of which have the same type. The objects are called the elements of the array and are numbered consecutively.
Pointers and dynamic objects COMP171 Fall Pointers and dynamic objects/ Slide 2 Topics * Pointers n Memory addresses n Declaration n Dereferencing.
10/06/08MET CS Fall Arrays and Vectors 1 Multidimensional Types: 5. Arrays and Vectors Need: ability to define and manipulate variable objects.
Review of pointers and dynamic objects. Memory Management  Static Memory Allocation  Memory is allocated at compiling time  Dynamic Memory  Memory.
Passing Arrays to Functions. COMP104 Lecture 16 / Slide 2 Array Element Pass by Value * Individual array elements can be passed by value or by reference.
1 6/20/2015CS150 Introduction to Computer Science 1 Functions Chapter 6, 8.
Computer Science 1620 Multi-Dimensional Arrays. we used arrays to store a set of data of the same type e.g. store the assignment grades for a particular.
Functions:Passing Parameters by Value Programming.
Pointers. COMP104 Pointers / Slide 2 Pointers * A pointer is a variable used for storing the address of a memory cell. * We can use the pointer to reference.
Functions. COMP104 Lecture 13 / Slide 2 Review of Array: Bubble Sort for (j=0; j List[j+1]) swap(List[j], List[j+1]); }
 2003 Prentice Hall, Inc. All rights reserved. 1 Arrays –Structures of related data items –Static entity (same size throughout program) A few types –Pointer-based.
Pointers: Part I. Why pointers? - low-level, but efficient manipulation of memory - dynamic objects  Objects whose memory is allocated during program.
Multiple-Subscripted Array
Review on pointers and dynamic objects. Memory Management  Static Memory Allocation  Memory is allocated at compiling time  Dynamic Memory  Memory.
Computer Science 1620 Arrays. Problem: Given a list of 5 student grades, adjust the grades so that the average is 70%. Program design: 1. read in the.
Review of C++ Basics.
Functions. COMP104 Lecture 13 / Slide 2 Function Prototype * The function prototype declares the interface, or input and output parameters of the function,
1 Chapter 9 Pointers. 2 Topics 8.1 Getting the Address of a Variable 8.2 Pointer Variables 8.3 Relationship Between Arrays and Pointers 8.4 Pointer Arithmetic.
Chapter 7 Arrays C++ Programming, Namiq Sultan1 Namiq Sultan University of Duhok Department of Electrical and Computer Engineering Reference: Starting.
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.
Arrays.
Searching Arrays. COMP104 Array Sorting & Searching / Slide 2 Unordered Linear Search * Search an unordered array of integers for a value and save its.
Computer Science 1620 Reference Parameters. Parameters – Pass by Value recall that the parameter of a function is assigned the value of its corresponding.
Programming Functions: Passing Parameters by Reference.
COMP103 - C++ Review1 Variables and Special Chars (Ch. 2, 3)
C++ Arrays. Agenda What is an array? What is an array? Declaring C++ arrays Declaring C++ arrays Initializing one-dimensional arrays Initializing one-dimensional.
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.
1 DATA STRUCTURES: LISTS. 2 LISTS ARE USED TO WORK WITH A GROUP OF VALUES IN AN ORGANIZED MANNER. A SERIES OF MEMORY LOCATIONS CAN BE DIRECTLY REFERENCED.
Functions—Part I. Slide 2 Where are we now? Simple types of variables 3 program structures cin(>>)/cout(
CHAPTER 7 arrays I NTRODUCTION T O C OMPUTER P ROGRAMMING (CSC425)
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 Topic: Array Topic: Array. 2 Arrays Arrays In this chapter, we will : Learn about arrays Learn about arrays Explore how to declare and manipulate data.
Section 5 - Arrays. Problem solving often requires information be viewed as a “list” List may be one-dimensional or multidimensional List is implemented.
 2008 Pearson Education, Inc. All rights reserved. 1 Arrays and Vectors.
Function 2. User-Defined Functions C++ programs usually have the following form: // include statements // function prototypes // main() function // function.
1 Chapter 12 Arrays. 2 C++ Data Types structured array struct union class address pointer reference simple integral enum char short int long bool floating.
Cop3530sp12. Parameter passing call by value- appropriate for small objects that should not be altered by the function call by constant reference- appropriate.
CSCI 161 Lecture 14 Martin van Bommel. New Structure Recall “average.cpp” program –Read in a list of numbers –Count them and sum them up –Calculate the.
Module 1: Array ITEI222 - Advance Programming Language.
Review. Problem 1 What is the output of the following piece of code void increment(int x) { x++; } int main() { int y = 10; increment(y); cout
Multi-dimensional Array 1 Multi-dimensional array refers to an array with more than one index. It is a logical representation. On physical storage, the.
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.
Lecture 9 – Array (Part 2) FTMK, UTeM – Sem /2014.
Arrays as Function Parameters. CSCE 1062 Outline  Passing an array argument (section 9.3)  Reading part of an array (section 9.4)  Searching and sorting.
1 C++ Data Types structured array struct union class address pointer reference simple integral enum char short int long bool floating float double long.
Computer Skills2 / Scientific Colleges 1 Arrays Topics to cover: Arrays Data Types One-dimensional Arrays Two-dimensional Arrays.
Write code to prompt for 5 grades, read them in, print “Thank you”, then reprint the 5 grades and their average. cout >
Engineering Problem Solving with C++, Etter
Pointers and Pointer-Based Strings
C++ Arrays.
Arrays & Functions Lesson xx
Pointers and dynamic objects
Functions.
CS150 Introduction to Computer Science 1
Pointers & Functions.
Pass by Reference.
Arrays Topics to cover: Arrays Data Types One-dimensional Arrays
Pointers and Pointer-Based Strings
Arrays Arrays A few types Structures of related data items
Pointers and dynamic objects
Pointers & Functions.
Pointers and dynamic objects
Data Structure(s) A way of storing and organizing data in a computer so that it can be used efficiently. e.g. Arrays Linked Lists stacks Queues Trees.
Presentation transcript:

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 by reference. * Reference (address) of parameter is passed to the function, instead of its value. * If the function changes the parameter value, the changes will be reflected in the program calling it. * How to pass parameters by reference: &,..., &

COMP104 Pass by Reference / Slide 3 Pass by Reference: Example 1 * To show how the function affects a variable which is used as a parameter: #include using namespace std; void Increment(int& Number){ Number = Number + 1; cout << "The parameter Number: " << Number << endl; } void main(){ int I = 10; Increment(I); // parameter is a variable cout << "The variable I is: " << I << endl; }

COMP104 Pass by Reference / Slide 4 Pass by Reference: Example 2 * It is possible to use both pass by reference and pass by value parameters in the same function. // Print the sum and average of two numbers // Input: two numbers x & y // Output: sum - the sum of x & y // average - the average of x & y #include using namespace std; void SumAve (double, double, double&, double&);

COMP104 Pass by Reference / Slide 5 Pass by Reference: Example 2 void main ( ) { double x, y, sum, mean; cout << "Enter two numbers: "; cin >> x >> y; SumAve (x, y, sum, mean); cout << "The sum is " << sum << endl; cout << "The average is " << mean << endl; } void SumAve(double no1, double no2, double& sum, double& average) { sum = no1 + no2; average = sum / 2; }

COMP104 Pass by Reference / Slide 6 Pass by Reference: Example 2 * Data areas after call to SumAve:

COMP104 Pass by Reference / Slide 7 Pass by Reference: Example 3 // Compare and sort three integers #include using namespace std; void swap (int&, int&); void main ( ) { int first, second, third; // input integers // Read in first, second and third. cout << "Enter three integers: "; cin >> first >> second >> third; if (first > second) swap (first, second); if (second > third) swap (second, third); if (first > second) swap (first, second); cout << "The sorted integers are " << first << ", " << second << ", " << third << endl; }

COMP104 Pass by Reference / Slide 8 Pass by Reference: Example 3 // Function for swapping two integers void swap (int& x, int& y) { int temp; temp = x; x = y; y = temp; }

COMP104 Pass by Reference / Slide 9 Pass by Reference: Example 4 // Pass-by-reference verses pass-by-value example #include using namespace std; void One (int a, int b, int& c) { int d; a = 10; b = 11; c = 12; d = 13; cout << "The values of a, b, c, and d in One: "; cout << a << " " << b << " " << c << " " << d << endl; } void Two (int a, int b, int& d) { int c = 0; cout << "The values of a, b, c, and d in Two: "; cout << a << " " << b << " " << c << " " << d << endl; }

COMP104 Pass by Reference / Slide 10 Pass by Reference: Example 4 void main () { int a = 1, b = 2, c = 3, d = 4; One(a, b, c); cout << "The values of a, b, c, and d after One: "; cout << a << " " << b << " " << c << " " << d << endl; Two(a, b, d); cout << "The values of a, b, c, and d after Two: "; cout << a << " " << b << " " << c << " " << d << endl; }

Passing Arrays to Functions

COMP104 Pass by Reference / Slide 12 Array Element Pass by Value * Individual array elements can be passed by value or by reference * Pass-by-value example: void printcard(int c) { if(c==1) cout << "A";... } void main() { int cards[5] ;... for(int n=0; n<5; n++) printcard(card[n]); }

COMP104 Pass by Reference / Slide 13 Array Element Pass by Reference * Pass-by-reference example: void swap(int& x, int& y) { int temp; if (x > y){ temp = x; x = y; y = temp; } void main() { int A[10] = {9,8,7,6,5,4,3,2,1,0}; swap(A[3], A[5]); }

COMP104 Pass by Reference / Slide 14 Array Element Pass by Reference * Before: * After:

COMP104 Pass by Reference / Slide 15 Passing Entire Arrays to Functions * Arrays can be passed to functions in their entirety. * All that is required is the address of the first element and dimensions of the array. * The remainder of the array will be passed by reference automatically.

COMP104 Pass by Reference / Slide 16 Arrays to Functions: Example 1 //Find the largest value in an array //input: n - number of elements to check // a[ ] - array of elements // output:index to the largest element #include using namespace std; int max_element(int n, const int a[]) { int max_index = 0; for (int i=1; i<n; i++) if (a[i] > a[max_index]) max_index = i; return max_index; } void main() { int A[10] = {9,8,7,6,5,4,10,2,1,0}; cout << A[max_element(10,A)] << endl; }

COMP104 Pass by Reference / Slide 17 Arrays to Functions: Example 2 //Add a[i] and b[i] and store the sum in c[i] //Array elements with subscripts ranging from //0 to size-1 are added element by element void add_array(int size, const double a[], const double b[], double c[]){ for (int i=0; i<size; i++) c[i] = a[i] + b[i]; } In main(): add_array (5, x, y, z );

COMP104 Pass by Reference / Slide 18 Arrays to Functions: Example 2

COMP104 Pass by Reference / Slide 19 Passing Multidimensional Arrays * How to pass a multidimensional array to a function: void displayBoard(int b[][4]); // function prototype requires variable name for arrays void main(){ int board [4][4];... displayBoard(board);... } void displayBoard(int b[][4]){ // could also be:void displayBoard(int b[4][4]){ // but NOT:void displayBoard(int b[][]){... } * When passing a multidimensional array, only the size of the 1st dimension is optional, the 2nd, 3rd, etc. dimensions must be specified.