What Is a Function in Python?

A function in Python is a reusable block of code that does a specific task. You can run it when needed, and it can optionally send a value back.

Functions are one of the main ways to organize code. They help you group related steps together and avoid writing the same code again and again.

Simple definition

A function is:

  • A named block of code
  • A way to group steps that belong together
  • Something you can run more than once
  • A tool for avoiding repeated code

Here is a very simple example:

def say_hello():
    print("Hello")

say_hello()

Output:

Hello

This example shows the two main parts:

  • Defining a function with def
  • Calling the function by writing its name with parentheses

The function does not run when Python first sees def say_hello():. It runs only when you call say_hello().

Why functions are useful

Functions are useful because they help you:

  • Make code easier to read
  • Reduce repetition
  • Break large problems into smaller parts
  • Reuse the same logic in different places

For example, without a function, you might repeat the same lines several times. With a function, you write the code once and call it whenever you need it.

How a function works

A basic function works like this:

  • You define a function with def
  • You give the function a name
  • You put the code for that task inside the function
  • The indented code runs when the function is called

Example:

def show_message():
    print("This is inside the function")

show_message()

Output:

This is inside the function

Key parts:

  • def starts the function definition
  • show_message is the function name
  • () are used in both the definition and the call
  • The indented line is the function body

If you want a fuller introduction, see Python functions explained.

Functions can take input

Functions can work with different data by taking input.

  • Inputs in the function definition are called parameters
  • Values you pass when calling the function are called arguments
  • This lets one function work with different values

Example:

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

greet("Maya")
greet("Leo")

Output:

Hello, Maya
Hello, Leo

Here:

  • name is a parameter
  • "Maya" and "Leo" are arguments

This is useful because you do not need a separate function for every person or value.

Functions can return output

A function can also send a value back using return.

  • return gives a value back to the program
  • The returned value can be stored in a variable
  • Not every function needs return
  • print() shows something on the screen, but return gives a value back to your code

Example:

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

result = add_numbers(2, 3)
print(result)

Output:

5

In this example:

  • The function calculates a + b
  • return a + b sends the result back
  • That result is stored in result

This is different from:

def add_and_print(a, b):
    print(a + b)

value = add_and_print(2, 3)
print(value)

Output:

5
None

Why does None appear? Because the function printed the result, but it did not return one.

If you want to learn this idea in more detail, see what is a return value in Python.

Built-in functions and your own functions

Python already includes many built-in functions, such as:

  • print()
  • len()
  • input()

Example:

text = "Python"
print(len(text))

Output:

6

You can also create your own functions with def.

Both built-in functions and your own functions are usually called with parentheses. For example:

  • print("Hi")
  • len("cat")
  • say_hello()

If you want to understand one common built-in function better, see Python print() function explained.

What this page does not cover in depth

This page explains the basic idea of a function. It does not go deeply into:

  • Detailed parameter rules
  • Default arguments and keyword arguments
  • Lambda functions
  • Advanced function concepts

If your next goal is to make one yourself, see how to create a simple function in Python.

Common mistakes

Beginners often run into these problems when learning functions:

  • Thinking a function runs as soon as it is defined
  • Forgetting to call the function after defining it
  • Confusing print() with return
  • Forgetting the parentheses when calling a function
  • Using wrong indentation inside the function body

Example of a function that is defined but not called:

def say_hello():
    print("Hello")

This code creates the function, but it does not produce output yet. To run it, you must call it:

def say_hello():
    print("Hello")

say_hello()

You can also use a few quick checks while learning:

print(my_function)
print(my_function())
help(print)
type(print)
type(my_function)

What these show:

  • print(my_function) shows that the function exists
  • print(my_function()) calls the function and prints its return value
  • help(print) shows help for the built-in print() function
  • type(print) shows that print is a built-in function
  • type(my_function) shows that your function is a function object

Be careful with print(my_function()): it runs the function. If the function only prints something and does not return a value, you may also see None.

FAQ

What is a function in simple words?

A function is a reusable set of instructions that performs a task.

What is the difference between defining and calling a function?

Defining creates the function. Calling runs it.

Does every function need a return statement?

No. Some functions only perform an action, such as printing output.

Is print() a function?

Yes. print() is a built-in Python function.

Why should beginners use functions?

Functions make code easier to reuse, organize, and understand.

See also

Next step: learn how to create your own function, pass values into it, and get a result back.