#include <stdio>
using std::cout;
void CountDown(int N) {
if(N == 0) {
return;
}
cout << N;
CountDown(N-1);
//return;
}
In the code the output I get when return is commented is same as when not commented.
What I want to ask is whether it makes a difference if I use a return; statement at the end of the function (since it would implicitly return to the function, which called it, at the end of the braces)?
Another question:
What if had a function with a return type instead of void here.
I tried it with a function. The value was wrong.But there was no compiler error.
So using simply a return; makes no difference right? Except when I want to prematurely end the function right?
For void return functions, no it doesn't make a difference. However, if the function is expected to return anything but void, then you'll need to include a valid return for all code paths.
At the end of a void function control is automatically returned to the calling function so you do not need a return; at the end. return in a void function is used to exit the function before it normally would like in your if statement.
If you have a function that is supposed to return a value and does not then that is undefined behavior.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With