C does not permit you to define a function which returns multiple types or multiple instances of a single type. You can however bundle variables of similar or disparate types together into a new user-defined type using a struct. In this case, you would do something like:
#include <stdio.h>
#include <stdlib.h>
typedef struct myStruct {
int sum;
int product;
} myStruct;
myStruct function(int x, int y) {
myStruct val;
val.sum = x+y;
val.product = x*y;
return val;
}
int main() {
int num1, num2;
myStruct result;
printf("Enter tow numbers: ");
scanf("%d %d", &num1, &num2);
result = function(num1, num2);
printf("Sum is: %d\nProduct is: %d", result.sum, result.product);
return 0;
}
Also, while your program above is syntactically correct, the ,
in the return of your function has the result of only returning the last value in the collection of comma separated values. Normally the ,
isn't used in conjunction with a return
; it can be quite handy when used in a for
loop however.