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:
print(“Hello”)
This code defines a function called say_hello.
Important parts:
defstarts the function definitionsay_hellois 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:
print(“Hello”)say_hello()
Output:
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:
print(“Welcome to Python”)
This creates the function, but it still does nothing until you call it.
To run it:
Output:
You can call the same function again:
greet()
Output:
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:
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:
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:
print(“Python is fun”)show_message()
Output:
If you forget the parentheses:
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:
print(“Calculating total”)def print_menu():
print(“Menu”)
Less clear examples:
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(“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:
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:
print(“Hello,”, name)
Here, name is a parameter.
Now you can call the function with different arguments:
greet(“Daniel”)
Output:
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:
print(“Hello,”, name)greet(“Emma”)
Here:
nameis 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:
print(“Name:”, name)
print(“Age:”, age)
Calling it:
Output:
Age: 25
This is useful when a task needs more than one input value.
Another example:
print(a + b)add_numbers(5, 7)
Output:
The order of arguments matters
When passing arguments by position, the order matters.
Example:
print(name, age)introduce(“Alice”, 25)
This works as expected.
But if you reverse the arguments:
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:
return a + b
Now calling the function gives a result back:
print(result)
Output:
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()
print(a + b)result = add(5, 7)
print(result)
Output:
None
Why? Because the function printed the value, but it did not return it. So result becomes None.
Example using return
return a + bresult = add(5, 7)
print(result)
Output:
Now the result is stored properly.
The rule is simple:
- use
print()to display something - use
returnto send a value back
Why return is so useful
A returned value can be reused in many ways.
Example:
return number ** 2result = square(4)
print(result)
print(result + 10)
Output:
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:
print(“Start”)
return
print(“End”)
Calling it:
Output:
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:
print(“Hi”)result = say_hi()
print(result)
Output:
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:
number = 10
print(number)show_number()
This works, but outside the function:
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:
print(“Hello,”, name)greet(“Alice”)
This is easier to understand and reuse.
Functions can call other functions
One function can call another.
Example:
print(“Hello”)def welcome_user():
say_hello()
print(“Welcome to the program”)
welcome_user()
Output:
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:
print(“Hello,”, name)
Now you can call it with or without an argument:
greet(“Emma”)
Output:
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:
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:
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
print(“Hello”)greet()
Example 2: Greeting with a name
print(“Hello,”, name)greet(“Alice”)
Example 3: Add two numbers
return a + bprint(add(4, 6))
Example 4: Check if a number is even
return number % 2 == 0print(is_even(8))
print(is_even(5))
Output:
False
Example 5: Print all items from a list
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:
print(“Hello”)
This defines the function, but nothing happens until you call:
Forgetting parentheses when calling
Incorrect:
Correct:
Using print instead of return when a value is needed
Incorrect:
print(a + b)result = add(2, 3)
print(result)
This prints 5 and then None.
Correct:
return a + b
Passing the wrong number of arguments
Example:
print(“Hello,”, name)greet()
This causes an error because the function expects one argument.
Indentation errors
Incorrect:
print(“Hello”)
Correct:
print(“Hello”)
Using variables outside their local scope
Example:
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
defis used to define a function- a function runs only when it is called
- parameters receive input values
- arguments are the actual values passed in
returnsends a value back to the callerprint()displays output, but does not replacereturn- 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.
Get the weekly RF & IT briefing
Radio guides, RF calculators, AI, Windows, Linux and satellite communication explainers. One useful email per week. No spam.
