初学递归错误,想不通哪地方有问题,
#include<iostream>
using namespace std;
void a (int );
//(n-1)n递归
int main()
{ int n;
cin>>n;
a(n);
}//4 3 2 1 0
void a ( int n)
{ int b;
if(n==0)
{ b=1;
}
else
{
b=a(n-1)*n;//提示这地方有错
return b;
}
cout << b<< endl;
}
//以下是错误信息
#1
Code
Start
prog.cc: In function 'void a(int)':
prog.cc:19:17: error: invalid operands of types 'void' and 'int' to binary 'operator*'
b=a(n-1)*n;
~~~~~~^~
prog.cc:20:16: error: return-statement with a value, in function returning 'void' [-fpermissive]
return b ;
^