Feel free to make this post as a duplicate if the question asked already, I haven't found a post same as this
As far as 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.
Here's the code,
#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;
}
Output,
1
2
3
4
5
6
7
8
9
10
Without the last return
, it works just fine too, or am I missing something?