No history yet

Function Basics

Reusable Blocks of Code

Imagine you have a task you need to do over and over again, like making a cup of coffee every morning. You wouldn't build a new coffee machine from scratch each day. You use the same machine, press a button, and it performs a series of steps for you. In programming, a function is like that coffee machine. It's a named, reusable block of code that performs a specific task.

Functions let you write code once and run it whenever you need it, just by calling its name. This keeps your programs organized, shorter, and easier to fix.

function

noun

A block of organized, reusable code that is used to perform a single, related action.

To create a function in Python, you use the def keyword, which is short for "define." You give your function a name, followed by parentheses () and a colon :. Any code that belongs to the function is indented on the lines below it.

def say_hello():
    # This code is inside the function
    print("Hello there!")

# This code is outside the function
say_hello() # This is how you call the function

The name of the function is important. It should be descriptive and follow Python's standard naming style, which is snake_case—all lowercase with underscores separating words. A name like calculate_tax is much clearer than ct or function1.

Passing Information to Functions

What if you want your coffee machine to make different kinds of coffee? You'd need to give it different inputs, like espresso beans instead of regular coffee grounds. Functions can also accept inputs. These inputs are called parameters.

parameter

noun

A special variable listed inside the parentheses in a function definition, acting as a placeholder for a value that will be provided when the function is called.

You define parameters by putting variable names inside the function's parentheses. When you call the function, you provide actual values, called arguments, for those parameters.

# 'name' is a parameter
def greet(name):
    print(f"Hello, {name}!")

# 'Alice' is the argument passed to the 'name' parameter
greet("Alice")

# 'Bob' is the argument passed to the 'name' parameter
greet("Bob")

This function is now much more flexible. We can greet anyone we want just by passing a different name when we call it.

Getting Information Back

Sometimes, a function needs to give a result back to the code that called it. A coffee machine's job isn't just to brew; it's to give you coffee. To send a value back, functions use a return statement.

When Python sees a return statement, it immediately exits the function and sends the specified value back.

Let's make a function that adds two numbers. It takes two parameters and returns their sum. We can then store this result in a variable.

def add_numbers(num1, num2):
    # Calculate the sum
    result = num1 + num2
    # Return the result
    return result

# Call the function and store the returned value
sum_of_numbers = add_numbers(5, 3)

print(sum_of_numbers) # This will print 8

The add_numbers function doesn't print anything itself. It just does the calculation and hands the answer back. This is very powerful because it lets us use the output of one function as the input for another, building more complex programs from simple, reliable pieces.

Time to check your understanding of function basics.

Quiz Questions 1/6

What is the primary purpose of a function in programming?

Quiz Questions 2/6

Which keyword is used to start a function definition in Python?

By defining, calling, and returning values from functions, you can create code that is far more powerful and easier to manage.