How to Break a Function in Python? Last Updated : 16 Dec, 2024 Summarize Comments Improve Suggest changes Share Like Article Like Report In Python, breaking a function allows us to exit from loops within the function. With the help of the return statement and the break keyword, we can control the flow of the loop.Using return keywordThis statement immediately terminates a function and optionally returns a value. Once return is executed, any code after it is ignored and the function exits at that point.Example: Python def fun(a): if a < 18: return "Underage" # Terminate return "Eligible" # Continue print(fun(15)) OutputUnderage Explanation:This function checks if the input a is less than 18. If true, it returns "Underage" and terminates the function.As a is 18 or more, it returns "Eligible", indicating the function continues normally.Common Methods to break a FunctionIn Python, we use the "break" and "return" statements to exit a function. However, there are other ways to manage flow control or exit from a function, depending on the situation.Let's explore these methods in detail, one by one.Table of ContentUsing return keywordUsing break keywordusing raise keywordusing sys.exit()Using break keywordbreak statement exits a loop early but allows the function to continue after the loop. It is generally used in for and while loops.Example: Python def fun(no): for i in no: if i % 2 == 0: print(i) # Found break else: print("Not Found") # Not found # input fun([1, 3, 5, 8, 7]) Output8 Explanation:This function finds the first even number in the list, prints it and exits the loop.If no even number is found, it prints "Not Found".using raise keywordraise statement exits the function by raising an exception, stopping the normal flow and passing the error.Example: Python def fun(a, b): if b == 0: raise ValueError("Error") # Error return a / b # Output # Input try: print(fun(10, 0)) # Will raise an exception except ValueError as e: print(e) # Error OutputError Explanation:This function raises a ValueError with the message "Error" if b is 0.This try block calls the function and the except block prints the error message when caught.using sys.exit()sys.exit() function terminates the entire program, not just the function and is typically used to end a script under specific conditions.Example: Python import sys def fun(a): if a < 18: print("Exiting") # Exit sys.exit() return "Valid." # Valid # Input fun(15) OutputExiting Explanation:This function exits the program if a is less than 18, printing "Exiting."Otherwise, it returns "Valid.". Comment More infoAdvertise with us Next Article How to Add Function in Python Dictionary V vishakshx339 Follow Improve Article Tags : Python python Practice Tags : pythonpython Similar Reads How to call a function in Python Python is an object-oriented language and it uses functions to reduce the repetition of the code. In this article, we will get to know what are parts, How to Create processes, and how to call them.In Python, there is a reserved keyword "def" which we use to define a function in Python, and after "de 5 min read How to Recall a Function in Python In Python, functions are reusable blocks of code that we can call multiple times throughout a program. Sometimes, we might need to call a function again either within itself or after it has been previously executed. In this article, we'll explore different scenarios where we can "recall" a function 4 min read How to Add Function in Python Dictionary Dictionaries in Python are strong, adaptable data structures that support key-value pair storage. Because of this property, dictionaries are a necessary tool for many kinds of programming jobs. Adding functions as values to dictionaries is an intriguing and sophisticated use case. This article looks 4 min read How to Define and Call a Function in Python In Python, defining and calling functions is simple and may greatly improve the readability and reusability of our code. In this article, we will explore How we can define and call a function.Example:Python# Defining a function def fun(): print("Welcome to GFG") # calling a function fun() Let's unde 3 min read Python | How to get function name ? One of the most prominent styles of coding is following the OOP paradigm. For this, nowadays, stress has been to write code with modularity, increase debugging, and create a more robust, reusable code. This all encouraged the use of different functions for different tasks, and hence we are bound to 3 min read How to Call Multiple Functions in Python In Python, calling multiple functions is a common practice, especially when building modular, organized and maintainable code. In this article, weâll explore various ways we can call multiple functions in Python.The most straightforward way to call multiple functions is by executing them one after a 3 min read Like