0

I am found below example and I can't understand the calculation of the return statement (return (kk, ll);).

Is there anybody who explain to me about the calculation of the return statement?

#include<stdio.h>

int addmult(int ii, int jj)
{
    int kk, ll;
    kk = ii + jj;
    ll = ii * jj;
    return (kk, ll);
}

int main()
{
    int i=3, j=4, k, l;
    k = addmult(i, j);
    l = addmult(i, j);
    printf("%d %d\n", k, l);
    return 0;
}
UNV
  • 33
  • 6

1 Answers1

3

Whoever wrote this seems to be under the impression that C allows returning multiple values in a list like Python does, or that this syntax dictates the values that get returns on the first and second calls to the function. That is not the case.

What you see here is an example of the comma operator. The left operand is evaluated and its value discarded, then the right operand is evaluated and its value becomes the value of the expression.

So the effect of this:

return (kk, ll);

Is to return the value of ll.

dbush
  • 205,898
  • 23
  • 218
  • 273