tags:

views:

335

answers:

8

This is a quick question, I did a search but couldn't find anything that answered my question.

When doing a recursive function in C do you need to have a return even when using a void function?

Eg:

    void addToLL(structA_ptr new, structA_ptr cur) {
        if (cur->next == NULL) {
             cur->next = new;
        } else {
          addToLL(new, cur->next);
        }
    }

Would I need to put a return keyword before the call to the function? I know that if the function would return something, like searching for something in the LL it would need a return statement.

+9  A: 

No, you don't need it.

Long answer: Your recursive function is executed like any other and if doesn't encounter a recursive call, it is simply terminated. You don't need an explicit return. You can use if you want to exit the function prematurely.

Dario
+5  A: 

There's no need for a return statement in this case, but only because the function doesn't do anything after the recursive call.

If you'd had this:

void addToLL(structA_ptr new, structA_ptr cur) {
    if (cur->next == NULL) {
         cur->next = new;
    } else {
         addToLL(new, cur->next);
    }

    someOtherCode();
}

then you'd need to insert a return; statement after the call to addToLL() if you didn't want someOtherCode() to be called after addToLL() returned.

RichieHindle
"The function doesn't do anything after the recursive call"? Not on the way down maybe, but how about on the way up?
Tony van der Peet
A: 

Returning values from functions isn't a prerequisite for recursion - in any language. So, no.

Wim Hollebrandse
A: 

It's more of a style issue. Some people consider ending a void function without a return keyword a faux-pas, others don't care. This really isn't recursion specific, though.

patros
A: 

When your function ends, it implicitly returns, so there is no need to call the return instruction.

[Value Add] When functions are added returned in a program, the functions local variables and arguments are removed and return values are made available to the calling function, and execution resumes at the calling position.
When "void" functions are returned , they simply clean up arguments and local variables, and don't need to worry about moving return values to the right place.

chustar
A: 

There is no need for a return type.

A recursive function is still useful without a return type because it could for example print values, calculate things by pointer parameter, or do a variety of other things.

Brian R. Bondy
A: 

I believe your example code is a good example of a recursive function than can easily be converted to a loop making less strain on the stack when operating on a very long linked-list.

void addToLL(structA_ptr new, structA_ptr cur) {
    while (cur->next)
        cur = cur->next;
    cur->next = new;
}
epatel
A: 

In a function with return type of void the only reason to use an explicit return statement would be if you wanted to exit function prematurely.

AndreyT