Break out of function in Python

In this post, we will see how to break out of function in Python.

Break out of function in Python

A function is a block of reusable code that can be called whenever required in a program. In Python, we can create functions using the def keyword. A function can return some value. This value is specified using the return statement. A function automatically ends when this statement is encountered.

We cannot use the break statement to break out of function in Python. The natural way to break out is by using the return statement only.

How to break out of function in Python?

Let us now discuss the methods to break out of function in Python.

Using the return statement to break out of function in Python

As discussed, the return statement is used to send some value back from the function. If this statement is missing then a None value is returned automatically.

The function ends automatically when a return is encountered, regardless of the code after the statement. We can use it to break out of function in Python.

See the code below.

Output:

Hello from the function

In the above example, we return a None value from the function. The print() function after the return statement is discarded and we break out of function in Python.

Using the try and except block to break out of function in Python

In Python, we use the try and except block to handle exceptions. If we have some code that can raise an exception, we put it in the try block. In the except block, we add an alternative code that will be executed in case an exception is raised.

Let us now discuss how we can use the try and except block to break out of function in Python. At the point from which we want to break out, we can add some code or use the raise statement to raise an exception.

When we execute the function, we do it inside the try block. So the code till the exception will run normally, and as soon as the exception is raised it will break out of the function and the code in the except block will execute.

We implement this logic in the code below.

Output:

Hello from the function
Exception raised

In the above example, the print(1/0) statement raises an exception and we break out of function in Python.

Using the sys.exit() function to break out of function in Python

This method can be thought of as a last resort. The sys.exit() function is used to end the python program. We can add this function to the point where we want to break out and the code will stop running.

For example,

Output:

Hello from the function
An exception has occurred, use %tb to see the full traceback.
SystemExit: 0

Conclusion

To conclude, we discussed functions and how to break out of function in Python. Several methods were demonstrated. The most obvious answer is the return statement. It exits the function whenever it is encountered.

Another convenient method involved the use of the try and except block. The issue here is that we need to raise an exception to break out of function in Python. The final method involved terminating the program completely to break out of function using the sys.exit() function.

That’s all about how to break out of function in Python.

Was this post helpful?

Leave a Reply

Your email address will not be published. Required fields are marked *