I'm trying to understand if there is any benefit to returning a const reference. I have a factorial function that normally looks like this:
unsigned long factorial(unsigned long n)
{
return (n == 0) ? 1 : n * factorial(n - 1);
}
I'm assuming that there will be a performance increase when we pass by const reference and we return a const reference... but const-correctness always confuses me.
const unsigned long & factorial(const unsigned long& n)
{
return (n == 0) ? 1 : n * factorial(n - 1);
}
Is it valid to return a const reference? Furthermore, could somebody please tell me: is it beneficial?