We will write a program for passing array to function in C language. This will help to understand the basic structure of programming. In this program, we will learn how to pass simple and multi-dimensional arrays to function with examples.
Pass Individual Array Elements :
In the program, we will learn how to pass individual array elements to a function. We have created a display function which takes two array elements as arguments.
In the program, we will learn how to pass the whole array to function in C. We have created a calculateSum function which takes a whole array as argument.
#include<stdio.h>
float calculateSum(float num[]);
int main() {
float result, num[] = {23.4, 55, 22.6, 3, 40.5, 18};
result = calculateSum(num);
printf("Result = %.2f", result);
return 0;
}
float calculateSum(float num[]) {
float sum = 0.0;
for (int i = 0; i < 6; ++i) {
sum += num[i];
}
return sum;
}
Output
Result = 162.50
Pass Multi-dimensional Arrays to Functions :
In the program, we will learn how to pass a multi-dimensional array to function in C. We have created a function named displayNumbers which takes a 2-D array as its argument.
Login/Signup to comment