Functions makes our program modular and maintainable. Big applications can have hundreds of functions.
Array is a data structure to store homogeneous collection of data. Arrays are equally important as functions. In programming we often use arrays and functions together.
Here in this post I will explain how to pass and return array from function in C programming.
How to pass single dimensional array to function?
In C you can pass single dimensional arrays in two ways. You can either pass it directly to a function. Or you can also pass it as a pointer to array.
Let us see both ways to pass single dimensional array to function one after one.
1. Passing array directly to function
You can pass single dimensional array directly to functions as you pass other variables.
2. Passing array using pointer
Since array and pointers are closely related to each other. Hence you can also pass an array to function as pointer.
Important Note: Arrays in C are passed as reference not by value. Which means any changes to array within the function will also persist outside the function.
How to return single dimensional array from function?
In C you cannot return an array directly from a function. But that does not impose a restriction on C language. There are two ways to return an array indirectly from a function.
1. Return pointer pointing at array from function
C does not allow you to return array directly from function. However, you can return a pointer to array from function.
Let us write a program to initialize and return an array from function using pointer.
C compiler reports a warning message on compilation of above program.
It complains about returning address of a local variable. We can return value of a local variable but it is illegal to return memory location that is allocated within function on stack. Since, after program control is returned from the function all variables allocated on stack within function are freed. Hence, returning a memory location which is already released will point at no man’s land.
But overlooking the compilers warning message let us run the program. On execution it produces following output which is somewhat weird.
It is clear that inside function our array successfully got initialized, but something awful happened after returning it from function.
To overcome this you can either allocate array dynamically using malloc()
function. Or declare array within function as static variable. Or you can pass the array to be returned as a parameter to the function.
However the best practice is to either pass array to return as parameter or allocate array dynamically using malloc()
function.
2. Pass the returned array as parameter
Arrays in C are passed by reference, hence any changes made to array passed as argument persists after the function. So you can accept the output array you need to return, as a parameter to the function.
The above program on execution prints.
How to pass multi-dimensional array to function?
Multi-dimensional arrays are passed in the same fashion as single dimensional. Which means you can pass multi-dimensional array to a function in two ways.
1. Passing multi-dimensional array directly to function
This is the simplest way to pass a multi-dimensional array to functions. Pass the array as other variables.
2. Passing multi-dimensional array to function using pointer
Important Note:
int (*mat)[COLS]
andint * mat[COLS]
both are different. First is a pointer to array whereas second is array of pointers.
How to return multi-dimensional array from function?
Returning multi-dimensional array from function is similar as of returning single dimensional array. Which means you can either return a pointer to array or pass the array to return as a function parameter.
Conclusion
We learned to pass array and return array from a function. Passing an array to function is not big deal and is passed as other variables. However always mind that arrays are passed by reference. Returning an array from function is not as straight as passing array to function. There are two ways to return an array from function. But I personally prefer to pass array to return as argument and fill the resultant array inside function with processed result.