0

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?

the_only_sa
  • 165
  • 1
  • 8
  • 1
    To change the values of `x` and `y`. They are passed by reference. – tkausl Dec 24 '22 at 05:34
  • 5
    `x` and `y` are references so that code is making modifications to those variables. Writing it that way just makes it confusing IMO. Of course, any time I see the silly macros that folks like to use in so called competitive programming I know I'm not looking at a work of art. – Retired Ninja Dec 24 '22 at 05:35
  • 2
    The return statement could've been written as two separate statements `y -= a / b * x;` and `return d;`, which is more conventional and readable than trying to fit as many operations into every statement as possible. The macros and typedefs that obfuscates the code is also a sign that the author does not prioritize readability. – kotatsuyaki Dec 24 '22 at 05:42
  • 3
    Not related to the main question, but for people reading the code: [Why should I not #include ?](https://stackoverflow.com/questions/31816095/why-should-i-not-include-bits-stdc-h) [Why is "using namespace std;" considered bad practice?](https://stackoverflow.com/questions/1452721/why-is-using-namespace-std-considered-bad-practice) – kotatsuyaki Dec 24 '22 at 05:45
  • 1
    As the author seems to limit at maximum the number of characters in the code, I would suggest to maximize this and remove all those spaces between characters, and avoid the overlong `ll` type name by using just `l`. – prapin Dec 24 '22 at 08:24
  • *"why does the author of this function still want to [...]?"* -- this is just speculation (so not a real answer), but perhaps the author wanted to confuse someone known as "Sheen An" (along with anyone else attempting to read the code)? – JaMiT Dec 24 '22 at 08:49
  • This seems to be a perfect example of how **not** to write code. Like defining a bunch of macros to make the code shorter (but unreadable), and then not use them. So in fact make the code longer! – BoP Dec 24 '22 at 09:52
  • haha @JaMiT that was a funny comment, but thanks anyway – the_only_sa Dec 25 '22 at 03:44

1 Answers1

1

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.

Yunnosch
  • 26,130
  • 9
  • 42
  • 54