Python, a high-level interpreted language, is well-known for its simplicity and readability. One of the core principles of Python is that “There should be one– and preferably only one –obvious way to do it.” This clarity and emphasis on simplicity extend to Python’s function definition and call syntax.
In this article, we will look at a detailed example of how to define a function in Python, call it, and retrieve the return value.
What is a Function in Python?
A function represents a segment of code designed for reusability that carries out a particular action. By enhancing the modular structure of your application and promoting extensive code reuse, functions contribute significantly to efficient programming. In Python, we define functions using the `def` keyword.
Here’s a simple example of a Python function:
1 2 | def greet(): print("Hello, world!") |
In this case, greet is a function that prints out “Hello, world!”. To call this function, you would simply use its name followed by parentheses:
1 | greet() |
Functions with Arguments and Return Values
While the above function is useful for demonstration, it’s fairly simplistic. Most functions will take arguments (input) and return a result. Here’s a function that takes two numbers as arguments and returns their sum:
1 2 | def add_two_numbers(num1, num2): return num1 + num2 |
The `return` statement serves to conclude a function’s execution and revert back to the point where the function was invoked. It may encompass an expression, which is then evaluated, with the resultant value sent back to the function caller. In the event of a function lacking a `return` statement, the function defaults to returning None.
To call this function and get the return value, we would do something like this:
1 2 | result = add_two_numbers(3, 5) print(result) # Outputs: 8 |
In this case, result is a new variable that we’re using to store the return value of the function.
More Complex Return Values
Functions aren’t limited to returning simple types like integers or strings. A function can return any Python object, including complex types like lists, dictionaries, or even other functions. For example, here’s a function that takes a list of numbers and returns a new list containing only the even numbers:
1 2 3 | def get_even_numbers(numbers): even_numbers = [num for num in numbers if num % 2 == 0] return even_numbers |
And here’s how we might call this function:
1 2 3 | numbers = [1, 2, 3, 4, 5, 6] even_numbers = get_even_numbers(numbers) print(even_numbers) # Outputs: [2, 4, 6] |
In this case, get_even_numbers is returning a list, which we’re then storing in the even_numbers variable.
Handling Errors
What happens if there’s an error in your function? Python functions can raise exceptions, which are a way of signaling that something unexpected has happened. You can catch these exceptions and handle them. Here’s an example:
1 2 3 4 5 6 | def divide_numbers(num1, num2): try: return num1 / num2 except ZeroDivisionError: print("Error: Division by zero is not allowed.") return None |
If you attempt to call divide_numbers(4, 0), instead of your program crashing, it will print “Error: Division by zero is not allowed.” and return None.
Conclusion
Python functions are a fundamental part of the language. They allow you to create reusable blocks of code that can perform a task and optionally return a result. The process of calling a function and handling its return value is straightforward and is a crucial part of writing effective Python code.