Pass Arrays To A Function in C
Pass Arrays To A Function in C
com
4-5 minutes
#include <stdio.h>
void display(int age1, int age2) {
printf("%d\n", age1);
printf("%d\n", age2);
}
int main() {
int ageArray[] = {2, 8, 4, 12};
Output
8
4
Here, we have passed array parameters to the display()
function in the same way we pass variables to a function.
#include <stdio.h>
float calculateSum(float num[]);
int main() {
float result, num[] = {23.4, 55, 22.6, 3, 40.5,
18};
return sum;
}
Output
Result = 162.50
result = calculateSum(num);
#include <stdio.h>
void displayNumbers(int num[2][2]);
int main() {
int num[2][2];
printf("Enter 4 numbers:\n");
for (int i = 0; i < 2; ++i) {
for (int j = 0; j < 2; ++j) {
scanf("%d", &num[i][j]);
}
}
return 0;
}
Output
Enter 4 numbers:
2
3
4
5
Displaying:
2
3
4
5
// function prototype
void displayNumbers(int num[2][2]);
For example,