In this tutorial, we will learn to c pass arrays to a function with examples.
Pass arrays to a function in C
Sometimes, we may find the need where we will have to pass a certain amount of numbers in the actual parameters of the function, in such cases, it becomes a tough task to handle a large number of parameters so instead of declaring different variables for passing each of them in the function we can instead pass a whole array as a function parameter. In this way, we are supposed to reduce the complexity of our codes.
The name of the arrays generally references the address of the entire array. Therefore, we only need to pass the name of the arrays and it is supposed to be containing the entire arrays.
The array which will be defined as the formal parameter will refer to the array name defined as an actual parameter.
Syntax for C Pass Arrays :
Considering the following syntax for the code to pass an array to the function.
Function_name(array_name);
Following are the methods to declare a function that will have an array as an argument
There are 3 ways for doing so.
1st way:
returntype function(type array_name[])
for e.g:
int num[int n2[]];
A widely used technique is declaring blank subscript notation []
2nd way:
returntype function(type array_name[SIZE])
for e.g:
int nums[int n2[2]];
We can also define size in subscript notation [], this step is optional.
3rd way:
return_type function(type *array_name)
int num[int *n2];
Template Approach (Reference to Array):
This method basically retains all information about the below array. Using this with templates optimizes our method but this method is generally based on reference to an array. The length of the array is calculated automatically at the time of the function call using template dependency so that it can be used to create a reference because as we know that a reference to an array must know the size of the array.
#include <iostream>
using namespace std;
template <size_t N> void print(int (&rr)[N]) {
for (int p : arr) { cout << p << endl;
}
}
int main() {
int arr[]{ 1, 0, 1, 0, 0 };
print(arr);
}
C language passing an array to function example:-
#include<stdio.h>
int riminarray(int arr[],int risize){
int rmin=arr[0];
int ri=0;
for(ri=1;ri<risize;ri++){
if(rmin>arr[ri]){
rmin=arr[ri];
}
}
return rmin;
}
int main(){
int ri=0,rmin=0;
int rinumbers[]={4,5,7,3,8,9};
rmin=riminarray(rinumbers,6);
printf("minimum number present in this arrays is %d \n",rmin);
return 0;
}
Output
The minimum number present in this array is 3.
Returning array from the function
As we are supposed to already know, any function can generally not return more than one particular value. Still, if we are going to try to write down the return statement as return r, a, j; in order to return three required values (r, a, j), the function is going to return the last mentioned value which is going to be j in this particular case. In some of the specific problems, we are probably going to need to return multiple values from only a single function. In cases like this, an array is supposed to be returned from the function.
Returning an array can be considered to be similar to passing the array into the function. The name of the array generally gets returned from the particular function. in order to make any desired function return some array of our choice, the following syntax can come to use.
int * ri_Function_name() {
//any statement of our choice;
return ri_array_type;
}