Passing Array to function

PASSING ARRAY TO FUNCTION:

                                                 The array can be able to pass as an argument to the function. Like a basic primitive normal variable, it is possible to pass the values of an entire array to a function. To pass a one-dimensional array to function, it is sufficient to mention the name of the array without any subscript and size of the array as the next argument.

syntax :

                                    function_name(array_name,size_of_array);


example,

#include<stdio.h>

#include<conio.h>

void add(int a[],int n);

 

int main(){

     int a[5],n,i;

     printf("enter the values for array size\n");

     scanf("%d",&n);

     printf("give the values for your assigend array:\n");

     for(i=0;i<=n;i++){

          scanf("%d",&a[i]);

     }

     add(a,n);

             return 0;

}

void add(int a[],int n){

     int sum=0,i;

     for(i=0;i<=n;i++){

          sum=sum+a[i];

     }

     printf("The sum of an array is:%d \n",sum);

    

     }



 online compiler


In the above example, we have an array function for doing summation operations. In this operation, we are going to add =all the elements one by one by using summation operators.  Really how it  is working means, In the above program, you can see, 
                                          void add(int a[],int n); 


 In this function,  we have declared one is an array and another one is the size of an array. In int main we have got the values for array element, and finally, we called the function to add(arrray_name,array_size).  once we called the function that function goes to the function definition and it doing the operation that we have given inside of the function. here, we only gave addition. so it will do some operation and finally, it gives the output.  

No comments:

Post a Comment