mailwal

Python From Scratch #31 - Functions

Shefali Jangid

Python From Scratch #31 - Functions

Learn what functions are, why we use them, and how to create and call your own functions in Python.

Hey 👋

So far, we’ve been writing our code directly in our programs.

But as programs become bigger, you may find yourself writing the same code again and again.

Wouldn’t it be nice if you could write that code once and use it whenever you need it?

That’s exactly what functions help us do.

What Is a Function?

A function is a reusable block of code that performs a specific task.

For example, if you often need to print a greeting, you could put that code inside a function.

def greet():
    print("Hello!")

Now you can use that function whenever you want:

greet()
greet()
greet()

# Output:

# Hello!
# Hello!
# Hello!

Instead of writing the same print() statement three times, we wrote it once and reused it.

Creating a Function

We use the def keyword to create a function.

The basic syntax is:

def function_name():
    # code to run

For example:

def greet():
    print("Hello!")

Here:

  • def tells Python that we’re creating a function.

  • greet is the name of the function.

  • () comes after the function name.

  • The indented code is the function body.

Calling a Function

Creating a function doesn’t run the code inside it.

To run the function, you need to call it.

def greet():
    print("Hello!")

greet()

# Output:

# Hello!

The line: greet() is what actually runs the code inside the function.

Think of it like this:

  • Define the function → create it

  • Call the function → run it

You can call the same function multiple times:

def greet():
    print("Hello!")

greet()
greet()
greet()

# Output:

# Hello!
# Hello!
# Hello!

What Does a Function Return?

So far, our `greet()` function only prints a message:

def greet():
    print("Hello!")

result = greet()

print(result)

# Output:

# Hello!
# None

Why does result contain None?

Because greet() doesn’t explicitly return a value.

Remember print() from lesson #3? print() also returns None.

The same thing happens with a function that doesn’t have a return statement.

We’ll learn how to make a function return a real value in a later lesson.

Function Names

Just like variables, function names should be descriptive and follow Python’s naming rules.

For example:

def say_hello():
    print("Hello!")

def show_profile():
    print("Profile")

def calculate_total():
    print("Calculating...")

Using a name that describes what the function does makes your code easier to understand.

Functions We’ve Already Used

We’ve actually been using functions throughout this series.

For example:

print("Hello")

Here, print() is a Python built-in function.

We’ve also used:

input("Enter your name: ")

len("Python")

int("10")

These are all built-in functions provided by Python.

We don’t have to create them ourselves.

Today, we’re learning how to create our own user-defined functions.

Built-in vs User-Defined Functions

There are two basic types of functions you’ll come across:

Built-in Functions

These are already provided by Python.

Examples:

print()
input()
len()
int()
float()

User-Defined Functions

These are functions that you create yourself using def.

For example:

def greet():
    print("Hello!")

This is a user-defined function.

A Function Can Contain More Than One Line

A function can contain multiple lines of code.

For example:

def show_profile():
    print("--- Profile ---")
    print("Name: Shefali")
    print("City: Delhi")

Call it:

show_profile()

# Output:

# --- Profile ---
# Name: Shefali
# City: Delhi

All the indented lines belong to the function.

This is another reason functions are useful: you can group related code together.

Indentation Matters

Just like with if statements and loops, indentation tells Python which code belongs to the function.

# Correct:
def greet():
    print("Hello!")

# Incorrect:
def greet():
print("Hello!")

The second example causes an indentation error because the function body isn’t indented.

Why Use Functions?

Functions help you:

  • Reuse code

  • Avoid repeating the same code

  • Break a large program into smaller pieces

  • Keep your code organized

  • Make your code easier to read and maintain

As your programs get bigger, these benefits become more important.

Try It Yourself

Now, a small exercise for you. Try these:

  • Create a function called greet that prints Hello, Python!and call the function.

  • Call your function three times.

  • Create a function called show_message that prints any message you want. Call it a few times.

That’s all for today!

Tomorrow, we’ll take a closer look at parameters and arguments and see how we can pass information to our functions.


If you enjoyed this newsletter, consider sharing it with another developer. And if you have any feedback, I’d love to hear it. It would genuinely mean a lot to me. ❤️


Want to sponsor my work? Let’s talk!

See you tomorrow ✨

- Shefali

Thanks for reading DevBits by Shefali! Subscribe for free to receive new posts and support my work.

If you enjoyed this edition of DevBits, you can support my work here ☕

Buy Me a Coffee

Your support helps me keep creating more developer tips, UI snippets, and helpful content for the community.

© 2026 Shefali Jangid
548 Market Street PMB 72296, San Francisco, CA 94104