I have created a procedure, exitFailure() that is invoked if a file doesn't exist:

int exitFailure()

{//start exitFailure()

	printf("Failure, hit any key to exit and press enter.\n");
	scanf("%d", &counterx);
	return 1;

}//end exitFailure()

Below is the snippit of code that invokes exitFailure()

inFile.open(inputFilename, ios::in);
if (!inFile) 
{
	cerr << "Can't open input file " << inputFilename << endl;
	exitFailure();
	return 1;
}

If the file doesn't exist, the program exits. Please note that return 1 is included in the procedure, as well as the snippit of code.

HOWEVER, if I remove return 1 from the snippit of code, run the program and enter in a filename that doesn't exist, the program continues to execute. It is as if my program is ignoring the return 1 from procedure exitFailure().

Please advise

The way I see your problem, it can be read in two ways. First, either you're not sure how to work with returned variables, or you're misunderstanding what return actually does.

1st understanding:

You need to catch the return from exitFailure and do something with it.

inFile.open(inputFilename, ios::in);
if (!inFile) 
{
	cerr << "Can't open input file " << inputFilename << endl;
	return exitFailure();
}

or

inFile.open(inputFilename, ios::in);
if (!inFile) 
{
	cerr << "Can't open input file " << inputFilename << endl;
	int retCode = exitFailure();
	return retCode;
}

2nd understanding:
Using return only exits the application if you're returning it from main(). If you use return in another function, it's going to return to the function that called the second.

int exitApp()
{
    return 1; // Returns 1 back to main
}

int main()
{
    exitApp(); // Call exitApp function
    
    return 0; // Exits the application and returns 0 to the OS's application layer (don't quote me on that)
}

Edited 6 Years Ago by Sodabread: n/a

Comments
Nice explanation :)
This question has already been answered. Start a new discussion instead.