I am missing very minor thing here i could not figure it out, i am just try to get the factorial of the number inputted, using a recursive call by reference concept in C++. Please let me know what am i missing. Declaring few additional variables for more further alterations in programs.
#include <iostream>
using namespace std;
int factorial(int &m);
int factorial(int &m)
{
int i,result=0;
if(m>=1)
return m * factorial(m-1);
else
return 0;
}
int main(){
int m,i,factorial1=1;
cout<<"Please enter the number to factor "<<std::endl;
cin>>m;
factorial1=factorial(m);
cout<<"Factorial value is " << factorial1<<std::endl;
return 0;
}
Getting error as below
factorial_func.cc: In function ‘int factorial(int&)’:
factorial_func.cc:33:25: error: cannot bind non-const lvalue reference of type ‘int&’ to an rvalue of type ‘int’
33 | return m * factorial((m-1));
| ~~^~~
factorial_func.cc:27:20: note: initializing argument 1 of ‘int factorial(int&)’
27 | int factorial(int &m)
| ~~~~~^