如果问题已经提出,请随时将此帖子重复,我还没有找到与此帖子相同的帖子
I know that there's no need to return
in a void
function, e.g:
void ex () {printf ("Hi\n");}
But, is it fine if there's no return
in a void
recursion? What I'm thinking is, the program will keep calling func (num-1)
until it reaches 0
and it returns so it doesn't print 0
to the output, and I need return
at the end of the function so after the recursion call is done, you go back to the previous func ()
immediate caller.
这是代码,
#include <stdio.h>
void func (int num)
{
if (!num) return;
func (num-1);
printf ("%d\n", num);
return; //Is it necessary to put return here?
}
int main ()
{
func (10);
return 0;
}
输出,
1
2
3
4
5
6
7
8
9
10