Passing Array to Functions

A name of an array gives the address of the 0th element of that array, passing the array name is the same as passing the address to the function. It means if there is an array of 3 elements (ex. arr[3]), so to pass the address of array as argument of a function, we can write arr or &arr[0] as argument of a function.

Passing individual array elements to a function

This we use when we want just one element of the array to pass to the function. And the syntax of it is as listed below.

Syntax(while function calling)

<function name>(<array name>[index number of element which we want to pass]);

Syntax of function declaration, the definition is the same as the function to which we pass a simple variable. In this, if we want to pass all array elements, we pass array elements one by one to the function so this is a simple type of function to which we are passing a simple variable. In this type, we have to use a for loop which passes one by one, all elements of an array to the function.

Example program to understand passing individually all array elements to the function

In the below program, there is a function named find. Which takes one int variable as an argument and prints it. In void main there is a for loop that passes all elements of the arr array starting from index 0 to 4. So one by one all values of an array is going to be printed using printf statement in the function.

#include<stdio.h>
#include<conio.h>
void find(int); //this function is used to find variable is positive or negative or zero

void main()
{
int i,arr[5]={-2,-1,0,1,2};
clrscr();
for(i=0;i<5;i++)
{
find(arr[i]);
}
getch();
}
void find(int a)
{
    if(a<0)
    printf("%d is negative \n",a);
    else if(a>0)
    printf("%d is positive \n",a);
    else if(a==0)
    printf("%d is zero \n",a);
}

Output:

Passing whole 1-D array to the function

To pass the whole array to the function, we just pass the name of that array because it automatically passes the address of the 0th element of an array. So the function will get an address array.

Syntax of function call, declaration, definition for passing 1-D array to the function is as shown below.

function declaration

<return type>   <function name>(<data type of array>[]); //here [] is written because we are passing the array

function call

<function name>(<array name>);

function definition

// here inside [], we can write index if we want

<return type> <function name>(< data type of array >   <array name>[])
{
<inner declaration part is same as normal function>
}
Example to find a number is positive or negative or zero, by passing the whole array to function

In the below program, there is an array arr up to 5 indexes. In the function call, the name of an array is written as an argument of the function. The name of an array is the same as the address of the 0th element of the array. So we can say that we are passing the address of the 0th element of the array in the function call.

To hold the array, we need an array. So in a function definition, in the argument we have to write an array type variable to hold an array which is arr here in function. And in function, there is a for loop which will start the value of i from 0 to 4 and it prints all array elements by using i as the index of an array.

#include<stdio.h>
#include<conio.h>
void find(int []);
void main()
{
int i,arr[5]={-2,-1,0,1,2};
clrscr();
find(arr);
getch();
}

void find(int arr[])
{  int i;
    for(i=0;i<5;i++)
    {
    if(arr[i]<0)
    printf("%d is negative \n",arr[i]);
    else if(arr[i]>0)
    printf("%d is positive \n",arr[i]);
    else if(arr[i]==0)
    printf("%d is zero \n",arr[i]);
    }
}

Output

Passing a 2-D array to the function

Syntax of function call, declaration, definition for passing 2-D array to the function is as shown below.

function call

<function name>(<array name>);

function definition

// here inside 1st [], we can write index if we want but 2nd index is compulsory to write

<return type> <function name>(< data type of array >   <array name>[][<2nd index>])

{
<inner declaration part is same as normal function>
}
Example program to pass a 2-D array to the function

The below program is to pass a 2-D array to function and print all values of a 2-D array using function. That array is arr here. It is having 2*3 = 6 elements. This is the same as the previous program Example program to pass a 1-D array to function. Here also we are passing array names as the argument to a function. But to hold this 2-D array we need a 2-D array in function. And i and j are used as the 1st and 2nd index of an array. And 2 for loops are used to print elements.

#include<stdio.h>
#include<conio.h>
void main()
{
int i,arr[2][3]={{1,2,3},{4,5,6}};
clrscr();
find(arr);
getch();
}
void find(int arr[2][3])
{	int i,j;
    for(i=0;i<2;i++)
    {
        for(j=0;j<3;j++)
        {
        printf("arr[%d][%d] = %d  ",i,j,arr[i][j]);
        }
        printf("\n");
    }
}

Output:

Learning from this post:
1 ) How to pass 1-D,2-D array to function
2 ) Use of passing an array to function.
3 ) Declaration of passing 1-D,2-D array to function
4 ) Definition of passing 1-D,2-D array to function
5 ) When an index is not necessary for function definition while passing an array
6 ) How to make a function to which we can pass the array

Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments