Can void functions return value ?

In c++ primer
[qoute]
A return statement terminates the function that is currently executing and returns
control to the point from which the function was called. There are two forms of
return statements:
return ;
return expression ;

A function with a void return type may use the second form of the return
statement only to return the result of calling another function that returns void.
Returning any other expression from a void function is a compile-time error

[\qoute]

Can anyone give an example ?
Last edited on
The book means like this.

1
2
3
4
5
6
7
8
9
void foo()
{
	// ...
}

void bar()
{
	return foo(); // same as writing foo(); return;
}

It's mainly useful when writing templated code.
A function with a void return type may:
a. not have a return statement
b. have an empty return statement
c. have a return statement where the expression is of type void

Example:
1
2
3
4
5
6
7
8
9
10
11
void fn_a( int& a ) { ++a ; } // fine: no return statement

void fn_b( int& a ) { fn_a(a) ; return ; } // fine: empty return statement

void fn_c( int& a ) { return fn_b(a) ; } // fine: return expression of type void

void fn_d( int& a ) { fn_c(a) ; return void(0) ; } // fine: return expression of type void

void fn_e( int& a ) { fn_d(a) ; return void( "hello world!" ) ; } // fine: return expression of type void

void fn_f( int& a ) { fn_e(a) ; return throw 123.45 ; } // fine: return expression of type void 
Topic archived. No new replies allowed.