Can I return in void function?
Yes, you can return from a void function.
Interestingly, you can also return void from a void function. For example:
void foo()
{
return void();
}
As expected, this is the same as a plain return;
. It may seem esoteric, but the reason is for template consistency:
template<class T>
T default_value()
{
return T();
}
Here, default_value
returns a default-constructed object of type T, and because of the ability to return void
, it works even when T
= void
.
Returning from a void function
Neither is more correct, so take your pick. The empty return;
statement is provided to allow a return in a void
function from somewhere other than the end. No other reason I believe.
return statement in void function in C
Of course you can use return;
in a void
function. How else would you return early from such a function?
In your case the statement is redundant as it's reached on all control paths, but a compiler will certainly not issue a diagnostic in that instance.
(Note that you must return
explicitly from a non-void
function, with the exception of main()
, where a compiler must generate return 0;
if missing.
Using return 0; in void function is possible or not?
You did get a warning, which you chose to ignore. Codeblocks default installation is a gcc/mingw one, which does give the following warning:
warning: 'return' with a value, in function returning void
If you compile with -pedantic-errors
, it will turn into an error.
The recommended setting for beginners is to go Settings -> Compiler, check the following options:
- Enable all common compiler warnings (-Wall)
- Enable extra compiler warnings (-Wextra)
- Treat as errors the warnings demanded by strict ISO C (-pedantic-errors)
Preferably you should also add an option -std=c11
which I don't think exists by default in Codeblocks.
Return statement that calls a function that returns void in a function that returns void
According to CPP Reference, when calling return expression;
:
The expression is optional in functions whose return type is (possibly cv-qualified) void, and disallowed in constructors and in destructors.
Later, they note:
In a function returning void, the return statement with expression can be used, if the expression type is void.
return
can have an expression for a void function as long as the expression is also void, which is what you've done. This could be useful in templates, where the return type of the function may be unknown at the time of writing your function.
How do you exit from a void function in C++?
Use a return statement!
return;
or
if (condition) return;
You don't need to (and can't) specify any values, if your method returns void
.
Returning a value from a void function
Yes, your idea is wrong. A void
function cannot return anything. The reason you can return a void *
is because, as @JohnBode mentions in the comments, "the language standard specifies that a pointer to void
may be converted to or from any object pointer type (6.3.2.3/1)". The value of the pointer is pushed on to the stack and then popped off when you return. The result can then be cast to a pointer of any type. With a void
function, the compiler doesn't expect to have to pop anything off the stack so it doesn't.
Changing Void function to Return Value function
After checking, I update my post.
try this:
void trackVar(double& x, double& y)
{
double z;
z = floor(x) + ceil(y);
x = x + z;
y = y - z;
}
So, the normal functiontrackVar(double x, double y)
it will sent a copy x and y into the function, so even you chang them in there, nothing changes.
But, if you using reference,trackVar(double& x, double& y)
, to change x and y there, it will really change the value, so be careful using reference.
for a simple example:
void trackVar(double& a, double& b)
{
a = 99;
b = 66;
}
int main()
{
double a = 1;
double b = 2;
trackVar(a, b);
std::cout << "New a: "<< a << std::endl;
std::cout << "New b: " << b << std::endl;
return 0;
}
output: New a: 99
output: New b: 66
Related Topics
C++ on X86-64: When Are Structs/Classes Passed and Returned in Registers
What Happens When a Computer Program Runs
How to Navigate Through a Vector Using Iterators? (C++)
How to Convert Cstring and Std::String Std::Wstring to Each Other
Is #Pragma Once Part of the C++11 Standard
Launch Failed. Binary Not Found. Cdt on Eclipse Helios
Openmp Set_Num_Threads() Is Not Working
Why Doesn't Reference-To-Member Exist in C++
Preincrement Faster Than Postincrement in C++ - True? If Yes, Why Is It
Why Isn't C/C++'s "#Pragma Once" an Iso Standard
Is the Data in Nested Std::Arrays Guaranteed to Be Contiguous
C++ Expected Constant Expression
Qml and C++ Image Interoperability