ARRAYS AND FUNCTIONS


C allows arrays to be passed to functions as arguments, so that a function may operate on different arrays with each call.
Arrays are passed by reference.
Arrays are passed to the functions by it it’s name. name of the array denotes the address of the first element of the array. Therefore, any changes done to the array elements by the function are reflected back in the array.

There are two ways to declare an array as a function parameter in a function header or prototype: with or without specifying the size.

1. double average(double a[], int numberOfElements); 

…
 
double average(double a[], int numberOfElements) {
  double sum = 0.0; 
  int    i; 

  for(i = 0; i < numberOfElements; i++)
    sum += a[i]; 
  return sum / numberOfElements

2. double average(double a[5], int numberOfElements); 

…
double average(double a[5], int numberOfElements) {
  double sum = 0.0; 
  int    i; 

  for(i = 0; i < numberOfElements; i++)
    sum += a[i]; 
  return sum / numberOfElements; 
}
Copyright © 2018-2020 TutorialToUs. All rights reserved.