Array with function


The array can be passed to functions using two methods:-
1) Passing individual elements of array.
2) Passing the whole array.

Passing individual Elements of the 1 D Array to function


All array elements can be passed as individual elements to the function like any other variable.
#include<stdio.h>
#include
<conio.h>
  void display(int,int);
  void main()
 {
   int arr[]={4, 5, 6, 3, 2};
    //passing second and third elements to the function //
   display(arr[1], arr[2]);
 }
   void display(int x,int y)
 {
   printf(“%d%d”,x,y);
 }

Passing 1D array to function.

#include<stdio.h>
#include<conio.h>
 int sum(int arr[]);
 void main()
  {
   int r, arr[]={4, 5, 6, 3, 2};
    // passing array to the function
   r=sum(arr);
   printf(“%d”,r);
  }
   int sum(int array[])
  {
   for(i=0;i<=4;i++)
  {
   s=s+array[i];
  }
   return s;
  }

Passing 2D array to function

#include<stdio.h>
#include<conio.h>
int display(int arr[] []);
 void main()
  {
   int arr[2][2];
   for(i=0;i<=1;i++)
  {
   for(j=0;j<=1;j++)
  {
   scanf(“%d”,&arr[i][j]);
  }
  }
   display(arr);
  }
   void display(int arr[2][2])
  {
   for(i=0;i<=1;i++)
  {
   for(j=0;j<=1;j++)
  {
   printf(“%d”,arr[i][j]);
  }
   printf(“\n”);
  }
  }