Blog Post #84: Your First Reusable Code: Defining a Simple Function with def

In Post #83, we learned about the “Don’t Repeat Yourself” (DRY) principle and why avoiding repeated code is so important for writing clean and maintainable programs. We established that the solution is to package our reusable logic into a block called a function.

Now it’s time to learn how to do that. In this post, you will learn the basic syntax for defining and calling your first function in Python.

Defining a Function with def

To create a function, you use the def keyword, which is short for “define”. This keyword tells Python that you are about to create a new function.

The basic syntax looks like this:

def function_name():
    # The body of the function
    # This code is indented and will run when the function is called.
    print("Hello from inside a function!")

Let’s break down each part of this structure:

  • def: The keyword that always starts a function definition.
  • function_name: The name you choose for your function. It should be descriptive and follow the same snake_case convention as variables (e.g., calculate_sum, print_report).
  • (): A pair of parentheses after the name. For now, they will be empty, but they are always required. We will learn their purpose very soon.
  • :: A colon that marks the end of the function header and signals the beginning of the indented function body.
  • Indented Body: The block of code that belongs to the function. This code will only run when the function is explicitly “called.”

From ‘WET’ to ‘DRY’: A Practical Example

Let’s take the repetitive “WET” code from our last post and refactor it into a DRY, reusable function. Instead of copying and pasting the banner logic, we’ll define it once.

def display_welcome_banner():
    print("--------------------")
    print("Welcome!")
    print("Have a great day!")
    print("--------------------")

We have now defined a function named display_welcome_banner that contains our four lines of print statements.

Defining vs. Calling a Function

A very important concept to understand is that defining a function does not execute it. If you run a script containing only the code above, nothing will be printed to the screen.

Defining a function is like writing down a recipe and giving it a name. The steps are recorded, but no cooking happens until you decide to actually follow that recipe.

The act of “following the recipe” is called calling the function. You call a function by writing its name followed by a set of parentheses.

display_welcome_banner()

Putting It All Together

Let’s see the full process. First, we define our function. Then, we can call it as many times as we need.

# 1. DEFINE the function (writing the recipe)
def display_welcome_banner():
    print("--------------------")
    print("Welcome!")
    print("Have a great day!")
    print("--------------------")


# 2. CALL the function (using the recipe)
print("Processing for User 1...")
display_welcome_banner()

print("\nProcessing for User 2...")
display_welcome_banner()

When Python runs this script, it first reads the def block and remembers the display_welcome_banner “recipe”. Then, it continues executing the code. When it sees display_welcome_banner(), it jumps to the function body, runs all the code inside it, and then returns to where it left off in the script.

Now, if we want to change the banner to say “Greetings!” instead of “Welcome!”, we only need to edit it in one place: inside the function definition.

What’s Next?

You’ve now learned the fundamental syntax for creating and using your own functions! By using the def keyword to define a block of code and then calling it by name, you can start writing clean, reusable, and DRY code.

We’ve now seen the difference between defining a function and calling it. To become more proficient, it’s important to be precise with our language. In Post #85, we will take a closer look at the anatomy of a function call and clarify the terminology we use to talk about them.

Author

Debjeet Bhowmik

Experienced Cloud & DevOps Engineer with hands-on experience in AWS, GCP, Terraform, Ansible, ELK, Docker, Git, GitLab, Python, PowerShell, Shell, and theoretical knowledge on Azure, Kubernetes & Jenkins. In my free time, I write blogs on ckdbtech.com

Leave a Comment