Passing Arrays to Function in C
Passing
Arrays to Function in C
Just as you
can pass primitive type values to functions, you can also pass arrays to a
function. To pass an array to a function, the array name must be specified
without any brackets within the function call. In addition to the array name,
we need to pass the array length as an additional argument. This necessary as
just passing array reference does not tell how many elements are there in the
given array. For example, consider we have an array x of int type with initial
values as shown,
int x[] = {10, 20, 30, 40, 50};
If we want
to modify this array using a function modify () then function call will look
like,
modify (x, n);
As an array is passed to the function so
the corresponding parameter in the called function header must be of array type.
So the function body for our example will be
void modify (int a[],
int m)
{
. . . . . . . . . . .
. . . . . . . . .
. . . . . . . . . . .
. . . . . . . . .
}
Now let us
consider some examples (programs) involving passing arrays to a function.
A program to
show how arrays are passed to a function.
#include
<stdio.h>
#include
<conio.h>
void modify
(int [], int); /*function declaration*/
void main
()
{
int x[] =
{10, 20, 30, 40, 50}; /*array
declaration*/
int I,
count;
clrscr ();
count =
sizeof (x)/ sizeof (x[0]);
/*calculating number of elements*/
printf (“-
- - - From main, before calling - - - - \n”);
for (i=0;
i<vount; i++)
{
printf (“%d\t”, x[i]);
}
modify (x,
count); /*functional call */
printf (“\n
- - - - From main after calling - - - -\n”);
for (i=0;
i<count; i++)
{
printf (“%d\t”,
x[i]);
}
getch ();
}
void modify
(int a[], int m) /*function
definition*/
{
int i;
printf (“\n
- - - - From within function - - - -\n”);
for(i=0;
i<m; i++)
{
a[i] = -1;
printf (“%d\t”,
a[i]);
}
return;
}
OUTPUT
- - - -
From main, before calling - - - -
10 20
30 40 50
- - - -
From within function - - - -
-1 -1
-1 -1 -1
- - - -
From main after calling - - - -
Explanation :
This
program shows how an array is passed to a function. Here, the values in the
array are printed initially before initially before going in function. When the
function is called, reference to the array (x) is passed to function modify ().
In addition, the number of elements in array are also passed . On calling the
function, the control shifts there and statements in its body are executed.
These statements modify the array and all modifications are reflected back to
the original array. This is because the array is passed by reference.
C program to
calculate the mean and standard deviation of n numbers using functions.
#include
<stdio.h>
#include
<conio.h>
#include
<math.h>
float
std_dev (int [], int);
float mean
(int [], int)
void main
()
{
int I, n,
a[20];
clrscr();
printf (“Enter
how many numbers you want to input (<20) :”);
scanf (“%d”,
&n);
printf (“-
- - - - Enter %d integer numbers - - - - -\,”,n);
for (i=0;
i<n; i++)
scanf (“%d”, &a[i]);
printf (“\nStandard
deviation = %0.2f”, std_dev (a, n));
getch ();
}
float
std_dev (int d[], int m)
{
int i,j,
sum = 0;
float x, z;
x = mean
(d, m);
printf (“Mean
of %d numbers that you entered = %0.2f”,m,x);
for (i=0;
i<m; i++)
sum += (x-d [i]*(x-d[i]);
z=
sqrt(float) sum/m);
return (z);
}
float mean
(int d[], int k)
{
int sum =
0, i;
float y;
for (i=0; i<k; i++)
sum += d[i];
y =
(float)sum/k;
return(y);
}
OUTPUT
Enter how
many numbers you want to input (<20) : 5
- - - - -
Enter 5 integer numbers - - - - -
10 21
35 -8 79
Mean of 5
numbers that you entered = 27.40
Standard
deviation = 29.37
Explanation:
When the
entire array is passed as an argument, the contents of the array are not copied
into formal parameter array, instead information about the address of the array
is passed to the function. In this program, array a is passed to the function
std_dev () and mean () where some calculations are performed on the elements
and the value is returned to the calling function, which is then printed.
Leave a Comment