Introduction to Python Functions, Arguments, and Return Values

Learning Python for Beginners — Part 9

By Data Impala

Welcome back to the Data Impala Python learning series. 

In the last article, we talked about Python modules and packages, and how they help you organize and reuse your code more efficiently. 

Now it’s time to zoom into one of the most important concepts in Python: Functions.

You’ve seen some built-in ones like print() and len()

Now it’s time to learn how to make your own, pass values to them, and get results back. 

Let’s break it all down, step by step.

What Is a Function?

A function is a named block of code that you can call whenever you need it. 

You define the logic once, and then you can reuse it as many times as you want.

Here’s what a python function looks like:

# Define a function to add two numbers

def add(x, y):
return x + y  # Return the sum of x and y

# Use the functions
a = 10
b = 5
print("Addition:", add(a, b))     # 10 + 5 = 15

Why Are Functions Useful?

  • They reduce repetition: You don’t need to write the same code again and again.
  • They make code easier to read and debug: You can focus on smaller chunks instead of reading through long scripts.
  • They help you reuse logic: You can apply the same function in different parts of your program.

How to Create a Function

To define a function in Python, you use the def keyword. Here’s what a simple function looks like:

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

This function is named greet. When you call greet(), it prints out a message.

Anatomy of a Function

  1. def: Tells Python you’re defining a new function.
  1. greet: This is the function’s name.
  1. () (parentheses): This is where you’ll later pass arguments (data).
  1. :(colon): Signals the start of the function body.
  1. The indented block: The part of the code that runs when the function is called.

Adding Arguments to a Function

Arguments are values you pass to a function so it can use them to do its job.

Example:

def greet_user(name):
    print("Hello,", name)

Now you can call it with:

greet_user("Mahi")  # Output: Hello, Mahi

You can also use more than one argument:

def introduce(name, age):
    print(name, "is", age, "years old.")

Calling introduce(“Amit”, 22) gives:

Amit is 22 years old.

Default Argument Values

You can assign a default value to an argument so the function still works even if the user doesn’t pass that argument.

def greet_user(name="Guest"):
    print("Welcome,", name)

Now:

greet_user() → “Welcome, Guest”

greet_user(“Anika”) → “Welcome, Anika”

This makes your functions more flexible.

What Is a Return Value?

Sometimes you want a function to give back a result that you can store and use later. 

This is done using the return keyword.

def add(a, b):
    return a + b

Now if you write:

result = add(3, 5)
print(result)  # Output: 8

Why Use ‘return’ Instead of ‘print’?

  • print() just shows the result.
  • return gives you the result to use later in your code.
  • You can store it, modify it, or use it in calculations.

Positional vs Keyword Arguments

Positional:

Arguments are matched based on their order.

introduce("Rafi", 29)

Keyword:

You mention the argument names.

introduce(age=29, name="Rafi")

This is clearer and avoids confusion in case of long or multiple arguments.

Variable Number of Arguments

Sometimes, you don’t know how many values a user will pass.

Python has two ways to handle this.

1. *args – For many positional arguments

def add_all(*numbers):
    return sum(numbers)

Now you can pass any number of arguments like add_all(1, 2, 3, 4).

2. **kwargs – For many keyword arguments

def show_details(**info):
    for key, value in info.items():
        print(key, ":", value)

You can now pass details like show_details(name=”Afsana”, age=24).

Functions Inside Functions

You can also call one function from inside another. This helps break down problems into smaller parts.

def multiply(x, y):
    return x * y

def double_and_add(x, y):
    product = multiply(x, y)
    return product + 10

Naming Tips and Best Practices

  • Choose names that describe what the function does (calculate_total, not ct).
  • Add comments to explain what your function does.
  • Keep functions short. One function should do one thing.
  • Avoid using or changing global variables inside functions. Always use arguments and return values.

Mini Test: Filter Even Numbers

Let’s put what we learned in to use.

Write a function that:

  1. Takes a list of numbers.
  2. Returns a new list containing only the even numbers.
  3. Returns an empty list if the input list is empty.

Solution:

def filter_even(numbers):
    even_list = []  # Create an empty list to store even numbers
    for num in numbers:
        if num % 2 == 0:  # If the number is divisible by 2, it's even
            even_list.append(num)  # Add the even number to the list
    return even_list  # Send the filtered list back

Example Use:

sample = [1, 2, 3, 4, 5, 6]
result = filter_even(sample)
print(result)  # Output: [2, 4, 6]

Why It Works:

  • You go through each number one by one.
  • Use % to check if it’s even.
  • Collect those into a new list and return it.

Functions are the core of writing clean, manageable Python code.

You can make your programs smarter, more flexible, and easier to maintain by using arguments and return values the right way.

Coming up next in the series, we’ll learn about List Comprehension and advanced list tricks in Python.

Stay sharp and keep building.

✍️ Written by Ahnaf Chowdhury

📘 Series: Learning Python for Beginners — Part 9

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top