473,395 Members | 1,437 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,395 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 2686
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:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

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.