How to write a C Program Function Example in C Programming Language ?
Solution For C Program :
//C Program Function Example.
#include <stdio.h>
int sum(int n); // prototype
int main(){
int a, b;
scanf("%d%d", &a, &b);
//int sumA = sum(a);
/*
int sumA = sum(3);
=> int sumA = 6;
int sumA = sum(3) * 8 + 15;
=> int sumA = 6 * 8 + 15
*/
//int sumB = sum(b);
printf("sumA: %d\n", sum(a));
printf("sumB: %d\n", sum(b));
return 0;
}
int sum(int n){
// (x) Do not function the same name with the variable name: int sum = 0;
int total = 0;
int i;
for( i = 1 ; i <= n ; i++ ){
total += i;
}
return total;
}