Return 0;

Please support our C advertiser: Programming Forums - DaniWeb Sister Site
Thread Solved

Join Date: Jul 2007
Posts: 226
Reputation: no1zson is on a distinguished road 
Solved Threads: 1
no1zson's Avatar
no1zson no1zson is offline Offline
Posting Whiz in Training

Return 0;

 
0
  #1
Aug 4th, 2009
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. }
I never drew first, but I drew first blood.
I'm no ones son, unforgiven.
Reply With Quote Quick reply to this message  
Join Date: Jun 2009
Posts: 830
Reputation: wildgoose is a name known to all wildgoose is a name known to all wildgoose is a name known to all wildgoose is a name known to all wildgoose is a name known to all wildgoose is a name known to all 
Solved Threads: 94
wildgoose's Avatar
wildgoose wildgoose is offline Offline
Practically a Posting Shark

Re: Return 0;

 
0
  #2
Aug 4th, 2009
You're not showing the entire function so there is no context!
Reply With Quote Quick reply to this message  
Join Date: Jul 2007
Posts: 226
Reputation: no1zson is on a distinguished road 
Solved Threads: 1
no1zson's Avatar
no1zson no1zson is offline Offline
Posting Whiz in Training

Re: Return 0;

 
0
  #3
Aug 4th, 2009
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. }
I never drew first, but I drew first blood.
I'm no ones son, unforgiven.
Reply With Quote Quick reply to this message  
Join Date: Jun 2009
Posts: 830
Reputation: wildgoose is a name known to all wildgoose is a name known to all wildgoose is a name known to all wildgoose is a name known to all wildgoose is a name known to all wildgoose is a name known to all 
Solved Threads: 94
wildgoose's Avatar
wildgoose wildgoose is offline Offline
Practically a Posting Shark

Re: Return 0;

 
1
  #4
Aug 4th, 2009
It's on main().
0 means successful.
else error!
Reply With Quote Quick reply to this message  
Join Date: Jul 2007
Posts: 226
Reputation: no1zson is on a distinguished road 
Solved Threads: 1
no1zson's Avatar
no1zson no1zson is offline Offline
Posting Whiz in Training

Re: Return 0;

 
0
  #5
Aug 4th, 2009
Thanks
I thought it was some kind of error deal.
What do you mean exactly, "It's on main()"
I never drew first, but I drew first blood.
I'm no ones son, unforgiven.
Reply With Quote Quick reply to this message  
Join Date: Feb 2008
Posts: 1,646
Reputation: jephthah has much to be proud of jephthah has much to be proud of jephthah has much to be proud of jephthah has much to be proud of jephthah has much to be proud of jephthah has much to be proud of jephthah has much to be proud of jephthah has much to be proud of 
Solved Threads: 122
jephthah's Avatar
jephthah jephthah is online now Online
Posting Virtuoso

Re: Return 0;

 
2
  #6
Aug 5th, 2009
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.
Reply With Quote Quick reply to this message  
Join Date: Mar 2009
Posts: 36
Reputation: 9868 is on a distinguished road 
Solved Threads: 7
9868 9868 is offline Offline
Light Poster

Re: Return 0;

 
0
  #7
Aug 5th, 2009
Originally Posted by no1zson View Post
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.
Reply With Quote Quick reply to this message  
Join Date: Jul 2007
Posts: 226
Reputation: no1zson is on a distinguished road 
Solved Threads: 1
no1zson's Avatar
no1zson no1zson is offline Offline
Posting Whiz in Training

Re: Return 0;

 
0
  #8
Aug 5th, 2009
Originally Posted by 9868 View Post
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.
I never drew first, but I drew first blood.
I'm no ones son, unforgiven.
Reply With Quote Quick reply to this message  
Join Date: Mar 2009
Posts: 36
Reputation: 9868 is on a distinguished road 
Solved Threads: 7
9868 9868 is offline Offline
Light Poster

Re: Return 0;

 
0
  #9
Aug 6th, 2009
Originally Posted by no1zson View Post
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.
Reply With Quote Quick reply to this message  
Join Date: Jul 2007
Posts: 226
Reputation: no1zson is on a distinguished road 
Solved Threads: 1
no1zson's Avatar
no1zson no1zson is offline Offline
Posting Whiz in Training

Re: Return 0;

 
0
  #10
Aug 6th, 2009
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?
I never drew first, but I drew first blood.
I'm no ones son, unforgiven.
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:



Similar Threads
Other Threads in the C Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC