Presentation is loading. Please wait.

Presentation is loading. Please wait.

Topic 9A – Arrays as Function Arguments. CISC105 – Topic 9A Arrays as Function Arguments There are two ways to use arrays as function arguments: Use an.

Similar presentations


Presentation on theme: "Topic 9A – Arrays as Function Arguments. CISC105 – Topic 9A Arrays as Function Arguments There are two ways to use arrays as function arguments: Use an."— Presentation transcript:

1 Topic 9A – Arrays as Function Arguments

2 CISC105 – Topic 9A Arrays as Function Arguments There are two ways to use arrays as function arguments: Use an array element as a function argument, i.e. passing one integer from an array of integers into a function. Use a full array as a function argument, i.e. passing an entire array into a function.

3 CISC105 – Topic 9A Array Elements as Function Arguments Individual array elements can be used as function arguments in the same way their corresponding data type “simple” variables can be. If a function takes one integer as an parameter, an element of a integer array can be used.

4 CISC105 – Topic 9A Array Elements as Function Arguments int x[5] = { 1, 2, 3, 4, 5 }; float average; average = avg_of5(x[0],x[1],x[2],x[3],x[4]); printf(“The total is %f.”,average);... float function_y(int a, int b, int c, int d, int e) { return (a + b + c + d + e) / 5.0; }

5 CISC105 – Topic 9A Array Elements as Function Arguments Notice that there is no difference between passing a “simple” integer into such a function and passing an element of an integer array into such a function. We can also use array elements as function output parameters. We simply pass in the address of the array element, in the same way as we do with “simple” variables, using the “&” operator.

6 CISC105 – Topic 9A Array Elements as Function Arguments int x[6] = { 1, 2, 3, 4, 5, 0}; tot_of5(x[0], x[1], x[2], x[3], x[4], &x[5]); printf(“The total is %d.”,x[5]);... void tot_of5(int a, int b, int c, int d, int e, int *p_f) { *p_f = a + b + c + d + e; }

7 CISC105 – Topic 9A Arrays as Function Arguments In addition to using array elements as function arguments, an entire array can be used as a function argument. However, this is a somewhat more complicated process. The first point to realize is that an array name (without an index) evaluates to the memory address of the first element of the array. Thus, if integer_array is an array of integers: integer_array == &integer_array[0]

8 CISC105 – Topic 9A Arrays as Function Arguments As the array name is equal to the address of the first element of the array, we can think of this as a pointer to the array. Now…how do we pass arrays as function arguments? To pass an array of variables, the parameter name is followed by a “[]”. Notice that the size of the array is NOT placed inside the brackets.

9 CISC105 – Topic 9A Arrays as Function Arguments Therefore, for a function that takes two arrays, one of floats and one of integers, as parameters, the prototype would look like: void function1(float [], int []);  The function definition would look like: void function1(float x[], int y[]) { }

10 CISC105 – Topic 9A Arrays as Function Arguments Notice that the size of the array is not passed into the function. So, this information must be passed in some other way. A program to set all elements of an integer array to some value, could look like: void fill_array( int array[], int n, int set_value) { int i; for (i = 0; i < n; i++) {array[i] = set_value;} }

11 CISC105 – Topic 9A Arrays as Function Arguments This function takes an array, the size of the array, and the value to set the array elements to as parameters. Thus, to set all 10 elements of the array y to 27, we could call the function: fill_array(y,10,27); void fill_array( int array[], int n, int set_value) { int i; for (i = 0; i < n; i++) {array[i] = set_value;} }

12 CISC105 – Topic 9A Arrays as Function Arguments Remember that the array name is simply the address of the first element of that array. Thus, we could also call the function: fill_array(&y[0],10,27) ; void fill_array( int array[], int n, int set_value) { int i; for (i = 0; i < n; i++) {array[i] = set_value;} }

13 CISC105 – Topic 9A Arrays as Function Arguments Therefore, for a function such as: void fill_array(int [], int, int); We could write a program: int y[10]; fill_array(y, 10, 27); fill_array(&y[0], 10, 27);... if (y == &y[0]) printf(“They are the same!”); These two statements do the exact same thing as y = &y[0]. This is just another demonstration that y and &y[0] are the same.

14 CISC105 – Topic 9A Array Name and Pointer Equivalence Again, the array name is simply the address of the first element of that array, thus a pointer to the array. Therefore, void fill_array(int [], int, int); Could rewritten as: void fill_array(int *, int, int);

15 CISC105 – Topic 9A Array Name and Pointer Equivalence Likewise, we rewrite the function, using pointer notation: void fill_array( int *array, int n, int set_value) { int i; for (i = 0; i < n; i++) {array[i] = set_value;} }

16 CISC105 – Topic 9A Array Name and Pointer Equivalence Therefore, these two functions are equivalent: void fill_array( int *array, int n, int set_value) { int i; for (i = 0; i < n; i++) {array[i] = set_value;} } void fill_array( int array[], int n, int set_value) { int i; for (i = 0; i < n; i++) {array[i] = set_value;} }

17 CISC105 – Topic 9A Array Name and Pointer Equivalence So, which way is better? Well…the second form does not specify weather array is an array of integers, or simply an integer function output parameter. That, as well as being more confusing, show why the first form is preferred. void fill_array( int *array, int n, int set_value ) void fill_array( int array[], int n, int set_value )

18 CISC105 – Topic 9A Arrays are Call-by-Reference OK, so now we know how to pass an array into a function. Notice, however, that when we pass an array, we are simply passing the memory address of the first element. Therefore, arrays are always passed call-by- reference, meaning that if the function we pass an array to modifies that array, that change also shows in the calling function after the return, just like using a function output parameter.

19 CISC105 – Topic 9A Arrays are Call-by-Reference int x[5] = { 0, 0, 0, 0, 0 }; printf(“%d %d %d %d %d\n”,x[0],x[1],x[2],x[3],x[4]); fill_array(x,5,27); printf(“%d %d %d %d %d\n”,x[0],x[1],x[2],x[3],x[4]);... void fill_array(int array[], int n, int set_value) { int i; for (i = 0; i < n; i++) array[i] = set_value; } Program Output: 0 0 0 0 0 27 27 27 27 27

20 CISC105 – Topic 9A Arrays as Strictly Function Input Parameters Sometimes, we want an array to be passed into a function, yet, not allow the function to change the array. This is possible by using the C keyword, const (standing for constant). This keyword is placed before the datatype and tells the C language not to let the function modify the variable (or the array, in this case).

21 CISC105 – Topic 9A Arrays as Strictly Function Input Parameters Thus, a function to compute the sum of an array, and return it, would look like: int array1[5] = { 1, 2, 3, 4, 5 }; int sum, array1_size=5; sum = array_total(array1, array1_size);... int array_total(const int x[], int size) { int counter, total = 0; for (count = 0; count < size; count++) total += x[count]; return total; }

22 CISC105 – Topic 9A Arrays as Strictly Function Input Parameters Notice in this program that the array (named x in the function and named array1 in the main program) cannot be modified by the function as it has been declared as a constant. Also keep in mind that this is the SAME array, as arrays are passed call-by- reference. Also note that the size, an integer, is call-by- value by default; thus changing size in the function cannot change array1_size in the main program.

23 CISC105 – Topic 9A The const Keyword The const keyword will not allow the function to change the array. If the function attempts to change any element of the array, a compiler error will be generated and the program will not compile. This keyword can also be used with variables, in addition to arrays. It has the same meaning; if the function attempts to change the variable, a compiler error will be generated and the program will not compile.

24 CISC105 – Topic 9A The const Keyword Thus, if the function were written like this, not only would a change to size in the function not affect array1_size, but we cannot even change size inside the function. sum = array_total(array1, array1_size);... int array_total(const int x[], const int size) { int counter, total = 0; for (count = 0; count < size; count++) total += x[count]; return total; }

25 CISC105 – Topic 9A Summary: Arrays as Function Input An array element can be passed into a function just like a normal “simple” variable. This is call-by- value (there are two copies, one for the caller and for for the called function.) An array can be passed into a function. This is call-by-reference (any change to the array in the called function changes the array in the calling function). In order to make an array passed into a function input-only (the called function cannot change the array), preface the parameter list (in the prototype and function definition) with the C keyword const.

26 CISC105 – Topic 9A Arrays as Function Output Now we have seen how to pass an array into a function. So, how do we return an array from a function? In the C programming language, it is not legal to specify an array as a function return type. Therefore, in order for a function to have an array as an output, we must use a function output parameter.

27 CISC105 – Topic 9A Arrays as Function Output Suppose we wish to write a function that takes two arrays as input and an array as output. This function adds the first elements of the two input arrays and puts the result into the first element of the output array, the second elements of the input arrays and puts the result into the second element of the output array, etc…

28 CISC105 – Topic 9A Arrays as Function Output Thus, we need the following parameters for this function: const int input1[], const int input2[], int output[], int n For this example, we are assuming all of the arrays are the same size. Note that the parameters do not include the size of the arrays, so we must pass the size separately, in this case, in the variable n.

29 CISC105 – Topic 9A Arrays as Function Output This function may look like: void add_arrays(const int input1[], const int input2[], int output[], int n) { int count; for (count = 0; count < n; count++) output[count] = input1[count] + input2[count]; return; }

30 CISC105 – Topic 9A Arrays as Function Output To call this function, we would write: int array1[5] = { 1, 2, 3, 4, 5 }; int array2[5] = { 6, 7, 8, 9, 10 }; int array3[5]; int i; add_arrays(array1, array2, array3, 5); for (i=0; i<5; i++) printf(“%d ”,array3[i]); Program Output: 7 9 11 13 15

31 CISC105 – Topic 9A Arrays as Function Output Notice that when we pass an array to a function, we do not pass the size of the array. Thus, we need to remember to ensure that the calling function declares the array to be a certain size and both the calling function and the called function should not overstep the bounds on that array. When an array is passed into a function, especially if the array is going to be used for output (the function is going to put values in that array), the size of that array must be passed to the function as well.

32 CISC105 – Topic 9A Summary An array can be passed into a function, as an input parameter, or an output parameter. To do so, the bracket set “[]” is used, in the function prototype and in the function definition, to indicate an array. An array name evaluates to the memory address of the first element of the array. As such, passing an array to a function is always call-by-reference, which allows a function to modify the actual array.

33 CISC105 – Topic 9A Summary In order to not allow a function to modify the array, the C keyword const can be used. This declares the array to be a constant, and any attempt to modify that array causes an error. When an array is passed to a function, the size of that array is not implicitly known. Therefore, we must pass the size of the array as a separate, explicit array.


Download ppt "Topic 9A – Arrays as Function Arguments. CISC105 – Topic 9A Arrays as Function Arguments There are two ways to use arrays as function arguments: Use an."

Similar presentations


Ads by Google