There are a variety of general situations in C that need sending several variables of the same type to a function. Consider a function that sorts the 10 elements in ascending order, for example. The real parameters from the main function must be given as ten numbers to this function. Instead of declaring ten individual numbers and then giving them into the method, we may declare and initialize an array and then pass it into it. This removes all of the complications because the function now works with any amount of values.
The array name, as we know, holds the address of the first member.
We must keep in mind that in a function that accepts an array, we only need to give the name of the array. The array provided by the array name defined as an actual parameter will automatically refer to the array specified by the formal parameter.
To pass an array to the function, use the syntax below.
functionname(arrayname);/array passing
Methods for declaring a function using an array as an argument.
There are three ways to declare a function that will take an array as an argument.
First way:
- return_type function(type arrayname[])
Declaring blank subscript notation [] is a widely used technique.
Second way:
- return_type function(type arrayname[SIZE])
Optionally, we can define size in subscript notation [].
Third way:
- return_type function(type *arrayname)
What do you think?