This is a recursive function I did to find out if the given digits in a number are in a decreasing order, I'm sure my base is correct as I see the function does return false for the first few digits, but because the last two are in a decreasing order the function returns true in the end.
I can't figure out how to keep that false value and return it back to the original call for the function.
#include<iostream>
using namespace std;
bool dec(int n);
void main()
{
cout << dec(1231);
}
bool dec(int n)
{
bool res;
if (n < 10)
return true;
else
{
res = dec(n / 10);
if ((n / 10) % 10 > n % 10)
return true;
else
return false;
}
}
resat all. You're just assigning to it.if (x) { return true; } else { return false; }is kind of an anti-pattern. Why not justreturn x;?return res.return res, but the whole expression. For example, as per @b4hand's answer:return res && (n / 10) % 10 > n % 10;ifelse.