473,324 Members | 2,356 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,324 software developers and data experts.

Return with no expression

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.
Nov 14 '05 #1
6 2682
sugaray <ru****@sohu.com> scribbled the following:
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?

--
/-- Joona Palaste (pa*****@cc.helsinki.fi) ------------- Finland --------\
\-- http://www.helsinki.fi/~palaste --------------------- rules! --------/
"This is a personnel commuter."
- Train driver in Scientific American
Nov 14 '05 #2
>> 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.


"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.

--
Martijn
http://www.sereneconcepts.nl
Nov 14 '05 #3


sugaray wrote:

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.

--
Fred L. Kleinschmidt
Boeing Associate Technical Fellow
Technical Architect, Common User Interface Services
M/S 2R-94 (206)544-5225
Nov 14 '05 #4
"Martijn" <subscription_remove_101@hot_remove_mail.com> writes:
[...]
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.

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
Schroedinger does Shakespeare: "To be *and* not to be"
Nov 14 '05 #5
Martijn wrote:
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.

Nov 14 '05 #6
>> 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.


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

Sorry 'bout that...

--
Martijn
http://www.sereneconcepts.nl
Nov 14 '05 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

9
by: Ximo | last post by:
Hello, I want that the return sentence don't return anything, how can I do it?. If i do only return it returns None, and pass don't run too. Can anyone help me?, thanks. XIMO
14
by: William Ahern | last post by:
is this legal: FILE *func(void) { extern int errno; return (errno = EINVAL, NULL); } my compiler (gcc 2.9.x on openbsd) is complaining that i'm returning a pointer from int w/o a cast....
10
by: LaEisem | last post by:
On-the-job, I have "inherited" a lot of old C language software. A question or two about when "casting" of null pointer constants is needed has occurred during behind-the-scenes cleanup of some...
15
by: Greenhorn | last post by:
Hi, when a function doesn't specify a return type ,value what value is returned. In the below programme, the function sample()is returning the value passed to 'k'. sample(int); main() { int...
15
by: Nerox | last post by:
Hi, If i write: #include <stdio.h> int foo(int); int main(void){ int a = 3; foo(a); }
59
by: Michael C | last post by:
eg void DoIt() { int i = FromString("1"); double d = FromString("1.1"); } int FromString(string SomeValue) {
2
by: Raj | last post by:
Hi, I was getting an error in my program saying that return keyword must not be followed by an object expression. I was confused first because I was using return statement in a valid "if"...
11
by: Joe HM | last post by:
Hello - I have the following very simple code ... Dim lStringA As String = "" Dim lStringB As String = "" .... lStringB = Replace(lStringA, "DUMMY", "", Compare:=CompareMethod.Text)
6
by: exander77 | last post by:
I am quite new to c++, about half of a year. I juct wonder, why such code is not possible: int chlen(char * ar) { for(int i=0;ar||return i;i++); } In my opinion, it would be much "cuter"...
34
by: Davy | last post by:
Hi all, I am writing a function, which return the pointer of the int. But it seems to be wrong. Any suggestion? int * get_p_t(int t) { return &t; } int main()
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.