4

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?

  • 2
    You need the `return` from the `if` as it is your stop condition. The second `return` you don't need as it does not affect the code. Without `return` the function ends and returns to the caller. – CRM May 17 '20 at 06:33
  • 1
    So the question is, whether it's required to use a `return` statement at the end of void function? No it's not; [here's](https://stackoverflow.com/questions/10079089/implicit-int-return-value-of-c-function) more details on that. – raina77ow May 17 '20 at 06:33
  • 1
    @LastSecond959 There's no difference between recursive and non-recursive function in how their bodies are compiled. Returned type is what gives a function a distinction. – raina77ow May 17 '20 at 06:35
  • 1
    sometimes I work like a compiler and ignore comments specially with no coffee :P anyway, my comments was about removing all returns, its just as mentioned by other comments, its not needed. – ROOT May 17 '20 at 06:39
  • 1
    @ROOT Ahaha, you're not the only one XD. Thanks anyway. – LastSecond959 May 17 '20 at 06:48
  • No `return` statement is required at all; the function can be structured as `{ if (num) { func(num-1); printf("%d\n", num); } }`. – Eric Postpischil May 17 '20 at 10:35

3 Answers3

7

A function with return type void doesn't need an explicit return. Simply reaching the end of the function will properly return.

It is no different for void functions that are also recursive. The first return in your function is required because you want it to return before reaching the end of the function, but the second isn't required.

ikegami
  • 367,544
  • 15
  • 269
  • 518
  • So you mean even it is a recursion, it will implicitly return to its immediate caller? While the `return` at `if` is needed to prevent infinite loop right? Am I correct? – LastSecond959 May 17 '20 at 06:43
  • 1
    Yes. . . . . . . . – ikegami May 17 '20 at 06:43
  • 1
    Just to be clear, even the explicit `return` will only return to its immediate caller. – ikegami May 17 '20 at 06:44
  • 1
    No `return` statement is required at all; the function can be structured as `{ if (num) { func(num-1); printf("%d\n", num); } }`. – Eric Postpischil May 17 '20 at 09:52
  • @EricPostpischil Please read again. I said it was required to return before reaching then end of the function. – ikegami May 17 '20 at 09:53
  • 1
    @Eric Postpischil, Yeah, that's kinda my point. It stands on its own, but one expects comments to provide feedback about the answer on which they are posted. It's just so random and pointless. At best, it's redundant since I already mentioned that reaching the end of the function doesn't require a `return`. I don't get it. – ikegami May 17 '20 at 10:47
2

A function returning void doesn't have to explicitly return. A program has 1 entry point, but might have multiple exit points. Consider the following example:

void print(int var)
{
    if (var)
       return; // first exit point
    /*
     *   do stuff
     */  
    // second exit point
}

It has 2 exit points. Second one is without a return;.

Tony Tannous
  • 14,154
  • 10
  • 50
  • 86
-2

try this :

#include <stdio.h>

void func (int num)
{
    if (!num){ 
     printf("0\n");//print 0 before you stop
     return;
   }
    func (num-1);
    printf ("%d\n", num);


}

int main ()
{
    func (10);

    return 0;
}
Escanor
  • 95
  • 3
  • 9