logo

Returning multiple values from a function

#include <stdio.h> #include <stdlib.h> #include <string.h> int sumAndProduct(int a, int b, int* sum, int* prod) { if (sum == NULL || prod == NULL) { return 0; } *sum = a + b; *prod = a * b; return 1; } int main(int argc, char* argv[]) { int x, y, sum, prod; int status = sumAndProduct(x, y, &sum, &prod); if (status == 0) { printf("Something went wrong\n"); return 0; } printf("Sum is %d\n", sum); printf("Product is %d\n", prod); return 0; }

Courses with this lesson