user2884789
user2884789

Reputation: 533

return; only with no return value

What does it mean when someone codes:

 template <class T>
 void binaryTree<T>::in_order(Node <T>* node, void (*fun)(T&))
{
    if (node == NULL)   
            return;     //<-- what does this mean here

    inorder(node->left, fun);   //<-- how does this continue here
    f(node->data);
    inorder(node->right,fun);
}

The question is how do you have a return; without returning anything. what does this do? **Note, I have edited the code above for clarity.

Upvotes: 0

Views: 129

Answers (7)

Abhineet
Abhineet

Reputation: 5389

If you are used to see the return at last of the code, you can rewrite the code as::

void foo( int x, int y )
{
    if( x != 1 )
    {
        foo( y ) ;
    }
    else /* You can keep it to understand better visually. 
            Even if you don't keep it, the code would do the same */
    {
        return ; 
        /* returns the control to the caller code. 
           Make sure you understand that, it just returns the control but 
           without any value i.e., returning void. */
    }
}

EDIT:: return just returns the control to the caller. If you want to return the control with some value, you would return variable_name and if you don't want to return anything, just return.

Real-Life Example:: Assume you have been asked to buy some soaps for bath. Now, if soaps are available at the store, you would come back and return me the soaps and caller will go to bath. This is you are returning back to the caller with some value. But suppose, if the store is out of soaps, then you would just simply return back to me and I will go to bath without any soap. This is you are returning back to the caller but without a value. In both the cases, you would return to the caller cuz the caller is waiting to proceed further with bathing only on your return.

Upvotes: 0

Aragon
Aragon

Reputation: 1551

When a function return type is void, You can return from the function by writing return only. no need to give any value after return. If return type would have been int or char etc. then it is compulsory to give a value after return.

Upvotes: 0

axon
axon

Reputation: 1200

foo is a function.

foo takes 2 parameters. Both parameters are integer. The parameters are named x and y.

The body of the function tests whether the value stored in the x variable is equal to 1. If the value stored in x equals 1, then the function will return (exit). If the value stored in x isn't equal to 1, then the function calls foo(int x).

The foo called inside the function may be different to the foo function that you have supplied the code for (because it has a different interface/prototype).

What is the body of the second foo (the one that takes a single parameter)? It is possible that the prototype of foo specifies a default value for the parameters x & y (if this is the case then this may be the only foo function.

Upvotes: 1

Maroun
Maroun

Reputation: 95958

It means that foo(y) is not executed when x == 1, you return from the function.

The code continues when x != 1, if you indent your code and put brackets in the if it'll be clearer:

void foo(int x, int y) { 
   if(x == 1) {
      return;    //return from the function, don't proceed to foo(y)
   }
   foo(y);   
}

A return statement with neither an expression nor a braced-init-list can be used only in functions that do not return a value, that is, a function with the return type void.

See this code on Ideone.

Upvotes: 1

Imtiaz Emu
Imtiaz Emu

Reputation: 489

it means if x == 1 then the function return without executing foo(y) function, if x != 1 , then the foo(y) function executes.

Upvotes: 1

NPE
NPE

Reputation: 500317

Despite the confusing indentation, the return is inside the conditional statement.

The following is a slightly less confusing way to write that code:

if (x == 1)
   return;
foo (y);

Being inside the conditional statement, return is only executed if x == 1. In all cases, foo(y) is called.

Upvotes: 0

Some programmer dude
Some programmer dude

Reputation: 409166

If you format it better it might be easier to see:

void foo (int x, int y)
{ 
    if (x == 1)
        return;
    foo (y);
}

It simply returns if the variable x is equal to 1. If x is not equal to 1 then the code skips to the recursive call to foo (and I really hope the real code is not like that, because you're calling foo with one argument when it wants two).

Upvotes: 1

Related Questions