The Islamic University of Gaza Engineering Faculty Department of Computer Engineering Fall 2017 ECOM 2003 Muath i.alnabris Computer Programming: C++ Experiment #7 Arrays Part II
Passing Array to a Function At some point, we may need to pass an array to a function as a parameter. In C++, it is not possible to pass the entire block of memory represented by an array to a function directly as an argument. But what can be passed instead is its address. In practice, this has almost the same effect, and it is a much faster and more efficient operation. To accept an array as parameter for a function, the parameters can be declared as the array type, but with empty brackets, omitting the actual size of the array. For example: void procedure (int arg[]) This function accepts a parameter of type "array of int" called arg. In order to pass to this function an array declared as: int myarray [40]; it would be enough to write a call like this: procedure (myarray); Example 1: Passing One-dimensional Array to a Function 2
In the code above, the first parameter (int arg[]) accepts any array whose elements are of type int, whatever its length. For that reason, we have included a second parameter that tells the function the length of each array that we pass to it as its first parameter. This allows the for loop that prints out the array to know the range to iterate in the array passed, without going out of range. Example 2: Passing Multidimensional Array to a Function C++ Program to display the elements of two dimensional array by passing it to a function 3
In the above program, the multi-dimensional array num is passed to the function display.)( Inside, display() function, the array n (num) is traversed using a nested for loop. The program uses 2 for loops to iterate over the elements inside a 2-dimensional array. If it were a 3-dimensional array, you should use 3 for loops. Finally, all elements are printed onto the screen. Note: Multidimensional array with dimension more than 2 can be passed in similar way as two dimensional array. Scope of variables: A scope is a region of the program and broadly speaking there are three places, where variables can be declared Inside a function or a block which is called local variables, - In the definition of function parameters which is called formal parameters - Outside of all functions which is called global variables. 4
This section special with function Pass By Reference: The call by reference method of passing arguments to a function copies the reference of an argument into the formal parameter. Inside the function, the reference is used to access the actual argument used in the call. This means that changes made to the parameter affect the passed argument. To pass the value by reference, argument reference is passed to the functions just like any other value. So accordingly you need to declare the function parameters as reference types as in the following function swap(), which exchanges the values of the two integer variables pointed to by its arguments. For now, let us call the function swap() by passing values by reference as in the following example 5
When the above code is put together in a file, compiled and executed, it produces the following result 6
Lab work: 1- Write function to display a matrix as shown below. The diagonal of the matrix fills with 0. The lower side fills will -1s and the upper side fills with 1s. 2- Write program Take a numbers from user and insert a random new values in array Then write function to sort this numbers 7
Homework: 1- Write program multiple two matrices using function, saves it in another matrix (twodimensional array) and displays it on the screen. 2- write C++ program to display a table of numbers as shown below: 8