Q:

C program to find sum of the array elements (pass an integer array to a function and return the sum)

Given an array of integers (one dimensional array) and we have to find sum of all elements using user define function in C.

Here, we will pass array to the function and function will return the sum of the elements.

Here is the function that we have used in the program,

`int sum_of_elements(int *arr , int n)`

Program to find the sum of all array elements using function in C

``````/*
* C program to a pass integer array elements to
* a function and return the sum of all elements
*/

#include <stdio.h>

// function to calculate the sum of array
// elements
int sum_of_elements(int *arr , int n)
{
int i=0,sum=0;

for(i=0; i<n ; i++)
{
sum = sum + arr[i];
}

return sum;
}

// main function
int main()
{
int total = 0;
int array[10] = {1,2,3,4,5,6,7,8,9};

// Passing array to Function
total = sum_of_elements(array,9);

printf("\nThe sum of all array elements is : %d",total);

return 0;
}``````

Output

`The sum of all array elements is : 45`