-5
#include <bits/stdc++.h>
using namespace std;
 

int nearestFibonacci(int num)
{
    
    if (num == 0) {
        cout << 0;
        return;
    }

    int first = 0, second = 1;
    int third = first + second;

    while (third <= num) {
        first = second;
        second = third;
        third = first + second;
    }
    int ans = (abs(third - num)
               >= abs(second - num))
                  ? second
                  : third;
    
    return ans;
}
 
int main()
{   int a = 0;
    int N = 20;
    a = nearestFibonacci(N);
    cout << a;
 
    return 0;
}

I have a code like this, however I got this error:

test.cpp: In function 'int nearestFibonacci(int)':

test.cpp:10:9: error: return-statement with no value, in function returning 'int' [-fpermissive] return; ^~~~~~

I can not return the value ans. Can someone explain and guide me how to return value in the right way.

Thank you so much.

  • Your problem is the return with no value when `num == 0` at the top of your function. – Chris Uzdavinis Aug 14 '21 at 05:12
  • The error message includes the line of code. It's talking about the `return;` line, not `return ans;`. – chris Aug 14 '21 at 05:16
  • `test.cpp:10:9` points at 10th line, 9th character, where you have just "return". The output there is superfluos. Aslo Fibonacci's closest number to 0 is 1. :P – Swift - Friday Pie Aug 14 '21 at 05:19
  • 1
    [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) – Evg Aug 14 '21 at 05:56

2 Answers2

2

The function int nearestFibonacci(int num) is expecting an int value when returning. When you type return; your returning without a value. A simple fix would be to just return 0; in the num == 0 case. Here is the working code.

#include <bits/stdc++.h>
using namespace std;
 

int nearestFibonacci(int num)
{
    
    if (num == 0) {
        return 0; // Changed it to return 0 when num == 0
    }

    int first = 0, second = 1;
    int third = first + second;

    while (third <= num) {
        first = second;
        second = third;
        third = first + second;
    }
    int ans = (abs(third - num)
               >= abs(second - num))
                  ? second
                  : third;
    
    return ans;
}
 
int main()
{   int a = 0;
    int N = 20;
    a = nearestFibonacci(N);
    cout << a;
 
    return 0;
}
Aswin Murali
  • 61
  • 1
  • 3
  • 6
  • 1
    [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) – Evg Aug 14 '21 at 05:55
  • @Evg I 100% agree with your comment. Thanks for pointing it out. – Aswin Murali Aug 14 '21 at 06:19
  • Thank you very much. I am really appreciate your guiding. Thank you so much. Hope many happy return to you – Lộc Nguyễn Thế Aug 14 '21 at 13:15
  • @LộcNguyễnThế Glad, I could help. If this answer is helpful. I would appreciate it if you could mark the answer as accepted. – Aswin Murali Aug 16 '21 at 12:55
1

You need to return an integer value in all paths inside your function nearestFibonacci() because this function is expected to always return an integer.

Your original code does not return an integer for the case num == 0 inside the first if statement at the top of that function.

To fix the error, you simply need to return 0 for the case num == 0 as shown in the code below

#include <cmath>
#include <iostream>
 

int nearestFibonacci(int num)
{
    
    if (num == 0) {
        return 0;    // You should return 0 here.
    }

    int first = 0, second = 1;
    int third = first + second;

    while (third <= num) {
        first = second;
        second = third;
        third = first + second;
    }
    int ans = (abs(third - num)
               >= abs(second - num))
                  ? second
                  : third;
    
    return ans;
}
 
int main()
{   int a = 0;
    int N = 20;
    a = nearestFibonacci(N);
    std::cout << a;
 
    return 0;
}
Job_September_2020
  • 858
  • 2
  • 10
  • 25
  • 2
    [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) – Evg Aug 14 '21 at 05:55
  • Good point. I do not use those in my own code. I only re-use them from the OP's code for a quick answer to his question. :-) – Job_September_2020 Aug 14 '21 at 08:06
  • But I guess nothing stops you from removing it instead of proliferating that bad practice, right? :) – Evg Aug 14 '21 at 08:08
  • thank you so much. Now I got the problem. Because I am a new programing learner so I usually trend to get stupid errors. But I will try my best to improve. Thank you very much – Lộc Nguyễn Thế Aug 14 '21 at 13:13
  • @Evg, How do the header files look now ? – Job_September_2020 Aug 14 '21 at 18:06
  • Much better. However, this code won't compile due to missing `std::` qualification. – Evg Aug 14 '21 at 19:51
  • @Evg, Oh, I had that ```std::cout<<``` in my code, but I forgot to copy that to the answer. It compiles now. – Job_September_2020 Aug 14 '21 at 20:59