Python: Creating (defining) functions
It is easier to write and maintain programs if you define your own functions. They allow you to combine compound operations into one. So in this lesson, we'll talk about how to create your own functions.
Let's say we want to send emails on a website. This is quite a complicated process that involves interaction with external systems. But if you define a function, all the complexity can be hidden behind one simple function:
# Hypothetical example # Where the function comes from from emails import send email = 'email@example.com' title = 'Help' body = 'I wrote a success story, how can I get a discount?' # There's just one call, but it's got a lot of logic inside send(email, title, body)
Internally, this call does a lot; it connects to the mail server, forms a valid request based on the message header and body, and then sends everything out without forgetting to close the connection.
Let's create our first function. Its task is to display a greeting:
# Function definition # Defining a function does not invoke or execute it # We're only saying that this function now exists def show_greeting(): # Indent four spaces inside the body text = 'Hello, Hexlet!' print(text) # Function call show_greeting() # => 'Hello, Hexlet!'
Unlike ordinary data, functions perform actions. Therefore, their names should be specified through verbs: “build something,” “draw something,” “open something”.
The information that's indented below the function name is called the function body. Any code can be entered within the body. It's like a small independent program, with whatever instructions you put in
The body is executed the moment the function is started. In this case, each function call launches the body independently of the other calls.
The body of the function can be empty. In that case, the keyword
pass is used inside it:
# Minimum function definition def noop(): pass noop()
The term “create a function” has many synonyms, you can also implement a function, or define it. These can often be found when working the real world. By creating your own function, you can make complex operations and development easier.
Implement a function named
print_motto() that displays the phrase Winter is coming.
print_motto() # => Winter is coming
In tasks where you have to implement a function, you don't need to call it. Automated tests will call this function to check if it works. The example call above is shown just to give you an idea of how your function will be used.
The exercise doesn't pass checking. What to do? 😶
If you've reached a deadlock it's time to ask your question in the «Discussions». How ask a question correctly:
- Be sure to attach the test output, without it it's almost impossible to figure out what went wrong, even if you show your code. It's complicated for developers to execute code in their heads, but having a mistake before their eyes most probably will be helpful.
In my environment the code works, but not here 🤨
Tests are designed so that they test the solution in different ways and against different data. Often the solution works with one kind of input data but doesn't work with others. Check the «Tests» tab to figure this out, you can find hints at the error output.
My code is different from the teacher's one 🤔
It's fine. 🙆 One task in programming can be solved in many different ways. If your code passed all tests, it complies with the task conditions.
In some rare cases, the solution may be adjusted to the tests, but this can be seen immediately.
I've read the lessons but nothing is clear 🙄
It's hard to make educational materials that will suit everyone. We do our best but there is always something to improve. If you see a material that is not clear to you, describe the problem in “Discussions”. It will be great if you'll write unclear points in the question form. Usually, we need a few days for corrections.
By the way, you can participate in courses improvement. There is a link below to the lessons course code which you can edit right in your browser.