#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.