Python Basics
Theory: Definition of functions
It is easier to write and maintain programs when you define your functions. They allow you to combine compound operations into one. So in this lesson, we will learn how to create your functions.
How to define functions
Suppose we want to send emails on a website. This complicated process involves interaction with external systems. But you can hide its complexity behind a simple function you define:
This call does a lot internally. It connects to the mail server, forms a valid request containing the message header and the body, sends everything out, and closes the connection.
Now, we will create our first function. Its task will be to display a greeting:
Unlike ordinary data, functions perform actions. Therefore, we should specify their names through verbs: build something, draw this, open that.
The information indented below the function name is the body of the function. We can enter any code within the body. It resembles a small independent program with whatever instructions you put in.
We execute the body when we start the function. In this case, each function call launches the body of the other calls independently.
The body of the function can be empty. In that case, we use the keyword pass inside it:
The term "to create a function" has many synonyms. For example, you can implement or define it. Programmers often use these terms in the real world. By creating your function, you can easier make complex operations and development.
Completed
0 / 43