Hello, OnlineGDB Q&A section lets you put your programming query to fellow community users. Asking a solution for whole assignment is strictly not allowed. You may ask for help where you are stuck. Try to add as much information as possible so that fellow users can know about your problem statement easily.

What's the difference between return 1 and return 0?

+2 votes
asked Dec 6, 2019 by yurbnessessness (140 points)
I'm sort of new and was trying to learn about recursion so I made a factorial calculator

#include <iostream>

using namespace std;

int FirstFactorial(int num) {
  int answer;
  if (num <= 1) {
    return 1;
  }  
  else {
    answer = num * FirstFactorial(num - 1);
  }
return answer;
}

int main() {
  int num;
  cin >> num;
  cout << FirstFactorial(num) << endl;
  return 0;
    
}

When I change the return 1 in the if statement to a return 0 it stops working, I was just wondering why

3 Answers

+1 vote
answered Dec 8, 2019 by Anmolnoor (240 points)

return from main() is equivalent to exit

the program terminates immediately execution with exit status set as the value passed to return or exit

return in an inner function (not main) will terminate immediately the execution of the specific function returning the given result to the calling function.

exit from anywhere on your code will terminate program execution immediately.


status 0 means the program succeeded.

status different from 0 means the program exited due to error or anomaly.

If you exit with a status different from 0 you're supposed to print an error message to stderr so instead of using printf better something like

if(errorOccurred) {
    fprintf(stderr, "meaningful message here\n");
    return -1;
}

note that (depending on the OS you're on) there are some conventions about return codes.

Google for "exit status codes" or similar and you'll find plenty of information on SO and elsewhere.


Worth mentioning that the OS itself may terminate your program with specific exit status codes if you attempt to do some invalid operations like reading memory you have no access to.

commented Dec 24, 2019 by anonymous
#include<conio.h>
#include<stdio.h>
void main()
{
    char rec[80], ch;
    char fname[20];
    int count=0, i;
    char ans='y';
    clrscr();

    cout<<"Enter file name: ";
    cin.get(fname, 20);
    ofstream fout(fname, ios::out);

    if(!fout)
    {
        cout<<"Error in creating the file..!!\n";
        cout<<"Press any key to exit...\n";
        getch();
        exit(1);
    }
    cin.get(ch);

    cout<<"Enter information to store..\n";
    while(ans=='y' || ans=='Y')
    {
        cin.get(rec, 80);
        fout<<rec<<"\n";
        cout<<"Want to enter more ? (y/n).. ";
        cin>>ans;
        count++;
        cin.get(ch);
    }
    cout<<"\nThe information successfully stored in the file..!!\n";
    fout.close();

    cin.get(ch);
    cout<<"Want to see ? (y/n)..";
    cin>>ans;

    if(ans=='y' || ans=='Y')
    {
        ifstream fin(fname, ios::in);
        if(!fin)
        {
            cout<<"Error in opening the file..!!\n";
            cout<<"Press any key to exit..\n";
            getch();
            exit(2);
        }
        fin.seekg(0);
        cout<<"\n";

        cout<<"The file contains:\n";
        for(i=0; i<count; i++)
        {
            fin.get(rec, 80);
            fin.get(ch);
            cout<<rec<<"\n";
        }
        fin.close();
    }

    getch();
}
    return 0;
}
0 votes
answered Dec 21, 2019 by kothakushal (560 points)

we use return statements in functions also

in the sub program function if we write return 0;

{ then it returns nothing to the main function}

if in the sub program function if we write return 1;

{then it will return the value 1 to the main function and there we store in some variable

we print that variabe (1)

0 votes
answered Dec 21, 2019 by Stark
By stops working you mean it gives final result 0 if you change the return condition in if statement to 0.

It is caused by basic maths whenever a number is multiplied by 0 the whole number becomes 0.

In this case, let we are taking num = 5                                      [5*4*3*2*1=ans]

your recursion function will work like

5*FirstFactorial(4)

4*FirstFactorial(3)

3*FirstFactorial(2)

2*FirstFactorial(1)

conditions fulfilled as num = 1 now and if return 0 is there it will send back 0

2*0

3*0

4*0

5*0

0 Final answer

That's why return 1 is important  1 * any number no effect on that number.
Welcome to OnlineGDB Q&A, where you can ask questions related to programming and OnlineGDB IDE and and receive answers from other members of the community.
...