943,954 Members | Top Members by Rank

Ad:
  • C Discussion Thread
  • Marked Solved
  • Views: 1353
  • C RSS
You are currently viewing page 1 of this multi-page discussion thread
Aug 4th, 2009
0

Return 0;

Expand Post »
What exactly does the "Return 0;" indicate in this function?
I know my code will not even compile without it, but I have just been putting it in out of habit without realizing that I do not even know what it does. I was under the assumption that it meant this particular function returned a value of 0, or contained no parameters, but in this case that does not make sense to me.

Can somebody help me out?

  1. if (store <1 || store >3) //if store number is not 1, 2 or 3
  2. {
  3. printf("\nPlease enter 1,2 or 3\n");
  4.  
  5. printf("\n\tINVALID NUMBER ENTERED! Would you like to try again? (y/n) ");//allows retry
  6. scanf("%c", &cAgain); //scans input at INVALID prompt
  7.  
  8. if (cAgain == 'y' || cAgain == 'Y')//repeats calculator
  9. {
  10. goto YES; //back to top
  11. }
  12. else if (cAgain == 'n' || cAgain == 'N')//end
  13.  
  14. return 0;
  15. }
Similar Threads
Reputation Points: 59
Solved Threads: 1
Posting Whiz in Training
no1zson is offline Offline
226 posts
since Jul 2007
Aug 4th, 2009
0

Re: Return 0;

You're not showing the entire function so there is no context!
Reputation Points: 546
Solved Threads: 99
Practically a Posting Shark
wildgoose is offline Offline
891 posts
since Jun 2009
Aug 4th, 2009
0

Re: Return 0;

Sorry
Here is the entire program. Notice the last two loops. Return 0; completes them both. What does that accomplish?


  1. #include <stdio.h> // standard input output library
  2.  
  3. //defines tax value for calculations
  4. #define DelMar 7.25
  5. #define Encinitas 7.5
  6. #define LaJolla 7.75
  7.  
  8. char cAgain; //variable for Y/N questions
  9.  
  10. int displayMenu() //defines menu variable
  11. { // menu
  12. int selection; //defines selection variable
  13. printf("Tax Calculation for $125 Purchase\n\n");
  14. printf("1. Del Mar \n");
  15. printf("2. Encinitas \n");
  16. printf("3. La Jolla \n");
  17. printf("\n\nPlease Select Store for Which to Calculate [1-3]:");
  18.  
  19. scanf("%d",&selection);
  20. return selection;
  21. }
  22.  
  23. int main() //main loop
  24. {
  25. YES://start again point
  26. {
  27.  
  28. float sales = 125.00; // Sales value for calculations
  29. int store; //defines store for main loop, menu will not work without
  30.  
  31. store = displayMenu(); //displays menu created above
  32.  
  33. if (store == 1)
  34. //Calculates and Displays Del Mar Store, Sales, Tax rate, and Tax
  35. printf("\nDel Mar \tSale $%.2f\tRate %.2f%%\tTax $%.2f%\t\n\n",sales, DelMar, sales*DelMar/100); //values are correct so I am leaving it this way. ahead of schedule here.
  36. if (store == 2)
  37. //Calculates and Displays Encinitas Store, Sales, Tax rate, and Tax
  38. printf("\nEncinitas \tSale $%.2f\tRate %.2f%%\tTax $%.2f%\t\n\n",sales, Encinitas, sales*Encinitas/100);
  39. if (store == 3)
  40. //Calculates and Displays La Jolla Store, Sales, Tax rate, and Tax
  41. printf("\nLa Jolla \tSale $%.2f\tTax %.2f%%\tTax $%.2f%\t\n\n",sales, LaJolla, sales*LaJolla/100);
  42.  
  43. if (store <1 || store >3) //if store number is not 1, 2 or 3
  44. {
  45. printf("\nPlease enter 1,2 or 3\n");
  46.  
  47. printf("\n\tINVALID NUMBER ENTERED! Would you like to try again? (y/n) ");//allows retry
  48. scanf("%c", &cAgain); //scans input at INVALID prompt
  49.  
  50. if (cAgain == 'y' || cAgain == 'Y')//repeats calculator
  51. {
  52. goto YES; //back to top
  53. }
  54. else if (cAgain == 'n' || cAgain == 'N')//end
  55.  
  56. return 0;
  57. }
  58.  
  59.  
  60. printf("\n\tWould you like to calculate for another store? (y/n) ");//prompts for repeat
  61. scanf("%c", &cAgain);//scans input at store number prompt
  62.  
  63. if (cAgain == 'y'|| cAgain == 'Y')//repeats calculator
  64. {
  65. goto YES; //back to top
  66. }
  67. else if (cAgain == 'n' || cAgain == 'N')//end
  68.  
  69. return 0;
  70. }
  71. }
Reputation Points: 59
Solved Threads: 1
Posting Whiz in Training
no1zson is offline Offline
226 posts
since Jul 2007
Aug 4th, 2009
1

Re: Return 0;

It's on main().
0 means successful.
else error!
Reputation Points: 546
Solved Threads: 99
Practically a Posting Shark
wildgoose is offline Offline
891 posts
since Jun 2009
Aug 4th, 2009
0

Re: Return 0;

Thanks
I thought it was some kind of error deal.
What do you mean exactly, "It's on main()"
Reputation Points: 59
Solved Threads: 1
Posting Whiz in Training
no1zson is offline Offline
226 posts
since Jul 2007
Aug 5th, 2009
2

Re: Return 0;

the main() program can often be called by a parent program. 'return 0;' indicates to the caller (parent) that the main() program executed correctly.

so... generally speaking, the main function must be of type 'int' and therefore must return some integer value on completion. zero (0) is the standard "successful" return value (think: returned with zero errors) although it could be some other value that indicates an error state, depending on the caller

if main does not return a value (like the case of 'void main()' ) then you are now in the realm of "undefined behavior" when the program is called by another. Undefined behavior is almost always a Bad Thing.



.
Last edited by jephthah; Aug 5th, 2009 at 2:54 am.
Reputation Points: 2143
Solved Threads: 178
Posting Maven
jephthah is offline Offline
2,567 posts
since Feb 2008
Aug 5th, 2009
0

Re: Return 0;

Click to Expand / Collapse  Quote originally posted by no1zson ...
Thanks
I thought it was some kind of error deal.
What do you mean exactly, "It's on main()"
Adding to jephthah's reply, return 0 is optional in C99 standards. So you can remove it.
Reputation Points: 45
Solved Threads: 7
Light Poster
9868 is offline Offline
36 posts
since Mar 2009
Aug 5th, 2009
0

Re: Return 0;

Click to Expand / Collapse  Quote originally posted by 9868 ...
Adding to jephthah's reply, return 0 is optional in C99 standards. So you can remove it.
If I remove it my program does not compile. That is why I was asking. Was not sure what it was for, but I know it had to be there.

Thanks guys.
Reputation Points: 59
Solved Threads: 1
Posting Whiz in Training
no1zson is offline Offline
226 posts
since Jul 2007
Aug 6th, 2009
0

Re: Return 0;

Click to Expand / Collapse  Quote originally posted by no1zson ...
If I remove it my program does not compile. That is why I was asking. Was not sure what it was for, but I know it had to be there.

Thanks guys.
1. Looking at your code now, I must tell you that those return 0s were not for main(). You dont have return 0 associated with main anywhere in your code(which should be before the ending brace of main).
2. You can remove them(return 0) without any errors, but else if has to be managed and you can simply put a semicolon at the end of else if to tell it not to do anything.
3. There is also an issue of '\n' in the keyboard buffer with scanf, so you have to modify it as well to make the program completely executable.
4. switch case is advisable in your code.
5. avoid goto.
Last edited by 9868; Aug 6th, 2009 at 1:40 am.
Reputation Points: 45
Solved Threads: 7
Light Poster
9868 is offline Offline
36 posts
since Mar 2009
Aug 6th, 2009
0

Re: Return 0;

9868,
I am going to give that a try and see what happens. Thanks for the tips.
What is wrong with GOTO? What would you suggest in its place?
Reputation Points: 59
Solved Threads: 1
Posting Whiz in Training
no1zson is offline Offline
226 posts
since Jul 2007

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C Forum Timeline: can u please solve
Next Thread in C Forum Timeline: Reading char from stdin without waiting for input





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC