unit 11b
play

Unit 11b Functions Pass-by-Reference & Array Arguments 2 - PowerPoint PPT Presentation

1 Unit 11b Functions Pass-by-Reference & Array Arguments 2 Passing Arrays As Arguments // Function that takes an array Can we pass an array to another int sum( int data[] , int size); 1 int sum( int data[] , int size) function? {


  1. 1 Unit 11b Functions Pass-by-Reference & Array Arguments

  2. 2 Passing Arrays As Arguments // Function that takes an array • Can we pass an array to another int sum( int data[] , int size); 1 int sum( int data[] , int size) function? { int total = 0; – YES!! for(int i=0; i < size; i++){ total += data[i]; } • Syntax: return total; } – Step 1 : In the prototype/signature: int main() { Put empty square brackets after int vals[100]; /* some code to initialize vals */ the parameter name if it is an 2 int mysum = sum( vals , 100); cout << mysum << endl; array (e.g. int data[] ) // prints sum of all numbers return 0; – Step 2 : When you call the } function, just provide the name of the array

  3. 3 Pass-by-Value & Pass-by-Reference • What are the pros and cons of emailing a document by: – Attaching it to the email – Sending a link (URL) to the document on some cloud service (etc. Google Docs) • Pass-by-value is like emailing an attachment – A copy is made and sent • Pass-by-reference means emailing a link to the original – No copy is made and any modifications by the other party are seen by the originator

  4. 4 Arrays And Pass-by-Reference void dec(int); • Single (scalar) variables are int main() { int y = 3; passed-by-value in C/C++ dec( y ); cout << y << endl; return 0; – Copies are passed } void dec( int y ) { y --; } • Arrays are passed-by- Single variables (aka scalars) are passed-by-value but arrays are reference passed-by-reference void init(int x[], int size); int main() – Links are passed { int data[10]; init( data, 10 ); – This means any change to the cout << data[9] << endl; // prints 0 array by the function is visible return 0; } void init( int x[], int size ) upon return to the caller { // x is really a link to data for(int i=0; i < size; i++){ x[i] = 0; // changing data[i] } }

  5. 5 But Why? // Function that takes an array • If we used pass-by-value then we'd have to int sum( int data[] , int size); make a copy of a potentially HUGE amount int sum( int data[] , int size) { of data (what if the array had a million int total = 0; elements) for(int i=0; i < size; i++){ total += data[i]; • To avoid copying vast amounts of data, we } return total; pass a link } int main() { int vals[100]; main() /* some code to initialize vals */ int mysum = sum( vals , 100); cout << mysum << endl; 520 916 // prints sum of all numbers [0] [99] 520 916 return 0; … ? ? [0] [99] } … ? ? vals data sum() return val

  6. 6 So What Is Actually Passed? // Function that takes an array • The "link" that is passed is just the int sum( int data[] , int size); starting address (e.g. 520) of the int sum( int data[] , int size) { array in memory int total = 0; for(int i=0; i < size; i++){ • The called function can now use 520 total += data[i]; } return total; to access the original array (read it } or write new values to it) int main() { int vals[100]; main() /* some code to initialize vals */ int mysum = sum( vals , 100); cout << mysum << endl; 520 916 // prints sum of all numbers [0] [99] return 0; … ? ? data } 520 vals sum() return val

  7. 7 Analogy • The first house on the 3600 block of Catalina Ave. has the address 3600. • How many houses are on that block? • There is no way to know!! We would have to count that separately.

  8. 8 Arrays in C/C++ vs. Other Languages // Function that takes an array • Notice that if sum() only has the start address it int sum( int data[] , int size); would not know how big the array is int sum( int data[] , int size) • Unlike Java or other languages where you can { int total = 0; call some function to find the size of an array, for(int i=0; i < size; i++){ C/C++ require you to track the size yourself in a total += data[i]; } separate variable and pass it as a secondary return total; argument } int main() { int vals[100]; main() /* some code to initialize vals */ int mysum = sum( vals , 100); cout << mysum << endl; 100 520 916 // prints sum of all numbers [0] [99] return 0; … 100 ? ? } 520 vals data size sum() return val

  9. 9 Why Don't We Return Arrays from Functions // Function that takes an array • In C++, we generally do NOT return int[] fill( int data[] , int size); void fill( int data[] , int size); arrays from a function…because we int[] fill( int data[] , int size) do NOT need to! void fill( int data[] , int size) { • WHY? for(int i=0; i < size; i++){ data[i] = i; } – Because we modified the original array in } the function int main() { int vals[100]; main() /* some code to initialize vals */ fill( vals , 100); cout << vals[0] << endl; 100 520 916 // prints sum of all numbers [0] [99] return 0; … 100 ? ? } 520 vals data size fill()

  10. 10 Summary • Syntax: – In the prototype/signature: Put empty square brackets after the parameter name if it is an array (e.g. void f1(int data[]) ) – When you call the function, just provide the name of the array (e.g. f1(data); ) • Functions only know what you pass them – You must pass the size of the array as an additional parameter in addition to the link to the array – Arrays are passed-by-reference meaning no copy is made and changes by a function are actually being made to the original • The C++ std:: library provides some alternatives to "plain-old arrays" (like vectors), but you will learn about these in CS 103/104 and should not use them in CS 102

Recommend


More recommend