Search This Blog

Translate

C program example to pass array to the function

Example of Function With array how to write c array program using function, how to pass array to function, c program to pass entire array to function, different methods to pass array to function, function using array example with source code,Write simple program to print array element using function

1. Write simple program to print array element using function

     There are different method to write this program in this page i have mention some of that

      following methods are used 

      1. Passing array element to function

       2. passing Entire array to function without size

       3.passing Entire array to function with size

     lets see example of that .

   

1. C Program to Explain Passing array element to function

  #include<stdio.h>
  #include<conio.h>
  void arraypr(int);
 void main()
{ int a[10];
   int i;
   printf("Enter arry Element");
   
  for(i=0;i<5;i++)
      scanf("%d",&a[i]);
   for(i=0;i<5;i++)
      arraypr(a[i]);

   getch();
  }
void arraypr(int n)
{
  printf("\n %d",n);
}

Above program accept array element after accepting  it pass element one by one to  function void arraypr(int n) where we are going to print the array element 

Output of the program is 


       2. passing Entire array to function without size


#include<stdio.h>
#include<conio.h>
void arraypr(int a[]); // function declaration

void main()
{ int a[10];
  int i;
  printf("Enter arry Element");
  
for(i=0;i<5;i++)
   scanf("%d",&a[i]);

   arraypr(a);   // passing name of array not size in function call

   getch();
  }

void arraypr(int n[]) //function Defination
{
  int i;
 for(i=0;i<5;i++)
  printf("\n %d",n[i]);

}

Above program accept array element after accepting in main function   we are going to pass entire array to function  void arraypr(int n[]) where we are going to print it.

Output of the program is

 

 

       3.passing Entire array to function with size

 #include<stdio.h>
#include<conio.h>
void arraypr(int a[], int); //function declaration

void main()
{ int a[10];
  int i,sz;
  clrscr();

  printf("\n\n \tEnter how many element :");
  scanf("%d",&sz);

  printf("Enter arry Element");
    for(i=0;i<sz;i++)
  scanf("%d",&a[i]);

   arraypr(a,sz);    //call to function   // name of arry & size as second parameter
   getch();
  }
void arraypr(int n[], int s)  //function Definition
{  
 int i;
 for(i=0;i<s;i++)
    printf("\n %d",n[i]);

}

Above program accept array element after accepting in main function   we are going to pass entire array and size of array to function  void arraypr(int a[], int) where we are going to print it.

 Output of the program is 

 

 

 

C Program example List

Google+ Badge