Title: Topic 9A Arrays as Function Arguments
1Topic 9A Arrays as Function Arguments
2Arrays 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.
3Array 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.
4Array Elementsas Function Arguments
int x5 1, 2, 3, 4, 5 float
average average avg_of5(x0,x1,x2,x3,x4
) 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
5Array Elementsas 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.
6Array Elementsas Function Arguments
int x6 1, 2, 3, 4, 5, 0 tot_of5(x0,
x1, x2, x3, x4, x5) printf(The
total is d.,x5) ... void tot_of5(int a, int
b, int c, int d, int e, int p_f) p_f a
b c d e
7Arrays 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_array0
8Arrays 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. - Nowhow 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.
9Arrays 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)
-
-
10Arrays 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 lt
n i) arrayi set_value
11Arrays 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 lt
n i) arrayi set_value
12Arrays 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(y0,10,27)
void fill_array( int array, int n, int
set_value) int i for (i 0 i lt
n i) arrayi set_value
13Arrays as Function Arguments
- Therefore, for a function such as
- void fill_array(int , int, int)
- We could write a program
int y10 fill_array(y, 10, 27) fill_array(y0
, 10, 27) ... if (y y0) printf(They are
the same!)
14Array 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)
15Array 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 lt
n i) arrayi set_value
16Array Name andPointer Equivalence
- Therefore, these two functions are equivalent
void fill_array( int array, int n, int
set_value) int i for (i 0 i lt
n i) arrayi set_value
void fill_array( int array, int n, int
set_value) int i for (i 0 i lt
n i) arrayi set_value
17Array Name andPointer Equivalence
- So, which way is better?
- Wellthe 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 )
18Arrays 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.
19Arrays are Call-by-Reference
int x5 0, 0, 0, 0, 0 printf(d d d d
d\n,x0,x1,x2,x3,x4) fill_array(x,5,27
) printf(d d d d d\n,x0,x1,x2,x3,x
4) ... void fill_array(int array, int n,
int set_value) int i for (i 0 i lt n
i) arrayi set_value
Program Output 0 0 0 0 0 27 27 27 27 27
20Arrays as StrictlyFunction 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).
21Arrays as StrictlyFunction Input Parameters
- Thus, a function to compute the sum of an array,
and return it, would look like
int array15 1, 2, 3, 4, 5 int sum,
array1_size5 sum array_total(array1,
array1_size) ... int array_total(const int x,
int size) int counter, total 0 for (count
0 count lt size count) total
xcount return total
22Arrays as StrictlyFunction 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.
23The 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.
24The 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 lt
size count) total xcount return
total
25Summary 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.
26Arrays 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.
27Arrays 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
28Arrays 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.
29Arrays 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 lt n
count) outputcount input1count
input2count return
30Arrays as Function Output
- To call this function, we would write
int array15 1, 2, 3, 4, 5 int array25
6, 7, 8, 9, 10 int array35 int
i add_arrays(array1, array2, array3, 5) for
(i0 ilt5 i) printf(d ,array3i)
Program Output 7 9 11 13 15
31Arrays 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.
32Summary
- 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.
33Summary
- 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.