Arrays can be used in function parameters. An array can be passed as argument to a function whose formal parameters are defined as below.
Example:
void func(int *param) {
statement(s);
}
void func(int param[10]) {
statement(s);
}
void func(int param[]) {
statement(s);
}
Example:
#include <stdio.h>
int sum(int arr[], int size) {
int i;
int sum = 0;
for (i = 0; i < size; ++i) {
sum += arr[i];
}
return sum;
}
int main () {
int balance[5] = {1000, 2, 3, 17, 50};
int total;
/* pass pointer to the array as an argument */
total = sum( balance, 5 ) ;
/* output the returned value */
printf( "Sum value is: %d ", total );
return 0;
}
Output of above program,
Sum value is: 1072
Related topics:
Arrays in C | Two Dimensional Array in C | Strings in C | Pointers in C | Returning Array from a Function in C
List of topics: C Programming
No comments:
Post a Comment