Passing Array to Function in C

To reuse the array operation, we can create functions that receives array as argument. To pass array in function, we need to write the array name only in the function call.

 
  • functionname(arrayname);//passing array  

There are 3 ways to declare function that receives array as argument.

First way:

 
  • return_type function(type arrayname[])  

Declaring blank subscript notation [] is the 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)  

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


C language passing array to function example

 
  1. #include<stdio.h>  
  2. int minarray(int arr[],int size){    
  3. int min=arr[0];    
  4. int i=0;    
  5. for(i=1;i<size;i++){    
  6. if(min>arr[i]){    
  7. min=arr[i];    
  8. }    
  9. }//end of for    
  10. return min;    
  11. }//end of function    
  12.     
  13. int main(){      
  14. int i=0,min=0;    
  15. int numbers[]={4,5,7,3,8,9};//declaration of array    
  16.   
  17. min=minarray(numbers,6);//passing array with size    
  18. printf(“minimum number is %d \n”,min);    
  19. return 0;  
  20. }    

Output

minimum number is 3