Using arrays as arguments to functions and return values in the C language is a simple start

  • 2020-05-07 20:05:14
  • OfStack

The function takes an array as an argument

If you want to use a 1-dimensional array as an argument to a function, you must declare a function form argument. The following three ways produce similar results as all three declaration methods, because each way tells the compiler that an integer pointer will be received. Similarly, you can use multidimensional array form parameters.

Way - 1
The formal parameter is the pointer as follows. In the next chapter you will learn what Pointers are.

void myFunction(int *param)
{
.
.
.
}
Way - 2
Size of array parameters in the following form:

void myFunction(int param[10])
{
.
.
.
}
Way - 3
The following form of array parameters as variable size:

void myFunction(int param[])
{
.
.
.
}
example
Now, consider the following function, which will require 1 array as another parameter, and will return the average value of the array according to the passed parameter, as follows:


double getAverage(int arr[], int size)
{
 int  i;
 double avg;
 double sum;

 for (i = 0; i < size; ++i)
 {
  sum += arr[i];
 }

 avg = sum / size;

 return avg;
}

Now, let's call the above function as follows:


 #include <stdio.h>
 
/* function declaration */
double getAverage(int arr[], int size);

int main ()
{
  /* an int array with 5 elements */
  int balance[5] = {1000, 2, 3, 17, 50};
  double avg;

  /* pass yiibaier to the array as an argument */
  avg = getAverage( balance, 5 ) ;
 
  /* output the returned value */
  printf( "Average value is: %f ", avg );
  
  return 0;
}

When the above code is compiled and executed in 1, it produces the following results:


Average value is: 214.400000

As you can see, the length of the array is not important, as long as the C language functions do not do boundary checking for formal arguments.

The function returns an array
C language programming does not allow you to return an entire array as an argument to a function. However, you can return an array of Pointers without an index by specifying the array name. If you want to return a 1-dimensional array from a function, you must declare to return a pointer, as in the following example:

int * myFunction()
{
.
.
.
}
The second point to keep in mind is that the C language does not advocate that the address of a local variable be returned outside of a function, so you must define a local variable as a static variable.

Now, consider the following function, which will generate 10 random Numbers and return them using an array, and call this function as follows:

 


#include <stdio.h>

/* function to generate and return random numbers */
int * getRandom( )
{
 static int r[10];
 int i;

 /* set the seed */
 srand( (unsigned)time( NULL ) );
 for ( i = 0; i < 10; ++i)
 {
   r[i] = rand();
   printf( "r[%d] = %d
", i, r[i]);

 }

 return r;
}

/* main function to call above defined function */
int main ()
{
  /* a yiibaier to an int */
  int *p;
  int i;

  p = getRandom();
  for ( i = 0; i < 10; i++ )
  {
    printf( "*(p + %d) : %d
", i, *(p + i));
  }

  return 0;
}

When the above code is compiled and executed from 1, it produces the following:


r[0] = 313959809
r[1] = 1759055877
r[2] = 1113101911
r[3] = 2133832223
r[4] = 2073354073
r[5] = 167288147
r[6] = 1827471542
r[7] = 834791014
r[8] = 1901409888
r[9] = 1990469526
*(p + 0) : 313959809
*(p + 1) : 1759055877
*(p + 2) : 1113101911
*(p + 3) : 2133832223
*(p + 4) : 2073354073
*(p + 5) : 167288147
*(p + 6) : 1827471542
*(p + 7) : 834791014
*(p + 8) : 1901409888
*(p + 9) : 1990469526


Related articles: