// Lab 11 Lecture // I have included several programs in the same file. Comment out all // programs except for the one you wish to run. // (Eclipse: ctrl+/ to comment and uncomment) // (Code Blocks: ctrl+shift+C to comment and ctrl+shift+x to uncomment) // ---------------------------- Pass by Reference ----------------------------- //// Passing by value and pass by reference. //// //// In the last lab we learned how to pass a variable by value. We learned that //// when passing a variable by value, the value is passed to the function //// and put inside a newly created variable. Any changes to this new variable //// do not change the value of the variable in main (the calling function). //// //// Passing a variable by reference passes the address of the variable to the //// function. The new variable is assigned the same address as the argument //// variable. This means that any changes to the variable in the function, //// will also change the variable in main (because they are the same variable //// with different names). //// //// This can be demonstrated with the following program. Note that when you //// want to pass a variable by reference an ampersand (&) must be in your //// prototype and function definition between the variables type and name. //// //// E.g. void myFunction(int & myVar) // //#include //using namespace std; // //void add_Function(int, int &); // //int main() //{ // int pass_by_value = 10; // variable is passed by value to function // int pass_by_reference = 10; // variable is passed by reference to function // // cout << "pass by value in main at top : " << pass_by_value << endl; // cout << "pass by reference in main at top : " << pass_by_reference; // cout << endl << endl; // // add_Function(pass_by_value, pass_by_reference); // // cout << "pass by value in main at bottom : " << pass_by_value << endl; // cout << "pass by reference in main at bottom : " << pass_by_reference; // cout << endl << endl; // // return 0; //} // //void add_Function(int val, int & ref) //{ // val += 5; // add 5 to both val and ref // ref += 5; // // cout << "pass by value inside function : " << val << endl; // cout << "pass by reference inside function : " << ref; // cout << endl << endl; //} //// As we can see at the end of main our pass by value was not changed in //// main, however pass by reference kept the value changed in the function. // ------------------- Passing an Array to a Function ------------------------- //// Here is how we can pass an array to a function. Note the syntax. // // //#include //using namespace std; // //// this is a prototype with an array. There are 2 acceptable syntaxes: //// 1. type name ( type []) ; //// 2. type name ( type arrayName []) ; //void array_func(double []); // //int main() //{ // double myArray [5] = {1.1, 1.2, 1.3, 1.4, 1.5}; // // // ** In the function call you only include the name of the array! ** // array_func(myArray); // // return 0; //} // //// Function definition //// type name ( type arrayName []) //void array_func (double myArray []){ // cout << "Array contents:" << endl; // for(int i=0; i < 5; i++){ // cout << arrayName[i] << " "; // } // cout << endl; //} // -------------- Closer Look at Arrays and Pass By Reference ----------------- //// Arrays are always pass by reference. //// //// This is because an array is simply the address to the first element in an //// array. //// //// Take the following example. The following is an array allocated in the //// memory addresses 1302 - 1305. //// //// int array [4] = {5, 10, 15, 20}; //// ___________________________________________ //// |______|___5___|__10__|__15__|__20__|______| << memory //// ..1301 1302 1303 1304 1305 1306...<< addresses in memory //// 0 1 2 3 << array index //// //// 'array' will have the address 1302 stored inside of it. Therefore, when //// you pass 'array' as an argument you are actually giving the function the //// array's address in memory. //// //// This is why our index starts at zero. we add the index to our array's address //// to get the new address of the element we want to access. This is an advanced //// concept so don't fret if you do not understand completely. //// //// E.g. array[2] would be 1302 + 2 = 1304 the address of the third element. //// //// These concepts are demonstrated in the following program. // //#include //using namespace std; // //void array_func(int [], const int); // //int main() //{ // const int SIZE = 5; // int my_arr [SIZE] = {10, 20, 30, 40, 50}; // // cout << "The address of our array 'my_arr': "<< int(my_arr); // cout << endl << endl; // // cout << "In main at top, element 0 : " << my_arr[0] << endl; // // array_func(my_arr, SIZE); // // cout << "In main at bottom, element 0 : " << my_arr[0] << endl; // // return 0; //} // //void array_func(int A [], const int S) //{ // A[0] += 5; // cout << "In function (add 5), element 0 : " << A[0] << endl; //} // ------------------------ Functions Example 1 ------------------------------- //// Using functions to process an array. // //#include //using namespace std; // //int arraySum(double [], const int); //double arrayAverage (double [], const int); // //int main() //{ // const int SIZE = 5; // // double my_arr [SIZE] = {10, 20, 30, 40, 50}; // int sum; // double avg; // // sum = arraySum(my_arr, SIZE); // // cout << "The sum of all elements is : " << sum << endl; // // avg = arrayAverage(my_arr, SIZE); // // cout << "The average of all elements is : " << avg << endl; // // return 0; //} // //int arraySum(double A[], const int SIZE) //{ // int total = 0; // // for(int i=0; i //#include //#include //using namespace std; // //void importData(ifstream &, double[], string[], const int, int &); // //int main() //{ // const int MAXSIZE = 100; // max number of students that can be imported // // double gpa [MAXSIZE]; // students gpa // string name [MAXSIZE]; // students name // int numStudents; // number of students imported from file // // // Open input file stream object // ifstream fin; // fin.open("input.txt"); // // if(!fin){ // cout << "ERROR - File not found." << endl; // return 1; // } // // // Import data - if data fails to be imported exit program // importData(fin, gpa, name, MAXSIZE, numStudents); // // // print out arrays // cout << "Name GPA" << endl; // cout << "-------------" << endl; // cout << setprecision(2) << fixed; // // for(int i=0; i> N[count] >> G[count] && count < SIZE) // count++; //}