Difference in a way of writing a void function return

78 views Asked by At

Here's a code snippet I found here

void App::onSave()
{
    if (filename.isEmpty())
        return onSaveAs();

//do saving here
}

void App::onSaveAs()
{
    QString f = QFileDialog::getSaveFileName(NULL, "Save as", "", "*.sb");
    if (!f.isEmpty())
    {

//filename generation

        return onSave();
    }
}

My question is: is there any difference between return onSave(); , as it is written here, and something like this:

onSave();
return;

It confused me because the functions are of void type and don't return anything.

2

There are 2 answers

0
Michael Burr On BEST ANSWER

The C++ standard says:

A return statement with an expression of type void can be used only in functions with a return type of cv void; the expression is evaluated just before the function returns to its caller.

So the two forms are equivalent in C++.

Note that the C standard says:

A return statement with an expression shall not appear in a function whose return type is void

The relaxation of this restriction in C++ probably has to do with templates.

2
R Sahu On

You asked:

is there any difference between

return onSave();

and

onSave();
return;

There is no difference between the two.