Home > OS >  Is comma separated return values necessary in this c extended euclid function?
Is comma separated return values necessary in this c extended euclid function?

Time:12-25

Today as I am revising about the extended euclidean algorithm, and I stumble a code library that does this:

#include <bits/stdc  .h>

using namespace std;
#define rep(i, a, b) for (int i = a; i < (b);   i)
#define all(x) begin(x), end(x)
#define sz(x) (int)(x).size()
typedef long long ll;
typedef pair<int, int> pii;
typedef vector<int> vi;

ll euclid(ll a, ll b, ll &x, ll &y) {
  if (!b) {
    return x = 1, y = 0, a;
  }
  ll d = euclid(b, a % b, y, x);
  return y -= a / b * x, d;
}

I am curious that the return type is a long long, and from my initial search, in a multiple comma return in c , only the right value is kept and returned. So the question is, why does the author of this function still want to x = 1, y = 0 or y -= a / b * x on the left hand side of the same return line?

CodePudding user response:

This return line

return y -= a / b * x, d;

does indeed only return the d via return value, true.
It does however also update the two reference parameters x and y,
which can be seen as part of the value-returning.

It could be done in a preceeding line.
That would avoid the false impression that the author tries to return more than one value, which as you correctly mention, C does not support.

  •  Tags:  
  • c
  • Related