Functions are one of the most important building blocks in Python. If you’ve ever used print(), you’ve already used a function. In this tutorial, you’ll learn what functions are, how to call them, how to define your own functions, and what it means when a function “returns” a value—or returns None.
A function is a reusable block of code that performs a specific task. You give it some input, it does something with that input, and it may produce an output.
Think about this idea in programming terms: you pass values into a function (inputs), and the function produces a result (output). This simple model helps you write code that is easier to read, test, and reuse.
[ A simple diagram showing input → function → output]
print() as your first exampleA function call looks like a name followed by parentheses. Inside the parentheses, you provide the inputs (also called arguments).
Here’s a simple function call using print():
print("Hello, world!")
Code language: PHP (php)
In this call, the function name is print, the parentheses () hold the arguments, and the argument value is the string "Hello, world!". When Python runs this line, print() writes the text to the screen.
Output of a simple print statement]
Before you write your own functions, it helps to understand two terms that often get mixed up.
Parameters are the variable names listed inside the function definition. They are placeholders that will receive values.
Arguments are the actual values you pass when calling the function.
So, parameters live in the function definition, and arguments live in the function call.
defPython lets you define your own functions using the def keyword (short for “definition”). Function naming rules are the same as variable naming rules. A function name should be descriptive, should not start with a number, and should typically use lowercase letters with underscores.
Here is a function that multiplies a value by 3:
def multiply_by_three(val):
return 3 * val
Code language: JavaScript (javascript)
This function definition has a few important parts.
The first line starts with def, followed by the function name multiply_by_three. Inside the parentheses, we define one parameter named val. The colon : indicates that a block of code follows, and that block must be indented.
Inside the block, we use return to send back the computed value. In this case, it returns 3 * val.
Now you can call the function like this:
result = multiply_by_three(4)
print(result)
Code language: PHP (php)
When you call multiply_by_three(4), the argument 4 is assigned to the parameter val, the function computes 3 * 4, and then returns 12.
[ Calling a function and seeing the result]
returnThe return keyword ends the function and sends a value back to the caller. That returned value can be printed, stored in a variable, used in an expression, or passed to another function.
Here’s what returned values enable:
x = multiply_by_three(10)
# x is now 30
final = x + 5
# final is now 35
Code language: PHP (php)
Without return, you wouldn’t be able to easily reuse the output of a function in other parts of your program.
Many functions need more than one input. You can add multiple parameters by separating them with commas.
Here’s a function that multiplies two values:
def multiply(val_one, val_two):
return val_one * val_two
Code language: JavaScript (javascript)
Calling it is straightforward:
answer = multiply(3, 4)
print(answer)
Code language: PHP (php)
The function returns 12, because 3 * 4 equals 12.
[ Function with two parameters]
Not every function needs to return a value. Sometimes the goal of a function is to perform an action rather than compute a result.
A common example is a function that changes (mutates) an object such as a list.
a = [1, 2, 3]
def append_four(my_list):
my_list.append(4)
append_four(a)
print(a)
Code language: PHP (php)
After running this code, the list becomes [1, 2, 3, 4]. Notice what’s happening: the function modifies the existing list object. That modification is the “result,” even though the function is not returning anything useful.
This is an important concept in Python. Some functions create and return new values, while others perform side effects such as printing, writing to a file, updating a list, or saving data.
[ Before/after list output]
None?In Python, if a function does not explicitly return a value, it returns None automatically.
You can see this with print(). It displays text, but it does not produce a usable return value.
value = print("This shows on the screen")
print(value)
Code language: PHP (php)
The first line prints the message. The second line prints the value stored in value, which will be None.
None is a special Python keyword that represents the absence of a value. Many other languages have a similar idea, such as null or undefined.
[Showing that print returns None]
None can cause errors if you treat it like a real valueNone is not a number, not a string, and not something you can do math with. If you accidentally assume you received a real value but you actually received None, your program will crash.
For example:
x = None
print(x + 1)
Code language: PHP (php)
This fails because Python cannot add an integer to None. That’s why it’s a good habit to pay attention to what a function returns, especially when you are chaining operations or assigning results to variables.
In real projects, None is often used intentionally to mean “no result,” “missing data,” or “not found,” but it should be handled carefully.
When you write or use a function, ask yourself one simple question: is this function meant to compute a value, or is it meant to perform an action?
If it computes a value, it should usually return something meaningful, and you should use that returned value.
If it performs an action, it might return None, and the value you care about may be the side effect, such as an updated list or a printed message.
This mindset helps you read other people’s code faster and avoid common beginner mistakes.
To build confidence, try writing a few small functions and calling them in different ways.
Start with a value-returning function:
def add_tax(price, tax_rate):
return price + (price * tax_rate)
print(add_tax(100, 0.18))
Code language: PHP (php)
Then try an action-based function:
def add_item(items, item):
items.append(item)
cart = ["apple", "banana"]
add_item(cart, "mango")
print(cart)
Code language: PHP (php)
As you practice, pay attention to when you use return and what you expect to receive from a function call.
[Small practice examples output]
Functions let you organize your code into reusable, readable units. You call functions using parentheses, you define your own functions with def, and you use return to send values back to the caller.
Some functions return useful results, and some exist mainly to perform actions. When a function does not return anything explicitly, Python returns None, and treating None like a normal value can quickly lead to errors.
Once you’re comfortable with these basics, you’ll be ready for more advanced function topics such as default arguments, keyword arguments, variable-length arguments, scope, and writing clean function interfaces.