array in function c++ code example
Example 1: how to pass an array to a function
#include <stdio.h>
float calculateSum(float age[]);
int main() {
float result, age[] = {23.4, 55, 22.6, 3, 40.5, 18};
result = calculateSum(age);
printf("Result = %.2f", result);
return 0;
}
float calculateSum(float age[]) {
float sum = 0.0;
for (int i = 0; i < 6; ++i) {
sum += age[i];
}
return sum;
}
Example 2: array syntax in c++
int foo [] = { 16, 2, 77, 40, 12071 };
Example 3: array as parameter c++
void myFunction(int param[]) {
.
.
.
}
Example 4: array declaration c++
int foo [5];
Example 5: array syntax in c++
int bar [5] = { 10, 20, 30 };
Example 6: arrays in c++
int baz [5] = { };