Programming Basics

What Is a Function in Programming? A Simple Article for Beginners

In programming, a function is a group of instructions with a name. This beginner-friendly guide explains why functions are useful, how to create and call them in Python, and common beginner mistakes.

Published: Jun 8, 2026Updated: Jun 8, 2026Reading time: 5 minViews: 0
functionsPythonbeginnersprogramming

💡Key Takeaways

  • In programming, a function is a group of instructions with a name.
  • This beginner-friendly guide explains why functions are useful, how to create and call them in Python, and common beginner mistakes.

Topic: basic programming knowledge
Audience: people who are new to programming
Level: beginner-friendly, minimal technical terms
Example language: Python
Image format: internet-hosted JPEG image, no base64 embedding, no SVG

Programming code illustration
Programming code illustration

Image source: Wikimedia Commons — Colorful code (Unsplash).jpg.
Photo author: Markus Spiske.
License: CC0 1.0 Public Domain Dedication according to the Wikimedia Commons page.
Original image page: https://commons.wikimedia.org/wiki/File:Colorful_code_(Unsplash).jpg
Direct display image URL: https://upload.wikimedia.org/wikipedia/commons/thumb/9/94/Colorful_code_%28Unsplash%29.jpg/1280px-Colorful_code_%28Unsplash%29.jpg

1. What is a function?

In programming, a function is simply a group of instructions with a name.

Real-life example: you make tea every day. Making tea has several steps:

Example

Get a cup. Put tea in the cup. Pour hot water. Wait a few minutes. Drink the tea.

Instead of repeating all steps every time, we can give the whole task a name:

Example

make_tea

After that, when we say “make_tea,” we understand that there are several smaller steps inside.

Programming works in a similar way. We group several lines of code, give that group a name, and use the name when needed.

2. Why do we need functions?

Functions make code less repetitive, easier to read, and easier to fix.

Without a function:

PYTHON
print("Hello, An!")
print("Have a nice day!")

print("Hello, Binh!")
print("Have a nice day!")

print("Hello, Chi!")
print("Have a nice day!")

This works, but it repeats the same idea many times.

With a function:

PYTHON
def greet(name):
    print("Hello, " + name + "!")
    print("Have a nice day!")

greet("An")
greet("Binh")
greet("Chi")

Now the greeting is written only once. To greet someone, call greet() and put the person’s name inside.

3. Creating a simple function

In Python, a simple function looks like this:

PYTHON
def function_name():
    work_to_do

Example:

PYTHON
def say_hello():
    print("Hello!")

Simple explanation:

  • def starts a function in Python.
  • say_hello is the function name.
  • The line below is what the function does.
  • The code inside the function must be indented.

Creating a function does not run it immediately. To run it, you must call it:

PYTHON
def say_hello():
    print("Hello!")

say_hello()

Result:

Example

Hello!

4. What does calling a function mean?

Calling a function means asking it to start working.

Think of a button named “turn on the light.” Before you press it, the light is off. When you press it, the light turns on.

PYTHON
def turn_on_light():
    print("The light is on")

turn_on_light()

def turn_on_light() creates the button.
turn_on_light() presses the button.

Result:

Example

The light is on

5. A function can receive information

Many functions need information before they can work.

Example: greeting a person by name.

PYTHON
def greet(name):
    print("Hello, " + name + "!")

greet("An")
greet("Binh")

Result:

Example

Hello, An! Hello, Binh!

Here, name is a place to receive a name.

When we call:

PYTHON
greet("An")

Python understands that name is "An".

Simple way to think about it:

Example

The greet function is like a sentence template. You put a name into it. The function builds the greeting.

6. A function can return a result

Some functions only show text on the screen. Other functions calculate something and give the result back.

Example:

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

result = add(3, 5)

print(result)

Result:

Example

8

Explanation:

  • a receives 3.
  • b receives 5.
  • The function calculates 3 + 5.
  • return gives the result back.
  • result stores 8.

Simple meaning:

Example

You put 3 and 5 in. The function adds them. The function gives back 8.

7. What is the difference between print and return?

Beginners often confuse these two.

print means show something on the screen.

return means give a result back to the program so it can use the result later.

Using print:

PYTHON
def add(a, b):
    print(a + b)

add(3, 5)

Displayed result:

Example

8

Using return:

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

result = add(3, 5)
print(result * 2)

Result:

Example

16

Because the function gives back 8, the program can use that result again.

Easy memory tip:

TEXT
print = show it to the person.
return = give it back to the program.

8. Real example: calculating total price

PYTHON
def calculate_total(price, quantity):
    return price * quantity

total = calculate_total(25000, 3)

print(total)

Result:

Example

75000

Explanation:

  • price is the price of one item.
  • quantity is how many items are bought.
  • The function multiplies price by quantity.
  • The result is the total price.

To calculate other items:

PYTHON
print(calculate_total(10000, 5))
print(calculate_total(45000, 2))
print(calculate_total(120000, 1))

No need to write the same formula many times.

9. Real example: checking age

PYTHON
def check_age(age):
    if age >= 18:
        return "Adult"
    else:
        return "Not adult yet"

print(check_age(20))
print(check_age(15))

Result:

Example

Adult Not adult yet

This function works like an age checker:

TEXT
Put in an age.
If the age is 18 or more, return "Adult".
If the age is below 18, return "Not adult yet".

10. How to name a function

A function name should clearly say what the function does.

Unclear:

PYTHON
def x(a, b):
    return a + b

Clearer:

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

Beginner-friendly names:

PYTHON
greet_user()
add_numbers()
check_age()
calculate_total()
send_email()

For beginners, clear names are better than short names.

11. When should you create a function?

Create a function when:

  • you write the same code many times;
  • you want to name one clear task;
  • you want code to be easier to read;
  • you want to fix one place instead of many places;
  • you want to split a big task into small tasks.

Instead of thinking:

Example

Build a sales program.

Split it into smaller tasks:

TEXT
Calculate total.
Calculate discount.
Print receipt.
Check stock.
Save customer information.

Each small task can become a function.

12. Common beginner mistakes

Mistake 1: Creating a function but not calling it

PYTHON
def say_hello():
    print("Hello!")

This does not print anything yet. You need to call the function:

PYTHON
say_hello()

Mistake 2: Forgetting indentation

Wrong:

PYTHON
def say_hello():
print("Hello!")

Correct:

PYTHON
def say_hello():
    print("Hello!")

Mistake 3: Confusing print and return

If you want to use the result later, use return.

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

If you only want to show something, use print.

PYTHON
def show_greeting():
    print("Hello!")

Mistake 4: Making one function do too many things

Do not make one function calculate money, send email, print a receipt, and save data all at once.

Use smaller functions:

PYTHON
calculate_total()
create_receipt()
send_email()
save_data()

One clear job per function is easier to understand.

13. How to read a function

When you see a function, ask:

  1. What is the function name?
  2. What does it receive?
  3. What does it do?
  4. Does it return a result?

Example:

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

Answers:

Example

Name: add Receives: a and b Does: adds a and b Returns: yes, returns a + b

14. Small exercises

Exercise 1

Write a function named say_hello() that prints:

Example

Hello!

Exercise 2

Write a function named greet(name) that greets a person by name.

Example:

PYTHON
greet("An")

Result:

Example

Hello, An!

Exercise 3

Write a function named add(a, b) that returns the sum of two numbers.

Example:

PYTHON
print(add(4, 6))

Result:

Example

10

Exercise 4

Write a function named calculate_total(price, quantity) that returns the total price.

Example:

PYTHON
print(calculate_total(20000, 3))

Result:

Example

60000

15. Suggested answers

Answer 1

PYTHON
def say_hello():
    print("Hello!")

say_hello()

Answer 2

PYTHON
def greet(name):
    print("Hello, " + name + "!")

greet("An")

Answer 3

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

print(add(4, 6))

Answer 4

PYTHON
def calculate_total(price, quantity):
    return price * quantity

print(calculate_total(20000, 3))

16. Quick memory notes

A function is a named group of instructions.

Creating a function prepares a task for reuse.

Calling a function makes it run.

A function can receive information.

A function can return a result with return.

print shows something to a person. return gives a result back to the program.

Function names should be clear and easy to understand.

17. What should you learn next?

After understanding functions, beginners can learn:

  • how to split a small program into several functions;
  • how to read errors when calling a function incorrectly;
  • how to use lists together with functions;
  • how to write a complete small program.

Do not rush. The key idea is simple: a function groups one task, gives that task a name, and lets you reuse it when needed.

References

  1. Python Docs — Defining Functions: https://docs.python.org/3/tutorial/controlflow.html#defining-functions
  2. MDN Web Docs — JavaScript Functions: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Functions
  3. Harvard CS50’s Introduction to Programming with Python: https://cs50.harvard.edu/python/
  4. Wikimedia Commons — programming image: https://commons.wikimedia.org/wiki/File:Colorful_code_(Unsplash).jpg
PR

Written by PixelRouter Editorial Team

We publish deep, authoritative guides on AI infrastructure, API gateway security, cloud financial management, and system optimizations for developers.

FAQ

What is a function in programming?

A function is a named group of instructions that can be called to perform a specific task, similar to giving a set of steps a name like make_tea.

Why should I use functions?

Functions reduce repetition, make code easier to read and maintain, and allow you to fix logic in one place instead of many.

How do I create a simple function in Python?

Use the `def` keyword followed by the function name and parentheses, then indent the code block. Example: ```python def say_hello(): print("Hello!") ```

What does calling a function mean?

Calling a function is asking it to run by writing its name followed by parentheses, e.g., `say_hello()`.

How can a function receive information?

A function can have parameters inside the parentheses, like `def greet(name):`, and you pass values when you call it, e.g., `greet("An")`.

What is the difference between `print` and `return`?

`print` displays a value to the screen, while `return` sends a value back to the program so it can be used later.