Menu
×
   ❮     
HOME Computer Fundamentals C Language C++ MS-OFFICE TALLY 11/12 SCIENCE 11/12 COMM BCA BCCA BBA MCA
     ❯   

Computer

▸ Computer Fundamentals▸ What is Computer▸ History of Computer▸ Types of Computer▸ History of C Language▸ History of C Language

Computer Components

▸ Computer components▸ Input Devices▸ Output Devices▸ CPU▸ Hardware

Computer Memory

▸ Computer Memory▸ Register Memory

Computer Virus

▸ Computer virus▸ Computer virus 2

Computer Network

▸ Computer Network details

Number Systems

▸ Number Systems

C Control Statements

▸ C if else▸ C Switch Statement▸ if-else vs switch▸ C Loops▸ C Do- While Loop▸ C While Loop▸ C For Loop▸ C For Loop▸ Nested Loops in C▸ Infinite Loop in C▸ C break▸ C continue▸ C goto ▸ C goto ▸ C goto ▸ C goto ▸ C goto ▸ Type Casting

C Functions

▸ What is Function▸ Recursion in C▸ Call: Value & Reference▸ Call: Value & Reference▸ Storage Classes▸ Storage Classes

C Array

▸ 1 D - Array▸ Return an Array in C▸ Array To Function ▸ 2 - D Array▸ Return an Array in C

C Pointers

▸ C Pointers▸ C Double Pointer (Pointer to Pointer)▸ C Double Pointer (Pointer to Pointer)

C Tutorial

▸ Data Types in C▸ Keywords in C▸ C Identifiers▸ Escape Sequence in C▸ ASCII value in C▸ Constants in C▸ Static in C▸ Programming Errors in C▸ Compile time vs Runtime▸ Compile time vs Runtime▸ Compile time vs Runtime▸ Conditional Operator in C▸ Bitwise Operator in C▸ Bitwise Operator in C▸ 2s complement in C▸ What is C Language▸ History of C Language▸ Features of C Language▸ How to install C▸ First C Program▸ Compilation process in c▸ Printf Scanft▸ Variables in C ▸ C Operators▸ Comments in C▸ C Format Specifier▸ Literals in C▸ Tokens in C▸ C Boolean▸ Programming Errors in C

Passing Array to Function in C ❮ Edit Details

In C, there are various general problems which requires passing more than one variable of the same type to a function. For example, consider a function which sorts the 10 elements in ascending order. Such a function requires 10 numbers to be passed as the actual parameters from the main function. Here, instead of declaring 10 different numbers and then passing into the function, we can declare and initialize an array and pass that into the function. This will resolve all the complexity since the function will now work for any number of values.

As we know that the array_name contains the address of the first element. Here, we must notice that we need to pass only the name of the array in the function which is intended to accept an array. The array defined as the formal parameter will automatically refer to the array specified by the array name defined as an actual parameter.

Consider the following syntax to pass an array to the function.

  1. functionname(arrayname);//passing array  

Methods to declare a function that receives an array as an argument

There are 3 ways to declare the function which is intended to receive an array as an 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 a pointer. In pointer chapter, we will learn about it.

C language passing an 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. }    
  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

C function to sort the array

  1. #include<stdio.h>   
  2. void Bubble_Sort(int[]);  
  3. void main ()    
  4. {    
  5.     int arr[10] = { 10, 9, 7, 101, 23, 44, 12, 78, 34, 23};     
  6.     Bubble_Sort(arr);    
  7. }    
  8. void Bubble_Sort(int a[]) //array a[] points to arr.   
  9. {  
  10. int i, j,temp;     
  11.     for(i = 0; i<10; i++)    
  12.     {    
  13.         for(j = i+1; j<10; j++)    
  14.         {    
  15.             if(a[j] < a[i])    
  16.             {    
  17.                 temp = a[i];    
  18.                 a[i] = a[j];    
  19.                 a[j] = temp;     
  20.             }     
  21.         }     
  22.     }     
  23.     printf("Printing Sorted Element List ...\n");    
  24.     for(i = 0; i<10; i++)    
  25.     {    
  26.         printf("%d\n",a[i]);    
  27.     }  
  28. }  

Output

Printing Sorted Element List ...
7  
9  
10  
12  
23 
23  
34  
44  
78  
101  

Returning array from the function

As we know that, a function can not return more than one value. However, if we try to write the return statement as return a, b, c; to return three values (a,b,c), the function will return the last mentioned value which is c in our case. In some problems, we may need to return multiple values from a function. In such cases, an array is returned from the function.

Returning an array is similar to passing the array into the function. The name of the array is returned from the function. To make a function returning an array, the following syntax is used.

  1. int * Function_name() {  
  2. //some statements;   
  3. return array_type;  
  4. }  

To store the array returned from the function, we can define a pointer which points to that array. We can traverse the array by increasing that pointer since pointer initially points to the base address of the array. Consider the following example that contains a function returning the sorted array.

  1. #include<stdio.h>   
  2. int* Bubble_Sort(int[]);  
  3. void main ()    
  4. {    
  5.     int arr[10] = { 10, 9, 7, 101, 23, 44, 12, 78, 34, 23};     
  6.     int *p = Bubble_Sort(arr), i;  
  7.     printf("printing sorted elements ...\n");  
  8.     for(i=0;i<10;i++)  
  9.     {  
  10.         printf("%d\n",*(p+i));  
  11.     }  
  12. }    
  13. int* Bubble_Sort(int a[]) //array a[] points to arr.   
  14. {  
  15. int i, j,temp;     
  16.     for(i = 0; i<10; i++)    
  17.     {    
  18.         for(j = i+1; j<10; j++)    
  19.         {    
  20.             if(a[j] < a[i])    
  21.             {    
  22.                 temp = a[i];    
  23.                 a[i] = a[j];    
  24.                 a[j] = temp;     
  25.             }     
  26.         }     
  27.     }     
  28.     return a;  
  29. }  

Output

Printing Sorted Element List ...
7  
9   
10  
12  
23 
23  
34  
44  
78  
101