Write a function that returns more than one value at a time.
We can use a structure and pass its variable to a function and can retrieve all its data members.
#include<stdio.h> struct cal { int sum; int sub; int mul; int div; }; void calculation(int,int,struct cal*); int main() { int n1, n2; struct cal ans; printf("Enter two integers : "); scanf("%d %d",&n1,&n2); calculation(n1, n2, &ans); printf("%d + %d = %d\n",n1,n2,ans.sum); printf("%d - %d = %d\n",n1, n2,ans.sub); printf("%d * %d = %d\n",n1, n2,ans.mul); printf("%d / %d = %d\n",n1, n2,ans.div); return 0; } void calculation(int i, int j, struct cal *r) { r->sum = i + j; r->sub = i - j; r->mul = i * j; r->div = i / j; }
Output
Enter two integers : 10 2 10 + 2 = 12 10 – 2 = 8 10 * 2 = 20 10 / 2 = 5