return integers instead of using void function
+10 −4 · 1 file changed
-
graph/db/codevault/ret_multiple_values_from_func.c+10 −4
@@ -1,7 +1,10 @@ #include <stdio.h> #include <stdlib.h> -void sumAndProduct(int a, int b, int* sum, int* prod){ +int sumAndProduct(int a, int b, int* sum, int* prod){ + if (sum == NULL || prod == NULL){ + return(0); + } *sum = a + b; *prod = a * b; } @@ -10,10 +13,13 @@ void sumAndProduct(int a, int b, int* sum, int* prod){ int main (int argc, char** argv) { int x = 5; int y = 12; - int sum = 0 + int sum = 0; int prod = 0; - sumAndProduct(x, y, &sum, &prod); + int status = sumAndProduct(x, y, &sum, &prod); + if (!status) { + fprintf(stderr, "Something went wrong\n"); + } else { fprintf(stdout,"The sum is %d and the product is %d\n", sum, prod); - + } return EXIT_SUCCESS; }