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.
💡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

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
Instead of repeating all steps every time, we can give the whole task a name:
Example
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:
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:
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:
def function_name():
work_to_do
Example:
def say_hello():
print("Hello!")
Simple explanation:
defstarts a function in Python.say_hellois 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:
def say_hello():
print("Hello!")
say_hello()
Result:
Example
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.
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
5. A function can receive information
Many functions need information before they can work.
Example: greeting a person by name.
def greet(name):
print("Hello, " + name + "!")
greet("An")
greet("Binh")
Result:
Example
Here, name is a place to receive a name.
When we call:
greet("An")
Python understands that name is "An".
Simple way to think about it:
Example
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:
def add(a, b):
return a + b
result = add(3, 5)
print(result)
Result:
Example
Explanation:
areceives3.breceives5.- The function calculates
3 + 5. returngives the result back.resultstores8.
Simple meaning:
Example
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:
def add(a, b):
print(a + b)
add(3, 5)
Displayed result:
Example
Using return:
def add(a, b):
return a + b
result = add(3, 5)
print(result * 2)
Result:
Example
Because the function gives back 8, the program can use that result again.
Easy memory tip:
print = show it to the person.
return = give it back to the program.
8. Real example: calculating total price
def calculate_total(price, quantity):
return price * quantity
total = calculate_total(25000, 3)
print(total)
Result:
Example
Explanation:
priceis the price of one item.quantityis how many items are bought.- The function multiplies price by quantity.
- The result is the total price.
To calculate other items:
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
def check_age(age):
if age >= 18:
return "Adult"
else:
return "Not adult yet"
print(check_age(20))
print(check_age(15))
Result:
Example
This function works like an age checker:
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:
def x(a, b):
return a + b
Clearer:
def add(a, b):
return a + b
Beginner-friendly names:
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
Split it into smaller tasks:
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
def say_hello():
print("Hello!")
This does not print anything yet. You need to call the function:
say_hello()
Mistake 2: Forgetting indentation
Wrong:
def say_hello():
print("Hello!")
Correct:
def say_hello():
print("Hello!")
Mistake 3: Confusing print and return
If you want to use the result later, use return.
def add(a, b):
return a + b
If you only want to show something, use print.
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:
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:
- What is the function name?
- What does it receive?
- What does it do?
- Does it return a result?
Example:
def add(a, b):
return a + b
Answers:
Example
14. Small exercises
Exercise 1
Write a function named say_hello() that prints:
Example
Exercise 2
Write a function named greet(name) that greets a person by name.
Example:
greet("An")
Result:
Example
Exercise 3
Write a function named add(a, b) that returns the sum of two numbers.
Example:
print(add(4, 6))
Result:
Example
Exercise 4
Write a function named calculate_total(price, quantity) that returns the total price.
Example:
print(calculate_total(20000, 3))
Result:
Example
15. Suggested answers
Answer 1
def say_hello():
print("Hello!")
say_hello()
Answer 2
def greet(name):
print("Hello, " + name + "!")
greet("An")
Answer 3
def add(a, b):
return a + b
print(add(4, 6))
Answer 4
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
- Python Docs — Defining Functions: https://docs.python.org/3/tutorial/controlflow.html#defining-functions
- MDN Web Docs — JavaScript Functions: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Functions
- Harvard CS50’s Introduction to Programming with Python: https://cs50.harvard.edu/python/
- Wikimedia Commons — programming image: https://commons.wikimedia.org/wiki/File:Colorful_code_(Unsplash).jpg
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.