Arrays and Functions

Passing Arrays to Functions ( in C)

  • When an entire array is passed to a function, the size of the array is usually passed as an additional argument.
  • For a one dimensional array, the function's formal parameter list does not need to specify the dimension of the array.  If such a dimension is provided, the compiler will ignore it.
  • For a multi-dimensional array, all dimensions except the first must be provided in a functions formal parameter list.  The first dimension is optional, and will be ignored by the compiler.
  • A partially qualified multi-dimensional array may be passed to a function, and will be treated as an array of lower dimension.  For example, a single row of a two dimensional array may be passed to a function which is expecting a one dimensional array. 

1.) User initializes the array using statement like int d [ ] = { 1,2,3,4}; instead of this, a function can also be directly called to initialize the array.
Example : Initializing an array using function

2.) Passing array elements to functions Arrays are collection of one or more elements of same data type .Array elements can be passed to the function by value or reference.
Below is an example passing array elements to function