Return with no expression

S

sugaray

hi, this question just came up my mind that what's the difference between
a 'return' with no expression and a 'break' or 'exit()' ? thanx in advance
for your direction.
 
J

Joona I Palaste

sugaray said:
hi, this question just came up my mind that what's the difference between
a 'return' with no expression and a 'break' or 'exit()' ? thanx in advance
for your direction.

Quite a big difference.
"return" returns from the current function. If the current function is
the original invocation of main(), the program ends. Otherwise flow of
control returns to whichever function called the current function.
"break" returns from the current loop (for, while or do...while). If the
loop is the last statement of the function body, or there is a "return"
after it, the function returns (see "return"). Otherwise it continues as
normal.
"exit()" ends the program, there and then. No matter from which function
it was called, and how many loops it is buried in, it always ends the
entire program.
Might I suggest you read a C textbook?
 
F

Fred L. Kleinschmidt

sugaray said:
hi, this question just came up my mind that what's the difference between
a 'return' with no expression and a 'break' or 'exit()' ? thanx in advance
for your direction.

'return' with no expression transfers control from a function (whose
return type is void) to the calling function. It is not valid in a
function with a non-null return type.

'break' transfers control out of a switch statement or do loop.

'exit()' is not legal. The exit function requires an int argument. It
terminates the program.
 
M

Martijn

hi, this question just came up my mind that what's the difference
"break" returns from the current loop (for, while or do...while). If
the loop is the last statement of the function body, or there is a
"return" after it, the function returns (see "return"). Otherwise it
continues as normal.

And of course it is used in switch statements as well, where it is a crucial
element.

IIRC C++ would allow this too:

int somefunction(void)
{
int i;

{
int j;

// do something here
if ( j )
break;

// do something else here
}
}

But I am no authority on the subject.
 
K

Keith Thompson

Martijn said:
IIRC C++ would allow this too:

int somefunction(void)
{
int i;

{
int j;

// do something here
if ( j )
break;

// do something else here
}
}

I don't believe so, but this isn't the place to discuss it.
 
E

E. Robert Tisdale

Martijn said:
IIRC C++ would allow this too:

> cat somefunction.cc
int somefunction(void) {
int i;

{ int j;

// do something here
if (j)
break;

// do something else here
}
}
> g++ -Wall -ansi -pedantic -c somefunction.cc
somefunction.cc: In function `int somefunction()':
somefunction.cc:2: warning: unused variable `int i'
somefunction.cc:8: break statement not within loop or switch

Nope.
 
M

Martijn

IIRC C++ would allow this too:
int somefunction(void) {
int i;

{ int j;

// do something here
if (j)
break;

// do something else here
}
}

somefunction.cc: In function `int somefunction()':
somefunction.cc:2: warning: unused variable `int i'
somefunction.cc:8: break statement not within loop or switch

Nope.

I must have mixed this up with a different OT language all together then -
something like Perl or PHP...

Sorry 'bout that...
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,142
Messages
2,570,818
Members
47,362
Latest member
eitamoro

Latest Threads

Top