OPPS - [C++ Array as Arguments]

♠ Posted by Unknown in at 09:53

Passing arrays to function


Like the values of simple variables, it is also possible to pass the values of an array to a function. To pass a one-dimensional an array to a called function, it is sufficient to list the name of the array, without any subscripts, and the size of the array as arguments. For example, the call

                        largest(a,n)

will pass  the whole array "a" to the called function. The called function expecting this call must be appropriately defined. The largest function header might look like:

                        float largest(float array[], int size)

The function largest is defined to take two arguments, the array name and the size of the array to specify the number of elements in the array.

#include<iostream.h>
#include<conio.h>
float largest(float a[], int n);
void main()
{
 float value[4] = {2.5, -4.75, 1.2, 3.67};
 cout<<"Largest Value Is : "<<largest(value,4);
 getch();
}

float largest(float a[], int n)
{
 int i;
 float max;
 max = a[0];
 for(i=0; i<n; i++)
 {
  if(max < a[i])
            max = a[i];
 }
 return(max);
}

0 comments:

Post a Comment