Program Control Flow and Functions
exit() terminates the entire program immediately and never returns to any caller.
When the Program Must Stop
A program may reach an error that it cannot recover from. For example, a program may need to open a file before it can continue, but the file operation may fail. In that situation, the program can print an error message and use exit() to terminate the entire program immediately. The important idea is that exit() does not merely leave the current function: it stops the program and does not return to any caller.
Use exit() for an unrecoverable error when continuing execution would not be meaningful.
Tracing an Immediate Exit
Calling exit() ends the entire program immediately. The current function does not continue, its callers do not regain control, and the rest of the program does not execute. This is why exit() is stronger than an ordinary function-level control-flow statement.
What do you think happens?
A deeply nested function calls exit(). What receives control next?
Reveal answer
The operating system after the program terminates
exit() does not return to any caller. It terminates the entire program and sends a status code to the operating system.
Return and Exit Compared
| Control-flow action | Where control goes | Effect on the program |
|---|---|---|
| return | Back to the function's caller | The wider program can continue |
| exit() | No caller receives control | The entire program terminates |
The central control-flow difference between return and exit().
A return statement belongs to the control flow of a function. It passes control back to the function that called the current function. That caller may then continue the wider program. By contrast, exit() does not pass control back to any caller. It terminates the entire program regardless of how deeply nested the call is.
Handling a Failed File Operation
A file cannot be opened
A program asks the user for a filename, tries to open it, and counts lines beginning with Subject:. What should happen if the file cannot be opened?
- 1Receive the filename
The program asks the user for the filename that it needs to process.
- 2Attempt the file operation
The program tries to open the file. If the operation fails, normal processing cannot continue.
- 3Handle the error
An except block handles the file error and prints an error message so the user knows what went wrong.
- 4Terminate cleanly
The error-handling path calls
exit(), so the program stops instead of continuing to the line-counting work.
The program reports the file error and terminates rather than attempting to count lines from a file that it could not open.
This use of exit() is graceful because the program communicates the problem before stopping. The error message explains the failure, and exit() prevents later file-processing steps from running with an unavailable file.
Status Codes and Program State
| exit() form | Status code | Meaning |
|---|---|---|
| exit() with no argument | 0 | Success or normal termination |
| exit() with an integer error code | Non-zero | An error occurred |
Status codes communicate the program's ending state to the operating system.
When a program exits, it sends a status code to the operating system. Calling exit() with no argument sends status code 0, which represents success or normal termination. An integer can also be passed to exit(); a non-zero value is typically used to indicate that an error occurred.
Choosing the Control-Flow Path
First decide whether the program can continue meaningfully. If the problem can be handled by the current function's caller, return control to that caller. If the problem prevents the entire program from doing its work, report the error and use exit() to terminate cleanly. A failed file operation is an example of an error that can require this whole-program response when the file is essential to the next steps.
Common mistake Treating exit() like return.
What it looks likeAssuming that a caller will continue after a nested function calls exit().
Why it failsreturn passes control to a caller, but
exit()terminates the entire program and never returns to any caller.Do this insteadUse return when control should go back to the caller; use exit() only when the whole program should stop.
Common mistake Continuing after an essential file operation fails.
What it looks likeTrying to count lines after the program could not open the requested file.
Why it failsThe file-dependent work cannot proceed normally when the file was not opened.
Do this insteadIn the file-error handler, print an error message and terminate with exit().
Common mistake Ignoring the meaning of the exit status.
What it looks likeEnding after an error without distinguishing normal termination from failure.
Why it failsThe operating system receives status code 0 for normal termination and a non-zero code typically for an error.
Do this insteadUse the status code to communicate whether the program ended successfully or because an error occurred.
Practice Check
A program cannot open the file that it needs to process. Write a short explanation of the control-flow response it should use. Include what should be communicated to the user, what should happen to the rest of the program, and what kind of status code should represent the failure.
Hints
- The file operation failure is handled in an except block.
- The program should print an error message before stopping.
- A non-zero status code typically indicates that an error occurred.
Checking a proposed response
Choose between return and exit() after an essential file operation fails.
- 1Identify the scope
The program cannot perform the file-dependent work it was designed to perform.
- 2Report the problem
Print an error message so the user receives a clear explanation.
- 3Stop the correct amount
Because the whole program cannot continue meaningfully, use
exit()rather than return. - 4Communicate the result
Use a non-zero status code to indicate that an error occurred.
Print the error message, call exit(), and use a non-zero status code for the failure.
Key Takeaways
- 1
exit()terminates the entire program immediately and never returns to a caller. - 2return passes control back to the function that called the current function.
- 3For an unrecoverable file error, print an error message and use
exit()so later file-processing steps do not run. - 4
exit()with no argument sends status code 0 for success or normal termination. - 5A non-zero exit status typically communicates that an error occurred.
At a glance
exit()stops the whole program rather than returning to a caller.- return and
exit()differ mainly in the scope of their control-flow effect. - A failed essential file operation can be handled by printing an error message and exiting cleanly.
- Status code 0 represents success or normal termination, while a non-zero code typically represents an error.