In this lesson, we will look at how to work with the functions we created.
When we define a function, it prints data on the screen:
def greeting():
print('Hello, Hexlet!')
These functions are useless because we cannot use its result in a program.
Let us look at another example. Suppose we process email addresses. When users register on the site, they can enter their email addresses any way they want:
- Add spaces at the beginning or the end by accident:
_support@hexlet.io__
- Use letters in a different case:
SUPPORT@hexlet.io
In this case, users cannot log in if we save data like this in the database. We can prevent it and prepare the email addresses before writing them to the database. We should convert them to lowercase and trim the spaces around the string edges. We can solve the problem in a couple of lines:
def save_email():
# The email comes from the form
email = ' SuppORT@hexlet.IO'
# We trim whitespace characters
trimmed_email = email.strip()
prepared_email = trimmed_email.lower()
print(prepared_email)
# We write the entry in the database, then
This code works because the strip()
and lower()
methods do not print anything on the screen. Instead, they return the results of their work so we can write the calculated values to variables. We can not assign the results to variables when the program prints the values on the screen. For example, we cannot do it with the greeting()
function:
message = greeting()
# The `print()` returns `None`
# The `None` is an object representing no value
print(message) # => None
Now, we will change the greeting()
function to make it return the data. To do this, we use a return
instead of printing to the screen:
def greeting():
return 'Hello, Hexlet!'
The return
is an instruction. It takes the expression from the right and gives it to a method. It is where the function ends:
# Now we can use the function's results
message = greeting()
print(message) # => Hello, Hexlet!
# And we can perform some actions on the results
print(message.upper()) # => HELLO, HEXLET!
Python does not execute any code after the return
instruction:
def greeting_with_code_after_return():
return 'Hello, Hexlet!'
print('I will never be executed')
Even when the function returns data, there are no limits on printing. In addition to returning data, we can also print it:
def greeting_with_return_and_printing():
print('I will appear in the console')
return 'Hello, Hexlet!'
# We print the text on the screen and return the value
message = greeting_with_return_and_printing()
You can print more than a specific value. Since return
works with expressions, we can use anything to the right of it. But still, we should keep with the code readability:
def greeting():
message = 'Hello, Hexlet!'
return message
Here we do not return a variable, but Python always returns its value. You can see an example with calculations below:
def double_five():
# We return 5 + 5
result = 5 + 5
return result
We cannot define functions without thinking about whether we will be able to use their results or not.
For practice, think about the results the call to run()
returns:
# It is the definition
def run():
return 5
return 10
# What will we see?
print(run())