In this tutorial you will learn about the C Program to Pass Array to Function and its application with practical example.
C Program to Pass Array to Function
In this tutorial, we will learn to create a C program that will Pass Array to Function in C programming.
Prerequisites
Before starting with this tutorial we assume that you are best aware of the following C programming topics:
- Operators in C Programming.
- Basic Input and Output function in C Programming.
- Basic C programming.
- For loop in c programming.
- String functions of c programming.
Program to Pass Array to Function
As we all know the array is a collection of similar data types. In an array, only one variable is declared which can store multiple values. First, we will declare the input array for the program.
Then we will pass that array to a function for the calculation.
Then we will add the elements of using the for a loop.
With the help of this program, we can Pass Array to Function.
Algorithm:-
1 2 3 4 5 6 7 8 9 10 11 |
1. Declare the variables for the program. 2. Give the input array for the program. 3. Pass that array to a user-defined function. 4. Adding that array in function using for loop. 5. Print the Result. 6. End the program. |
Program to Pass Array to Function:-
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 |
/* Program to adding the array elements by passing to a function */ #include <stdio.h> /* user defined function for the addtion of the array */ float Sumarry(float no[]); int main() { //declaring the variables for the program float result, no[] = {23.4, 55, 22.6, 3, 40.5, 18}; // no array is passed to Sumarry() result = Sumarry(no); //printing the result printf("Result = %.2f", result); return 0; } //body of the user defined function float Sumarry(float no[]) { float sum = 0.0; for (int i = 0; i < 6; ++i) { sum += no[i]; } //returning the sum of array from the function return sum; } |
Output:-
In the above program, we have first initialized the required variable.
- no[] = it will hold the float value array.
- result = it will hold the float value.
- sum = it will hold the float value.
- i = it will hold the integer value for the loop control.
User-defined function declaration for the addition of array.
Printing the output.