Logo
/
Programming
/
Python Course
/

Creating (defining) functions

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 = 'support@hexlet.io'
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:

Hello, Hexlet!
# 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.

Instructions

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.

Tips

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.
Found a bug? Have something to add? Pull requests are welcome!