Python Functions – Very Beginner Friendly Guide
This guide explains Python functions in the simplest possible way. It is written for absolute
beginners. If you read this carefully and try the examples, you will understand functions clearly.
1. What Is a Function?
A function is a named block of code that performs one task. Instead of writing the same code many
times, you write it once inside a function and use it whenever you need it.
Think of a function like a button on a phone. When you press the button, something happens. The
button is the function call.
2. Why Functions Are Important
Functions help you organize your code.
Functions help you avoid repeating the same code.
Functions make programs easier to read and understand.
Functions help you fix errors in one place.
3. Structure of a Function
Every function in Python follows this structure:
def function_name():
code goes here
The word def means define a function. The function name is the name you choose. The
parentheses are used for input values. The colon means a block of code is starting. The indented
lines are the function body.
4. A Simple Function Example
This function prints a greeting when it is called.
def say_hello():
print("Hello!")
This code only defines the function. Nothing will happen until the function is called.
5. Calling a Function
To run a function, you must call it by writing its name followed by parentheses.
say_hello()
6. Functions With Input Values
Some functions need information to work with. These values are called parameters.
def greet(name):
print("Hello", name)
When you call the function, you give it a value.
greet("Victor")
greet("Alex")
7. Functions With Numbers
Functions can work with numbers and perform calculations.
def add(a, b):
print(a + b)
8. Returning Values From a Function
The return keyword sends a value back to where the function was called.
def add(a, b):
return a + b
result = add(4, 6)
print(result)
9. Indentation Rule in Functions
Indentation is required after the colon. Press Enter and then press Tab once. Indented code
belongs to the function.
def show_age(age):
print("Age:", age)
10. Common Beginner Mistakes
• Defining a function but not calling it.
• Forgetting indentation after the colon.
• Using print when return is needed.
11. Practice Assignments
1 Write a function that prints your name.
2 Write a function that returns the square of a number.
3 Fix a function with missing indentation.
4 Explain what the return keyword does in your own words.
If you can complete these assignments, you understand Python functions.