Getting Started with Python Functions

Learn Python functions the easy way. Follow simple examples for adding numbers, doubling values, and greeting users while keeping your code clean and reusable.

Getting Started with Python Functions

Welcome to the world of Python! One of the first building blocks you’ll encounter as you learn this versatile language is the function. Functions let you package up reusable pieces of code, making your programs cleaner, easier to read, and—most importantly—simpler to maintain. In this post, we’ll walk through three simple but powerful examples:

  1. Adding Two Numbers
  2. Doubling a Value
  3. Greeting a User

By the end, you’ll see how easy it is to define your own functions and start writing more organized Python code.


1. Adding Two Numbers

Let’s begin with the classic “hello world” of arithmetic: summing two values. We’ll create a function called add_numbers that takes two arguments and returns their sum.

def add_numbers(a, b):
    """
    Returns the sum of a and b.
    """
    return a + b

# Example usage:
result = add_numbers(3, 7)
print(f"The sum of 3 and 7 is {result}")
# Output: The sum of 3 and 7 is 10

What’s happening here?

  • def add_numbers(a, b):
    Declares a new function named add_numbers that expects two parameters, a and b.
  • Docstring
    The triple-quoted string immediately after the function header describes what the function does.
  • return a + b
    Sends the result back to wherever the function was called.

You can now call add_numbers with any two numbers—integers, floats, even complex types (e.g., NumPy arrays).


2. Doubling a Value

Next up is a function named double_it that takes a single argument and returns twice its value. This demonstrates how simple transformations can be encapsulated in reusable functions.

def double_it(x):
    """
    Returns twice the input value.
    """
    return x * 2

# Example usage:
value = 5
doubled = double_it(value)
print(f"{value} doubled is {doubled}")
# Output: 5 doubled is 10

Why use double_it?

  • Clarity: Anyone reading your code sees immediately that you’re doubling something.
  • Reusability: If your doubling logic grows more complex (e.g., type-checking, logging), you only need to update it in one place.
  • Abstraction: Higher-level code can work with functions like double_it without worrying about the multiplication details.

3. Greeting a User

Finally, let’s create a user-friendly function called greeting. It will accept a person’s name and optionally their title, then print a personalized greeting.

def greeting(name, title=""):
    """
    Prints a friendly greeting.  
    If title is provided, it’s included in the message.
    """
    if title:
        print(f"Hello, {title} {name}! Welcome to Python.")
    else:
        print(f"Hello, {name}! Welcome to Python.")


# Example usage:
greeting("Alice")
# Output: Hello, Alice! Welcome to Python.

greeting("Smith", title="Dr.")
# Output: Hello, Dr. Smith! Welcome to Python.

Key takeaways:

  • Default parameters
    We set title="", so callers can omit it if they don’t need a title.
  • Conditional logic
    The if title: block shows how you can branch behavior inside a function.
  • print vs. return
    Here we choose to print directly, since our goal is to display text. If you wanted to reuse the greeting string elsewhere, you could return it instead.

Wrapping Up

Functions are the heart and soul of Python coding. By defining clear, well-named functions like add_numbersdouble_it, and greeting, you:

  • Encapsulate logic in one place
  • Make your code more readable
  • Enable easy testing and future enhancements

Now it’s your turn! Try creating your own functions—perhaps one to compute the square of a number, or a tiny calculator that chains multiple operations together.

To learning more about Python, please check out our book. Link below: