954,160 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Return 0;

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?

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

You're not showing the entire function so there is no context!

wildgoose
Practically a Posting Shark
896 posts since Jun 2009
Reputation Points: 546
Solved Threads: 99
 

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

#include <stdio.h>  // standard input output library

//defines tax value for calculations
#define DelMar 7.25
#define Encinitas 7.5
#define LaJolla 7.75

char cAgain;  //variable for Y/N questions

int displayMenu()  //defines menu variable
{ // menu 
    int selection; //defines selection variable
    printf("Tax Calculation for $125 Purchase\n\n");
    printf("1. Del Mar \n");
    printf("2. Encinitas \n");
    printf("3. La Jolla \n");
    printf("\n\nPlease Select Store for Which to Calculate [1-3]:");

    scanf("%d",&selection);
    return selection;
}

int main() //main loop
{
YES://start again point
{				

    float sales = 125.00; // Sales value for calculations
    int store; //defines store for main loop, menu will not work without
      
    store = displayMenu(); //displays menu created above
    
    if (store == 1)
//Calculates and Displays Del Mar Store, Sales, Tax rate, and Tax
        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.
    if (store == 2)
//Calculates and Displays Encinitas Store, Sales, Tax rate, and Tax
        printf("\nEncinitas \tSale $%.2f\tRate %.2f%%\tTax $%.2f%\t\n\n",sales, Encinitas, sales*Encinitas/100);
    if (store == 3)
 //Calculates and Displays La Jolla Store, Sales, Tax rate, and Tax
        printf("\nLa Jolla \tSale $%.2f\tTax %.2f%%\tTax $%.2f%\t\n\n",sales, LaJolla, sales*LaJolla/100);
  
    if (store <1 || store >3) //if store number is not 1, 2 or 3
    {
    	printf("\nPlease enter 1,2 or 3\n");
    	
    	printf("\n\tINVALID NUMBER ENTERED! Would you like to try again? (y/n) ");//allows retry 
          scanf("%c", &cAgain);  //scans input at INVALID prompt 
            
               if (cAgain == 'y' || cAgain == 'Y')//repeats calculator
               {
               goto YES;  //back to top
               }
               else if (cAgain == 'n' || cAgain == 'N')//end
                           
    return 0;  
    } 
    

    printf("\n\tWould you like to calculate for another store? (y/n) ");//prompts for repeat
            scanf("%c", &cAgain);//scans input at store number prompt   
            
               if (cAgain == 'y'|| cAgain == 'Y')//repeats calculator
               {
               goto YES;  //back to top
               }
               else if (cAgain == 'n' || cAgain == 'N')//end
                            
   return 0;
}
}
no1zson
Posting Whiz in Training
226 posts since Jul 2007
Reputation Points: 59
Solved Threads: 1
 

It's on main().
0 means successful.
else error!

wildgoose
Practically a Posting Shark
896 posts since Jun 2009
Reputation Points: 546
Solved Threads: 99
 

Thanks
I thought it was some kind of error deal.
What do you mean exactly, "It's on main()"

no1zson
Posting Whiz in Training
226 posts since Jul 2007
Reputation Points: 59
Solved Threads: 1
 

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.

.

jephthah
Posting Maven
2,587 posts since Feb 2008
Reputation Points: 2,143
Solved Threads: 179
 
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.

9868
Light Poster
36 posts since Mar 2009
Reputation Points: 45
Solved Threads: 7
 
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.

no1zson
Posting Whiz in Training
226 posts since Jul 2007
Reputation Points: 59
Solved Threads: 1
 

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.

9868
Light Poster
36 posts since Mar 2009
Reputation Points: 45
Solved Threads: 7
 

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?

no1zson
Posting Whiz in Training
226 posts since Jul 2007
Reputation Points: 59
Solved Threads: 1
 

no1zson> What is wrong with GOTO?
It usually is misused and it has the potential of making any code hard to debug and maintain.

no1zson> What would you suggest in its place?
Ideally, a sharp mind with the ability to design a flow control from top to bottom.

Aia
Nearly a Posting Maven
2,392 posts since Dec 2006
Reputation Points: 2,224
Solved Threads: 218
 

Meaning if it was not misused there is no issue?

Perhaps it is my lack of experience, but I would think a sharp mind might also weigh the possibility of future POTENTIAL issues against this program's simplicity and the cost of re-coding something that already works and find no real reason to remove it.

no1zson
Posting Whiz in Training
226 posts since Jul 2007
Reputation Points: 59
Solved Threads: 1
 
Meaning if it was not misused there is no issue?

The only times it should ever be used are of the most rarest situations.
So don't get any ideals about using in substitution of well planned logic.

MosaicFuneral
Posting Virtuoso
1,691 posts since Nov 2008
Reputation Points: 888
Solved Threads: 116
 

Thanks.
I was hoping that was well planned logic! lol

Shows what I know.

no1zson
Posting Whiz in Training
226 posts since Jul 2007
Reputation Points: 59
Solved Threads: 1
 

This question has already been solved

Post: Markdown Syntax: Formatting Help
You