I am writing a simple factorial function to practice recursion within c++. I wanted to write it in ternary format but it was not working so I wrote it out first to see if it worked the more traditional way.
Ternary Format:
num > 1 ? num * factorial(num-1) : return 1;
Traditional :
if (num > 1){
return num * factorial(num-1);
}
else {
return 1;
}
I am simply printing the result of factorial(5) which should display 120. While it works for the second it does not for the first. Why can I not use return in the ternary format? Or, am I using it wrong?