0

So I have a function which shall return the variable "ergebnis" but only if the operator is "/" it shall also return the rest called "rest". How do I accomplish that?

unsigned int (unsigned int zahl1, unsigned int zahl2, char operat)
{
    unsigned int ergebnis;
    unsigned int rest;
    switch (operat)
    {
    case '+':
        ergebnis = zahl1 + zahl2;
        return ergebnis;
    case '-':
        ergebnis = zahl1 - zahl2;
        return ergebnis;
    case '*':
        ergebnis = zahl1 * zahl2;
        return ergebnis;
    case '/':
        ergebnis = zahl1 / zahl2;
        rest = (zahl1 % zahl2);
        return ergebnis, rest;
    }
}
Felix
  • 56
  • 1
  • 5
  • You'll have to either live with `rest` being 0 in those other cases, or using `std::optional` combined with `std::pair` or `std::tuple` – I3ck Feb 26 '19 at 18:08
  • The "C" way would be to return a struct with both variables and an extra boolean flag to determine if `rest` is active. – meowgoesthedog Feb 26 '19 at 18:08
  • The "Pascal" way would be to use out parameters. (But please don't do that in C++.) – Eljay Feb 26 '19 at 18:11
  • 1
    The return type can't depend on run time values. You'll have to return two values every time or find another solution. – François Andrieux Feb 26 '19 at 18:11

0 Answers0