Python functions explained in a simple way

Once you understand variables, input, conditions, loops, lists, and dictionaries, the next major step in Python is learning how to organize code better. Up to this point, many beginner programs are written as one continuous block from top to bottom. That works for small examples, but as soon as the program becomes longer, repetition and clutter start to appear. The same task may need to be performed several times, and copying the same code again and again quickly becomes inefficient. This is exactly where Python functions become important.

A function is a reusable block of code designed to perform a specific task. Instead of rewriting the same logic in multiple places, you define it once and call it whenever needed. Functions make code cleaner, easier to understand, easier to debug, and easier to expand later. They are one of the core building blocks of real programming, and learning them well is a major step forward for every beginner.

This lesson explains Python functions in a simple, beginner-friendly way. You will learn what a function is, why functions matter, how to define and call them, how parameters and arguments work, what return does, how local variables behave, what common beginner mistakes look like, and why functions are essential in practical Python programming.

What is a function in Python

A function is a named block of code that performs a task.

Basic example:

def say_hello():
print(“Hello”)

This code defines a function called say_hello.

Important parts:

  • def starts the function definition
  • say_hello is the function name
  • parentheses () are required
  • the line ends with a colon :
  • the function body is indented

At this point, the function exists, but it does not run automatically. To use it, you need to call it.

Example:

def say_hello():
print(“Hello”)say_hello()

Output:

Hello

This is the basic idea of a function: define it once, call it when needed.

Why functions matter

Functions matter because they solve several important programming problems at once.

Without functions, code often becomes:

  • repetitive
  • difficult to read
  • harder to debug
  • harder to update
  • harder to reuse

With functions, you can:

  • group related code into one place
  • reuse logic without copying it
  • make code more organized
  • break large tasks into smaller parts
  • test and debug code more easily
  • give meaningful names to repeated operations

For beginners, this is one of the first major lessons in writing cleaner Python rather than just writing code that happens to work.

Defining a function in Python

A function is defined with the def keyword.

Example:

def greet():
print(“Welcome to Python”)

This creates the function, but it still does nothing until you call it.

To run it:

greet()

Output:

Welcome to Python

You can call the same function again:

greet()
greet()

Output:

Welcome to Python
Welcome to Python

This shows one of the biggest advantages of functions: reuse without rewriting the code.

A function does not run when it is defined

This is a very important beginner concept.

When Python reads:

def greet():
print(“Hello”)

it only stores the function definition. It does not execute the print() line immediately.

The code inside the function runs only when you call the function:

greet()

Beginners sometimes expect a function to run as soon as it is defined, but definition and execution are separate steps.

Calling a function

Calling a function means using its name followed by parentheses.

Example:

def show_message():
print(“Python is fun”)show_message()

Output:

Python is fun

If you forget the parentheses:

show_message

the function is not actually called. You are only referring to it.

This is one of the most common beginner mistakes with functions.

Why function names matter

A good function name should describe what the function does.

Good examples:

def calculate_total():
print(“Calculating total”)def print_menu():
print(“Menu”)

Less clear examples:

def do_it():
print(“Something”)def thing():
print(“Something”)

For beginners, descriptive naming is especially helpful because it makes the code easier to read later.

A function name is like a label for a task. A clear label makes the program easier to understand.

Functions can reduce repetition

This is one of the strongest beginner reasons to use functions.

Without a function:

print(“Welcome”)
print(“Please choose an option”)
print(“1. Start”)
print(“2. Exit”)print(“Welcome”)
print(“Please choose an option”)
print(“1. Start”)
print(“2. Exit”)

With a function:

def show_menu():
print(“Welcome”)
print(“Please choose an option”)
print(“1. Start”)
print(“2. Exit”)show_menu()
show_menu()

This version is cleaner and easier to maintain. If the menu changes, you update it in one place instead of multiple places.

Functions with parameters

A function becomes much more useful when it can receive input values. These input values are called parameters.

Example:

def greet(name):
print(“Hello,”, name)

Here, name is a parameter.

Now you can call the function with different arguments:

greet(“Alice”)
greet(“Daniel”)

Output:

Hello, Alice
Hello, Daniel

This is a major step forward because the function is no longer fixed. It can work with different input values.

Parameters versus arguments

Beginners often confuse these two words, so it helps to separate them clearly.

  • a parameter is the variable in the function definition
  • an argument is the actual value passed when calling the function

Example:

def greet(name):
print(“Hello,”, name)greet(“Emma”)

Here:

  • name is the parameter
  • "Emma" is the argument

In everyday beginner practice, people sometimes use these terms loosely, but knowing the difference is still useful.

Functions with multiple parameters

A function can have more than one parameter.

Example:

def introduce(name, age):
print(“Name:”, name)
print(“Age:”, age)

Calling it:

introduce(“Alice”, 25)

Output:

Name: Alice
Age: 25

This is useful when a task needs more than one input value.

Another example:

def add_numbers(a, b):
print(a + b)add_numbers(5, 7)

Output:

12

The order of arguments matters

When passing arguments by position, the order matters.

Example:

def introduce(name, age):
print(name, age)introduce(“Alice”, 25)

This works as expected.

But if you reverse the arguments:

introduce(25, “Alice”)

the meaning changes.

So beginners should remember:

when calling a function, arguments must match the expected order unless you use keyword arguments later

Returning values from a function

So far, the functions have mostly printed results. But very often, a function should produce a value and send it back to the caller. This is what return does.

Example:

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

Now calling the function gives a result back:

result = add(5, 7)
print(result)

Output:

12

This is different from printing inside the function. return makes the result available for later use.

The difference between print() and return

This is one of the most important beginner function concepts.

Example using print()

def add(a, b):
print(a + b)result = add(5, 7)
print(result)

Output:

12
None

Why? Because the function printed the value, but it did not return it. So result becomes None.

Example using return

def add(a, b):
return a + bresult = add(5, 7)
print(result)

Output:

12

Now the result is stored properly.

The rule is simple:

  • use print() to display something
  • use return to send a value back

Why return is so useful

A returned value can be reused in many ways.

Example:

def square(number):
return number ** 2result = square(4)
print(result)
print(result + 10)

Output:

16
26

Because the function returns a value, that value can be:

  • stored in a variable
  • printed
  • used in calculations
  • passed into another function
  • tested in conditions

This makes functions far more flexible.

A function ends when it reaches return

When Python reaches a return statement, the function ends immediately.

Example:

def test():
print(“Start”)
return
print(“End”)

Calling it:

test()

Output:

Start

The last print("End") never runs because the function already ended.

This behavior matters when writing real logic inside functions.

Functions without return return None

If a function does not explicitly return a value, Python returns None automatically.

Example:

def say_hi():
print(“Hi”)result = say_hi()
print(result)

Output:

Hi
None

This is normal behavior. It explains why printing a function result sometimes shows None.

Local variables inside functions

A variable created inside a function is usually local to that function. That means it exists only there.

Example:

def show_number():
number = 10
print(number)show_number()

This works, but outside the function:

print(number)

would cause an error, because number only exists inside the function.

This is called local scope.

For beginners, the main idea is:

variables created inside a function usually stay inside that function

Using global variables inside functions

A function can read a variable from outside if it already exists.

Example:

name = “Alice”

def greet():
print(“Hello,”, name)

greet()

This works because the function can access the outer variable.

However, beginners should be careful. It is usually cleaner to pass values as parameters rather than relying heavily on outside variables.

Cleaner version:

def greet(name):
print(“Hello,”, name)greet(“Alice”)

This is easier to understand and reuse.

Functions can call other functions

One function can call another.

Example:

def say_hello():
print(“Hello”)def welcome_user():
say_hello()
print(“Welcome to the program”)

welcome_user()

Output:

Hello
Welcome to the program

This is useful because larger tasks can be built from smaller reusable tasks.

Default parameter values

A function can define a default value for a parameter.

Example:

def greet(name=“Guest”):
print(“Hello,”, name)

Now you can call it with or without an argument:

greet()
greet(“Emma”)

Output:

Hello, Guest
Hello, Emma

This is useful when a function should work even if the caller does not provide every value.

Keyword arguments

You can also pass arguments by parameter name.

Example:

def introduce(name, age):
print(“Name:”, name)
print(“Age:”, age)introduce(age=25, name=“Alice”)

This works because Python matches the values by keyword instead of by position.

For beginners, keyword arguments are helpful because they improve readability and reduce confusion in some cases.

Functions with input and return together

Functions become especially practical when they combine input, logic, and return.

Example:

def get_square():
number = int(input(“Enter a number: “))
return number ** 2result = get_square()
print(“Square:”, result)

This shows how functions can package a full task:

  • get input
  • process it
  • return the result

That is a very realistic beginner programming pattern.

Real beginner examples with functions

Example 1: Simple greeting

def greet():
print(“Hello”)greet()

Example 2: Greeting with a name

def greet(name):
print(“Hello,”, name)greet(“Alice”)

Example 3: Add two numbers

def add(a, b):
return a + bprint(add(4, 6))

Example 4: Check if a number is even

def is_even(number):
return number % 2 == 0print(is_even(8))
print(is_even(5))

Output:

True
False

Example 5: Print all items from a list

def show_items(items):
for item in items:
print(item)show_items([“apple”, “banana”, “orange”])

These examples show how functions work with text, numbers, lists, and boolean logic.

Common beginner mistakes with Python functions

Several problems appear often when beginners start using functions.

Forgetting to call the function

Example:

def greet():
print(“Hello”)

This defines the function, but nothing happens until you call:

greet()

Forgetting parentheses when calling

Incorrect:

greet

Correct:

greet()

Using print instead of return when a value is needed

Incorrect:

def add(a, b):
print(a + b)result = add(2, 3)
print(result)

This prints 5 and then None.

Correct:

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

Passing the wrong number of arguments

Example:

def greet(name):
print(“Hello,”, name)greet()

This causes an error because the function expects one argument.

Indentation errors

Incorrect:

def greet():
print(“Hello”)

Correct:

def greet():
print(“Hello”)

Using variables outside their local scope

Example:

def make_value():
x = 10make_value()
print(x)

This causes an error because x is local to the function.

Why functions are essential in real Python programs

Functions are one of the most important tools in all of programming because they help structure logic into manageable pieces.

In real Python code, functions are used for:

  • calculations
  • formatting output
  • validating input
  • processing lists
  • handling files
  • connecting to APIs
  • organizing menu actions
  • building reusable program modules

Even a small program becomes much easier to manage when it is divided into clear functions with meaningful names.

That is why functions are not just another syntax topic. They are a major shift in how you think about code.

Good beginner habits with functions

A few habits make function-based code much cleaner.

Give functions clear names

The function name should describe the task.

Keep functions focused

A function should usually do one clear job rather than many unrelated things.

Use return when the result needs to be reused

If the caller needs the value, return it instead of only printing it.

Use parameters instead of relying too much on outside variables

This makes the function easier to reuse and easier to test.

Test functions with small examples

When learning, call the function with simple values first so that the behavior is easy to understand.

What you should understand before moving on

Before continuing to the next lesson, you should be comfortable with the following:

  • a function is a reusable block of code
  • def is used to define a function
  • a function runs only when it is called
  • parameters receive input values
  • arguments are the actual values passed in
  • return sends a value back to the caller
  • print() displays output, but does not replace return
  • variables inside a function are usually local
  • good function names improve readability
  • functions help reduce repetition and organize code

If these ideas are clear, the next lesson on basic error handling with try and except will be much easier, because functions and error handling are often used together in practical Python code.

A Python function is a reusable block of code that performs a specific task. For beginners, functions are one of the most important steps in moving from simple scripts to more organized programming. They reduce repetition, improve readability, and make code easier to maintain, test, and expand.

Functions become especially powerful when they use parameters and return values. Once you understand how to define, call, and reuse functions, you have a much stronger foundation for building practical Python programs of any size.


Image(s) used in this article are either AI-generated or sourced from royalty-free platforms like Pixabay or Pexels.

This article may contain affiliate links. If you purchase through these links, we may earn a commission at no extra cost to you.

Weekly briefing

Get the weekly RF & IT briefing

Radio guides, RF calculators, AI, Windows, Linux and satellite communication explainers. One useful email per week. No spam.

Similar Posts