|
-
Jan 2nd, 2003, 10:48 AM
#1
Thread Starter
Frenzied Member
return
I am reading this book and it is using return to do certain things like putting return x * y (x-1); at the bottom of the program. While a function prototype is at the top outside all braces.
But it does not explain what return is actually doing? And I know that return 0 terminates the program but why?
Last edited by aewarnick; Jan 2nd, 2003 at 11:03 AM.
-
Jan 2nd, 2003, 12:39 PM
#2
Frenzied Member
return does actually what it says it does 
it returns a value.
lets try it by an example, let's say you have a function that is used to increment a number you pass it by 5.
Code:
int Inc(int iNr){
return (iNr + 5);
}
The function expects an integer as a parameter, and returns an integer too.
so if you call this function like
Code:
int iTmp = 5; //This is the value we're gonna send to the function
iTmp = Inc(iTmp); //This will pass 5 to the function
iTmp will be 10 now, since we passed 5, and the function added 5 to it.
I hope you understand it now? 
By the way, with returning from the Main function, you can specify error codes. Like if you return 0 it means no error occurred, and if you return 1 you can specify that something went wrong. This is used if your app is being launched from another app, and the numbers you return can mean anything, it's just the way you implement it.
usually you just return 0
Jop - validweb.nl
Alcohol doesn't solve any problems, but then again, neither does milk.
-
Jan 2nd, 2003, 01:22 PM
#3
Monday Morning Lunatic
If your program returns other than 0, most environments will assume it failed to do what was necessary. For example:
Code:
$ myprog --arg=val && somecommand --dependent=myprog
If myprog fails, then somecommand cannot operate correctly. If you return 0 from myprog, the shell will interpret that as non-zero, i.e. success.
I refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
-- Linus Torvalds
-
Jan 2nd, 2003, 03:13 PM
#4
Frenzied Member
Oops I messed things up, sorry I rarely make console apps, my bad...
Jop - validweb.nl
Alcohol doesn't solve any problems, but then again, neither does milk.
-
Jan 2nd, 2003, 10:26 PM
#5
Thread Starter
Frenzied Member
Jop, did you erase your post?
-
Jan 3rd, 2003, 10:35 AM
#6
The DOS choice app used return values from the app too. You used it in batch files. You call it and it waits for the user to type one of several characters. Then it returns the 0-based index of this character in the parameter list. You could then test this value using the ERRORLEVEL.
if ERRORLEVEL 3 goto three
if ERRORLEVEL 2 goto two
...
retrun actually means "return from the function". You can simply write
return;
if the function has no return type. This will exit the function.
If the function has a return type you have to specify what should be returned:
return 0;
All the buzzt
 CornedBee
"Writing specifications is like writing a novel. Writing code is like writing poetry."
- Anonymous, published by Raymond Chen
Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.
-
Jan 5th, 2003, 01:07 AM
#7
Thread Starter
Frenzied Member
I think that I understand return now. Return is only useful if another function or statement calls it, right? Except for the return 0 to end the program.
-
Jan 5th, 2003, 07:50 AM
#8
return isn't called. return is a statement.
return is useful in many places. It ends the function you're in and gives control back to the function that called it. It can optionally return a value.
The return 0; at the end of your app gives control back to the function that called your main function. This is some windows-internal function which will then end the program.
All the buzzt
 CornedBee
"Writing specifications is like writing a novel. Writing code is like writing poetry."
- Anonymous, published by Raymond Chen
Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.
-
Jan 5th, 2003, 08:36 AM
#9
Guru
Originally posted by CornedBee
This is some windows-internal function
Not a Windows function but a CRT function, mainCRTStartup, assuming you use the MSVCRT.
Check out: crt\src\crt0.c
It's one big function that's actually four functions all in one.
The function (of the four) that will be compiled depends on the preprocessor macros defined.
Code:
#ifdef _WINMAIN_
#ifdef WPRFLAG
int wWinMainCRTStartup(
#else /* WPRFLAG */
int WinMainCRTStartup(
#endif /* WPRFLAG */
#else /* _WINMAIN_ */
#ifdef WPRFLAG
int wmainCRTStartup(
#else /* WPRFLAG */
int mainCRTStartup(
#endif /* WPRFLAG */
#endif /* _WINMAIN_ */
void
)
{
This is the function Windows calls on startup (the entry point).
Looking further down...
Code:
mainret = main(__argc, __argv, _environ);
is the actual call to main, and
is the call that terminates the process after main returns.
-
Jan 5th, 2003, 10:48 AM
#10
Good Ol' Platypus
Return codes and && save you a hell of a lot of grief in UNIX command lines.
All contents of the above post that aren't somebody elses are mine, not the property of some media corporation. 
(Just a heads-up)
-
Jan 5th, 2003, 11:57 AM
#11
Originally posted by Yonatan
Not a Windows function but a CRT function, mainCRTStartup, assuming you use the MSVCRT.
I know, but it doesn't really matter, does it? Let's not make it overcomplicated. I regretted even mentioning the startup function when I had posted this.
All the buzzt
 CornedBee
"Writing specifications is like writing a novel. Writing code is like writing poetry."
- Anonymous, published by Raymond Chen
Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.
-
Jan 5th, 2003, 12:03 PM
#12
Monday Morning Lunatic
Anything outside main() should never normally be considered, except by compiler/library implementors.
For example, on here I don't have a mainCRTStartup function...
I refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
-- Linus Torvalds
-
Jan 5th, 2003, 12:05 PM
#13
Yep, that's why I regretted it.
All the buzzt
 CornedBee
"Writing specifications is like writing a novel. Writing code is like writing poetry."
- Anonymous, published by Raymond Chen
Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.
-
Jan 5th, 2003, 02:41 PM
#14
Thread Starter
Frenzied Member
"It ends the function you're in and gives control back to the function that called it."
What if I put return; at the end of a function but no funcion has called it. For example:
{int main()
cout<<"fjlakjsdlkfjdjflkajsd";
int funk (){
int c; cin>>c; cout<<"c";
return;}
return 0;}
-
Jan 5th, 2003, 02:42 PM
#15
Thread Starter
Frenzied Member
"It ends the function you're in and gives control back to the function that called it."
What if I put return; at the end of a function but no funcion has called it. Will it just be ignored and move on down? For example:
{int main()
cout<<"fjlakjsdlkfjdjflkajsd";
int funk (){
int c; cin>>c; cout<<"c";
return;}
return 0;}
-
Jan 5th, 2003, 03:05 PM
#16
You may not put a function inside another function.
All the buzzt
 CornedBee
"Writing specifications is like writing a novel. Writing code is like writing poetry."
- Anonymous, published by Raymond Chen
Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.
-
Jan 5th, 2003, 04:28 PM
#17
Thread Starter
Frenzied Member
I can't put any function inside int main()? So, for, while, do/while, and if are not functions? How do I know what is a function and what is not?
Last edited by aewarnick; Jan 5th, 2003 at 04:34 PM.
-
Jan 5th, 2003, 04:31 PM
#18
Guru
Nor inside any other, no.
Note that you can put a function declaration inside a function:
Code:
int a()
{
int b();
return b() + 17;
}
int b()
{
return 42;
}
But this is deprecated, and if it isn't, it should be.
-
Jan 5th, 2003, 04:38 PM
#19
Thread Starter
Frenzied Member
So, for, while, do/while, and if are not functions? How do I know what is a function and what is not?
-
Jan 5th, 2003, 05:14 PM
#20
Monday Morning Lunatic
A function is defined as a function. I.E.:
Code:
type name(args) {
/* statements */
}
A function looks like a special kind of block, which is any code between braces.
I refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
-- Linus Torvalds
-
Jan 5th, 2003, 05:15 PM
#21
Thread Starter
Frenzied Member
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|