0

I want to return two float variables from a bool function althought i dont know how to do it. What should i write in main? Here is my code.

bool triwnymo(int a, int b, int c, float& x1, float& x2){
    
    int d;
    d=diak(a,b,c);
    if(d>0){
        x1=(-b+sqrt(d))/(2*a);
        x2=(-b-sqrt(d))/(2*a);
        return x1,x2;
        return true;
    }else if(d==0){
        x1=-b/(2*a);
        x2=x1;
        return x1,x2;
        return true;
    }else{
        return false;
    }
}
  • You have a few options - maybe this answers your question: https://stackoverflow.com/questions/321068/returning-multiple-values-from-a-c-function ? – codeling Dec 17 '21 at 13:06
  • This is maybe interesting for you: https://isocpp.github.io/CppCoreGuidelines/CppCoreGuidelines#Rf-out-multi – Dundo Dec 17 '21 at 13:07
  • 2
    Are you sure you want to _return_ these values? From the declaration it looks like you want to return one `bool` and overwrite the values of `x1` and `x2`. I mean, why would you pass them by non-const reference if not to use them as out-parameters? – Lukas-T Dec 17 '21 at 13:08
  • When the function returns, the values are in the variables you pass as `x1` and `x2`. (I suspect that you were given the prototype by somebody and didn't think the reference parameters were significant.) – molbdnilo Dec 17 '21 at 13:09
  • TLDR: `std::tuple` – OrenIshShalom Dec 17 '21 at 13:20
  • A `bool` function returns `bool`; it cannot return floating point values. – Pete Becker Dec 17 '21 at 13:32
  • 1
    The statement `return x1,x2;` won't do what you want. Instead, it will ignore `x1` and return what you'd get if `x2` were converted to `bool`. – jjramsey Dec 17 '21 at 13:43
  • Thank you all for the answers – Dimitris Malkotsis Dec 17 '21 at 13:55
  • @AwesomeJSF A pair of two bool doesn't seem suited to return two floats ;) Much less to return two floats and a bool as OP want's/does. – Lukas-T Dec 17 '21 at 15:05
  • Oh yes, my big mistake ;( tk you – Awesome JSF Dec 17 '21 at 15:11

2 Answers2

3

You can use a custom data structure to return as many values you like:

struct triwnymo_result_type {
     float root1 = 0.0;
     float root2 = 0.0;
     bool has_solution = false;
};

triwnymo_result_type triwnymo(int a, int b, int c){        
    int d = diak(a,b,c);
    if(d>0) {
        return  {(-b+sqrt(d))/(2*a), (-b-sqrt(d))/(2*a), true};
    } else if(d==0) {
        int x=-b/(2*a);
        return {x,x,true};
    } else {
        return {0.0,0.0,false};
    }
}

Note that return x1,x2; is using the comma operator. The comma operator evaluates both operands, discards the first and results in the value of the latter. Thats not what you want. Also return true; after you already returned from the function is never reached. You can only return from a function once.

I didn't want to change too much, but you don't really need to distinguish between d>0 and d==0 because when d==0 then -b+sqrt(d) == -b-sqrt(d) == -b.

463035818_is_not_an_ai
  • 109,796
  • 11
  • 89
  • 185
  • 1
    I am in favor of using this solution specially if you give the members in the struct good names it keeps code clean and readable (pair and tuple don't do that for me). In this case I would call the members : root_1, root_2 and has_solution though – Pepijn Kramer Dec 17 '21 at 13:47
0
#include <tuple>
// bad answer.
//// by pair
//std::pair<bool, float> triwnymo(...) {...}

// 1. by tuple
std::tuple<bool, float, float> triwnymo(...) {...}

// 2. by class or struct
struct data { bool b; float f1; float f2; };
data triwnymo(...) {...}

// 3. by reference or pointer
bool triwnymo(..., float& float_out1, float& float_out2) {...}

// other way
float f1, f2;
bool triwnymo(...) {...; f1 = ?; f2 = ?; }
김범무
  • 103
  • 6
  • That code returns *one* floating-point value. The question asked about returning *two* floating-point values. That rules out using a `std::pair`, if the `bool` value is to be returned as well. – jjramsey Dec 17 '21 at 13:29