8.1 The return
Statement
In Python, the return
statement plays a central role in functions
since it allows a function to return its value. Understanding its
usage is essential for effective software development.
The return
statement concludes the execution of a function and "returns"
the result to the calling code. If a function is intended to output a result
for further use, return
is utilized.
Returning a Value
Functions can return any type of data, including numbers, strings, objects,
even other functions and None
. Example:
def sum(a, b):
return a + b
result = sum(5, 3)
print(result) # Outputs 8
The result of adding a + b
can be assigned to the variable result
.
Returning Multiple Values
Python allows returning multiple values from a function using tuples:
def get_user():
name = "Ivan"
age = 25
return name, age
user_name, user_age = get_user()
print(user_name, user_age) # Outputs Ivan 25
Using return
to End a Function
The return
statement can also be used to immediately
end a function, including loops or conditional blocks within the function:
def check_password(pswd):
if len(pswd) < 8:
return "Password too short"
return "Password accepted"
return
Without a Value
If return
is used without a value, or is omitted entirely, the function returns
None
:
def print_message(text):
print(text)
return
result = print_message("Hello")
print(result) # Outputs None
The return
statement is a powerful tool in Python,
providing flexibility in managing the program's execution flow. It
allows functions not only to return values for further
use but also to control execution logic, making the code cleaner and
easier to understand.
8.2 The pass
Statement
Python has a special statement — pass
, which does nothing.
The pass
statement is used as a placeholder in code blocks
where content is syntactically required but functionally yet to be
defined (there should be some code, but it's undecided what it should be yet).
This is particularly useful when working on large projects and
you want to structure the program without performing any operations.
Below are several examples of using
pass
.
In function definitions:
def my_function():
pass
Here pass
is used to define a function that currently
has no implementation.
In Loops and Conditions:
for item in my_list:
pass
If you need to create a loop but don't need to perform any actions in its body.
In Classes:
Defining a class without methods and attributes to reserve the name and structure of the class for future use.
The pass
statement is often used during development and
testing, when the program structure is clear, but the implementation
details of individual components are yet to be defined. This allows developers
to organize code and gradually add functionality without disrupting
the overall application operation.
8.3 Returning a Function
In Python, the ability of a function to return another function is a powerful tool, thanks to the support of closures* and first-class functions. This language feature allows for flexible and expressive programs, leveraging concepts like decorators* and function factories*.
In Python, functions are objects, which means they can be assigned to variables, passed as arguments to other functions or returned as results. This capability makes functions exceptionally powerful tools for creating modular, easy-to-test code.
Let's, for the sake of example, create a function that will generate us functions for exponentiation. One function will square, another will cube, etc. Here's what that code might look like:
def power(exponent):
def inner(base):
return base ** exponent
return inner
square = power(2)
print(square(3)) # Outputs 9
cube = power(3)
print(cube(3)) # Outputs 27
Notice—the power
function does not call
the inner
function. Instead, inside the power
function
there's a function declaration for inner
with the base
parameter. But
the inner
function doesn't just declare this parameter, it uses the
exponent
parameter within itself.
GO TO FULL VERSION