Anitej Rao
Anitej Rao

Reputation: 25

Returning in double functions

double interpolation(int input, vector<int>&a, vector<double>&b)
{
    for(int i=0;i<a.size();++i)
    {
        if(input==a.at(i))
            return b.at(i);
    }

    for(int i=0;i<a.size()-1;++i)
    {
        if(input>a.at(i)&&input<a.at(i+1))
        {
            int low=i;
            int high=i+1;
            double m= b.at(low);
            int n= input-a.at(low);

            int p= a.at(high) - a.at(low);
            double q= b.at(high) - b.at(low);

            double fp = (m+n) / (p*q);
            return fp;
        }
    }        
}

I keep getting a warning: control reaches end of non-void function [-Wreturn-type] } message.

I know it has something to with the way I'm returning a value to the function. The purpose of the function is to calculate f(b) = f(a) + (b - a)/(c - a)(f(c) - f(a)). It's hard explaining what the equation is, but yeah help with the return would do the trick I think.

Upvotes: 1

Views: 130

Answers (5)

tniles
tniles

Reputation: 327

The warning message (control reaches end of non-void function) is exactly correct: there is at least one path in your executable code where there is no return statement.

Some will argue you should have a single return in your function. That is one way to do it.

Another way to do it is to add a "default" return statement, which would mean placing a return at the very end of the function (i.e. on the line just before the last }). A best practice would be to make it a typical error code or a predetermined code, via a #define to make it more readable, e.g.

#define RET_ERR_VAL -1.0
    return RET_ERR_VAL;
}

Considering the mathematical operation the function is performing (interpolation), it may make sense to use a default value whose absolute value is very large, e.g. -INF or something like -9999999999.0 so it is obvious (if graphed) that something went wrong. However, an error value may still be more appropriate in order for the calling code to deal with the error, rather than purportedly provide a real answer.

Upvotes: 1

kgorham
kgorham

Reputation: 54

You are getting a warning because the code does not return a value when the size of the 'a' vector is zero. It probably should return zero in that event, unless an error code is more appropriate.

Upvotes: 2

PaulMcKenzie
PaulMcKenzie

Reputation: 35440

Your return statements are in conditional if blocks. What if none of those if statements are true? You need to provide a return value that is not conditional, i.e. at the end of your function.

Also, one good reason why the compiler would give such a warning is that exiting a function that is supposed to return a value but doesn't, leads to undefined behavior.

Upvotes: 1

anycmon
anycmon

Reputation: 23

Not all execution path of function interpolation returns a value. You can provide quick fix, append return 0 at the end but I don't know it is appropriate behavior for this piece of code.

Upvotes: 0

Hatted Rooster
Hatted Rooster

Reputation: 36483

Because your two return statements are encased in if statements the if statements might not evaluate to true and thus the two returns might not be reached and the compiler doesn't know what to return in that case. You could add a default return value :

double interpolation(int input, vector<int>&a, vector<double>&b)
{
for(int i=0;i<a.size();++i)
{
    if(input==a.at(i))
    return b.at(i);

}

for(int i=0;i<a.size()-1;++i)
{
    if(input>a.at(i)&&input<a.at(i+1))
    {
    int low=i;
    int high=i+1;



    double m= b.at(low);
    int n= input-a.at(low);

    int p= a.at(high) - a.at(low);
    double q= b.at(high) - b.at(low);

    double fp = (m+n) / (p*q);
    return fp;
    }
}

return 0.0;

}

Upvotes: 0

Related Questions