Passing Array to Function

Course- 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.

 
  1. functionname(arrayname);//passing array  

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

First way:

 
  1. return_type function(type arrayname[])  

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

Second way:

 
  1. return_type function(type arrayname[SIZE])  

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

Third way:

 
  1. 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. #include <conio.h>    
  3. int minarray(int arr[],int size){  
  4. int min=arr[0];  
  5. int i=0;  
  6. for(i=1;i<size;i++){  
  7. if(min>arr[i]){  
  8. min=arr[i];  
  9. }  
  10. }//end of for  
  11. return min;  
  12. }//end of function  
  13.   
  14. void main(){    
  15. int i=0,min=0;  
  16. int numbers[]={4,5,7,3,8,9};//declaration of array  
  17. clrscr();    
  18.   
  19. min=minarray(numbers,6);//passing array with size  
  20. printf("minimum number is %d \n",min);  
  21.   
  22. getch();    
  23. }    

Output

minimum number is 3