Passing Arrays to Function in C

In C programming, a single dimensional array element or an entire array can be passed to a function. Here, you would have to declare a formal parameter in one of following three ways and all three declaration methods produce similar results.

Way – 1

Formal parameters as a pointer −

1

2

3

4

5

voidmyFunction(int*param){

.

.

.

}

You can also use the concept of a pointer. In pointer chapter, we will learn about it.

Way – 2

Formal parameters as a sized array −

1

2

3

4

5

voidmyFunction(intparam[10]){

.

.

.

}

Optionally, we can define size in subscript notation [].

Way – 3

Formal parameters as an unsized array −

1

2

3

4

5

voidmyFunction(intparam[]){

.

.

.

}

Declaring blank subscript notation [] is the widely used technique.

Example

Now, consider the following function, which takes an array as an argument along with another argument and based on the passed arguments, it returns the average of the numbers passed through the array as follows:

1) Using unsized array

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

#include <stdio.h>

/* function declaration */

doublegetAverage(intarr[],intsize);

intmain()

{

/* an int array with 5 elements */

intbalance[5]={55,20,35,27,38};

doubleavg;

/* pass pointer to the array as an argument */

avg=getAverage(balance,5);

/* output the returned value */

printf("Average value is: %f ",avg);

//exit status

return0;

}

// calling function

doublegetAverage(intarr[],intsize){

inti;

doubleavg;

doublesum=0;

for(i=0;i<size;++i)

{

sum+=arr[i];

}

avg=sum/size;

returnavg;

}

When you run above program, the output will be following:

1

Average value is: 35.000000

2) Using Pointer

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

#include <stdio.h>

intadd_up(int*a,intnum_elements);

intmain(){

intorders[5]={100,220,37,16,98};

printf("Average of numbers is %d\n",add_up(orders,5));

return0;

}

intadd_up(int*a,intnum_elements){

intavg=0;

intk;

for(k=0;k<num_elements;k++){

avg+=a[k];

}

avg=avg/num_elements;

return(avg);

}

When you run above program, the output will be following:

1

Average value is94

3) Using sized array

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

#include <stdio.h>

/* function declaration */

doublegetAverage(intarr[5],intsize);

intmain()

{

/* an int array with 5 elements */

intbalance[]={55,20,35,27,38};

doubleavg;

/* pass pointer to the array as an argument */

avg=getAverage(balance,5);

/* output the returned value */

printf("Average value is: %f ",avg);

//exit status

return0;

}

// calling function

doublegetAverage(intarr[5],intsize){

inti;

doubleavg;

doublesum=0;

for(i=0;i<size;++i)

{

sum+=arr[i];

}

avg=sum/size;

returnavg;

}

When you run above program, the output will be following:

1

Average value is: 35.000000

Passing Multi-dimensional Arrays to Function

Following example shows how to calculate the average by passing multi-dimensional arrays to function.