Passing Array To Function In C

Consider the subsequent syntax for transmitting an array as an argument to the function.

Example

functionname(arrayname);//passing array

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

There are three methods to define a function that is designed to accept an array as a parameter.

First way:

Example

return_type function(type arrayname[])

Defining an empty square bracket notation is a commonly employed method.

Second way:

Example

return_type function(type arrayname[SIZE])

Optionally, we have the choice to specify dimensions using subscript notation .

Third way:

Example

return_type function(type *arrayname)

You can also apply the idea of a pointer. In the section dedicated to pointers, we will delve deeper into its usage.

C language passing an array to function example

Example

Example

#include<stdio.h>
int minarray(int arr[],int size){  
int min=arr[0];  
int i=0;  
for(i=1;i<size;i++){  
if(min>arr[i]){  
min=arr[i];  
}  
}//end of for  
return min;  
}//end of function  
  
int main(){    
int i=0,min=0;  
int numbers[]={4,5,7,3,8,9};//declaration of array  

min=minarray(numbers,6);//passing array with size  
printf("minimum number is %d \n",min);  
return 0;
}

Output

Output

minimum number is 3

C function to sort the array

Example

Example

#include<stdio.h> 
void Bubble_Sort(int[]);
void main ()  
{  
    int arr[10] = { 10, 9, 7, 101, 23, 44, 12, 78, 34, 23};   
    Bubble_Sort(arr);  
}  
void Bubble_Sort(int a[]) //array a[] points to arr. 
{
int i, j,temp;   
    for(i = 0; i<10; i++)  
    {  
        for(j = i+1; j<10; j++)  
        {  
            if(a[j] < a[i])  
            {  
                temp = a[i];  
                a[i] = a[j];  
                a[j] = temp;   
            }   
        }   
    }   
    printf("Printing Sorted Element List ...\n");  
    for(i = 0; i<10; i++)  
    {  
        printf("%d\n",a[i]);  
    }
}

Output

Output

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

Returning array from the function

While it's a known fact that a function is unable to provide more than a single value upon return, attempting to use a return statement like return a, b, c; to send back three values (a, b, c) will result in the function only returning the final value mentioned, which in this scenario is c. There are instances where it becomes necessary to return multiple values from a function. In such situations, an array is typically utilized to return these values collectively.

To return an array from a function mirrors the process of passing the array into the function. The function returns the array's identifier. When creating a function that returns an array, you utilize the syntax below.

Example

int * Function_name() {
//some statements; 
return array_type;
}

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 thaLogic Practiceer since pointer initially points to the base address of the array. Consider the following example that contains a function returning the sorted array.

Example

Example

#include<stdio.h> 
int* Bubble_Sort(int[]);
void main ()  
{  
    int arr[10] = { 10, 9, 7, 101, 23, 44, 12, 78, 34, 23};   
    int *p = Bubble_Sort(arr), i;
    printf("printing sorted elements ...\n");
    for(i=0;i<10;i++)
    {
        printf("%d\n",*(p+i));
    }
}  
int* Bubble_Sort(int a[]) //array a[] points to arr. 
{
int i, j,temp;   
    for(i = 0; i<10; i++)  
    {  
        for(j = i+1; j<10; j++)  
        {  
            if(a[j] < a[i])  
            {  
                temp = a[i];  
                a[i] = a[j];  
                a[j] = temp;   
            }   
        }   
    }   
    return a;
}

Output

Output

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

Input Required

This code uses input(). Please provide values below: