When a C++ (boolean) function contains multiple if-else tests, and each of those tests contains a return statement (return=false; or return=true;), does the function exit when encountering the first return?
Or does the function continue executing the remaining code within that function?
In other words, when encountering that first return, does the function exit much like a break command?
Darrell
On 16 January 2012 15:52, Darrell Anderson humanreadable@yahoo.com wrote:
When a C++ (boolean) function contains multiple if-else tests, and each of those tests contains a return statement (return=false; or return=true;), does the function exit when encountering the first return?
Or does the function continue executing the remaining code within that function?
In other words, when encountering that first return, does the function exit much like a break command?
Code always returns from the function when the first return is executed.
When a C++ (boolean) function contains multiple
if-else tests, and each of those tests contains a return statement (return=false; or return=true;), does the function exit when encountering the first return?
Or does the function continue executing the remaining
code within that function?
In other words, when encountering that first return,
does the function exit much like a break command?
Code always returns from the function when the first return is executed.
Ok. Thanks. I haven't gotten that far in my studies of C++ and none of the simplistic examples on the web provided an answer. :)
Darrell
On 16 January 2012 09:59, Darrell Anderson humanreadable@yahoo.com wrote:
When a C++ (boolean) function contains multiple
if-else tests, and each of those tests contains a return statement (return=false; or return=true;), does the function exit when encountering the first return?
Or does the function continue executing the remaining
code within that function?
In other words, when encountering that first return,
does the function exit much like a break command?
Code always returns from the function when the first return is executed.
Ok. Thanks. I haven't gotten that far in my studies of C++ and none of the simplistic examples on the web provided an answer. :)
Darrell
Consider this function however
int main() { // define two integers int x = 3; int y = 4;
//print out a message telling which is bigger if (x > y) {
} else { cout << "x is smaller than y" << endl; } return 0; }
On 16 January 2012 11:22, Calvin Morrison mutantturkey@gmail.com wrote:
On 16 January 2012 09:59, Darrell Anderson humanreadable@yahoo.comwrote:
When a C++ (boolean) function contains multiple
if-else tests, and each of those tests contains a return statement (return=false; or return=true;), does the function exit when encountering the first return?
Or does the function continue executing the remaining
code within that function?
In other words, when encountering that first return,
does the function exit much like a break command?
Code always returns from the function when the first return is executed.
Ok. Thanks. I haven't gotten that far in my studies of C++ and none of the simplistic examples on the web provided an answer. :)
Darrell
Consider this function however
int main() { // define two integers int x = 3; int y = 4;
//print out a message telling which is bigger if (x > y) {
} else { cout << "x is smaller than y" << endl; } return 0; }
Sorry! gmail keeps sending my emails half way done!
int main() { // define two integers int x = 3; int y = 4;
//print out a message telling which is bigger if (x > y) { return 1; } else { cout << "x is smaller than y" << endl; } return 0; }
In this example, if X > Y then it would return. of course if X was not greater than Y it would return a the end of the function normally.
In this example, if X > Y then it would return. of course if X was not greater than Y it would return a the end of the function normally.
So you are saying when x>y that the function does not evaluate the else statement, stops at that point, and returns 1?
Darrell
On Mon, 16 Jan 2012 10:06:45 -0800 (PST) Darrell Anderson humanreadable@yahoo.com wrote:
In this example, if X > Y then it would return. of course if X was not greater than Y it would return a the end of the function normally.
So you are saying when x>y that the function does not evaluate the else statement, stops at that point, and returns 1?
Yes. Everything that you could put between the if-then-else statement and the return 0; wouldn't be executed.
Darrell
To unsubscribe, e-mail: trinity-devel-unsubscribe@lists.pearsoncomputing.net For additional commands, e-mail: trinity-devel-help@lists.pearsoncomputing.net Read list messsages on the Web archive: http://trinity-devel.pearsoncomputing.net/ Please remember not to top-post: http://trinity.pearsoncomputing.net/mailing_lists/#top-posting
Le lundi 16 janvier 2012, Darrell Anderson a écrit :
When a C++ (boolean) function contains multiple if-else tests, and each of those tests contains a return statement (return=false; or return=true;), does the function exit when encountering the first return?
Or does the function continue executing the remaining code within that function?
In other words, when encountering that first return, does the function exit much like a break command?
Darrell
--------------------------------------------------------------------- Hi everybody,
If it returns on this instruction "return=false;", does this writing supose that the "=" operator had been overloaded? Can someone tel me the interest of overloading the "=" operator for a return instruction? It just looks to me like adding ambigiusity, even if the reader knows that "return" is a reserved word and not an variable identifier. The old K&R "return(expression);" looks definitively clearer to me.
I personely would write something like if (IsRouge()) return(Red()); if (IsJaune()) return(Yellow()); ... rather than if (IsRouge) return(Red()); else if (IsJaune return(Yellow()); Is'nt that "else" keyword adding ambigiusity, reason why this thread exists? Furthermore: depending of the compiler and optimisation, I suppose that code could be added because the "else" keyword existance.
Thank you, Patrick
Is'nt that "else" keyword adding
ambigiusity, reason why this thread exists? Furthermore: depending of the compiler and optimisation, I suppose that code could be added because the "else" keyword existance.
I'm not qualified to answer your coding question, but with reference to why this thread exists, I never stated my reason. :) My reason is I was browsing the code of tdelibs/kio/kio/kdirwatch.cpp in the hopes of seeing something obvious to help resolve bug report 756. As I am learning C++, one of the functions in that cpp caught my attention. I wanted to know how multiple return statements within a function operate. Here is the particular snippet from line 707:
==================================================== #ifdef HAVE_INOTIFY // setup INotify notification, returns false if not possible bool KDirWatchPrivate::useINotify( Entry* e ) { e->wd = 0; e->dirty = false; if (!supports_inotify) return false;
e->m_mode = INotifyMode;
int mask = IN_DELETE|IN_DELETE_SELF|IN_CREATE|IN_MOVE|IN_MOVE_SELF|IN_DONT_FOLLOW; if(!e->isDir) mask |= IN_MODIFY|IN_ATTRIB; else mask |= IN_ONLYDIR;
// if dependant is a file watch, we check for MODIFY & ATTRIB too for(Entry* dep=e->m_entries.first();dep;dep=e->m_entries.next()) { if (!dep->isDir) { mask |= IN_MODIFY|IN_ATTRIB; break; } }
if ( ( e->wd = inotify_add_watch( m_inotify_fd, TQFile::encodeName( e->path ), mask) ) > 0 ) return true;
if ( e->m_status == NonExistent ) { if (e->isDir) addEntry(0, TQDir::cleanDirPath(e->path+"/.."), e, true); else addEntry(0, TQFileInfo(e->path).dirPath(true), e, true); return true; }
return false; } #endif ====================================================
There are four return statements in that function, two true and two false. I have been told that a function stops executing upon any return statement. That is good enough for me. If that was incorrect then I would read the snippet as always returning false because that is the last statement in the function. I did not think that was correct and my texts provided me no clues. Therefore I asked the question to be sure. Now I know that any of three return statements prior to the final return statement will cause the function to exit and return that assigned boolean value.
For those itching to hack some code and become a hero, bug report 756 identifies several items when Konqueror fails to update the file pane when a file changes state.
Darrell
Le lundi 16 janvier 2012, Darrell Anderson a écrit :
For those itching to hack some code and become a hero, bug report 756 identifies several items when Konqueror fails to update the file pane when a file changes state.
Hi Darell,
I will not be that hero! I did not even wanted to do search in the images of the source files I have. The files are part of a partition of 400 GB and it requires me to finish Konqueror with the saber :-) That way, I'm rather filling anti-hero!
I am very far from being an ace in C++, and that's why I asked too. I am reassured: there is no "=" after "return"...
Sincerly, Patrick
I am very far from being an ace in
C++, and that's why I asked too. I am reassured: there is no "=" after "return"...
Typing the equal sign in my original post was my mistake. The snippet of code to which I referred does not use equal signs. Thus, I'm the cause of your confusion. :)
I'm glad you asked because these little things help me learn. I'm getting the hang of parts of C++ but find the language peculiar. I hope six months from now I don't feel that way! :)
Darrell