A function is a reusable block of code that performs a single task

A function in Python is a named section of code that you write once and then run as many times as you need. Instead of typing the same instructions over and over, you bundle them into a function, give it a name, and call that name whenever you need the task done. Python functions start with the word def, followed by the function name, parentheses, and a colon.

The basic shape is always the same: you declare the function with def, indent the code that belongs inside it, and then call it by typing its name with parentheses. Python reads top to bottom, so you must define a function before you use it in your script.

Key Takeaways

  • Every function starts with def, a name, parentheses, and a colon, with the function body indented underneath.
  • Parameters go inside the parentheses and act as placeholders for information the function will receive when you call it.
  • The return statement sends a result back to the part of your code that called the function.
  • You call a function by typing its name followed by parentheses, with any required information inside those parentheses.
  • Functions without a return statement still run their code but send back None by default.

The structure of a simple function with no parameters

The simplest function takes no information in and just runs the same code each time. Here is an example:

def greet():     print("Hello, World!") greet()

The function is named greet. The parentheses are empty because it does not need any input. The line print("Hello, World!") is indented four spaces, which tells Python it belongs inside the function. When you type greet() at the bottom, Python runs the code inside the function and prints the message.

Every time you call greet(), the same thing happens. If you call it three times in a row, the message prints three times. The indentation is not optional — Python uses it to know which lines belong to the function and which do not.

Functions that accept parameters and use them

Most functions need to work with different information each time you call them. You pass that information through parameters, which are placeholders inside the parentheses. Here is a function that takes one parameter:

def greet(name):     print("Hello, " + name) greet("Alice") greet("Bob")

The parameter name sits inside the parentheses. When you call greet("Alice"), Python assigns "Alice" to the name parameter, and the function prints "Hello, Alice". When you call greet("Bob"), the same function prints "Hello, Bob". The function runs the same code both times, but with different input.

You can have multiple parameters separated by commas. A function that adds two numbers might look like this:

def add(a, b):     result = a + b     print(result) add(5, 3) add(10, 20)

The function receives two pieces of information, stores their sum in a variable called result, and prints it. Each time you call add, you provide two numbers in the parentheses.

Using return to send a result back to your code

The return statement lets a function send a value back to the code that called it. This is different from print, which only displays text on the screen. When you return a value, you can store it in a variable or use it in another calculation:

def add(a, b):     result = a + b     return result total = add(5, 3) print(total)

The function add now uses return instead of print. When you call add(5, 3), the function calculates the sum and sends it back. That returned value gets stored in the variable total. Then you can print it, use it in another calculation, or pass it to another function.

A function can return any type of value: a number, a string, a list, or even nothing at all. If a function reaches the end without a return statement, Python automatically returns None, which means "no value".

Default parameters let you call a function with fewer arguments

Sometimes you want a parameter to have a standard value that the caller can override if needed. You set this by putting an equals sign and a value inside the parentheses:

def greet(name, greeting="Hello"):     print(greeting + ", " + name) greet("Alice") greet("Bob", "Hi")

The parameter greeting has a default value of "Hello". When you call greet("Alice"), Python uses "Hello" automatically. When you call greet("Bob", "Hi"), you override the default and use "Hi" instead. Default parameters must come after regular parameters in the function definition.

Common mistakes when writing functions

Indentation errors are the most frequent problem. If you forget to indent the code inside your function, Python will not know it belongs there. Every line that is part of the function must be indented the same amount, usually four spaces. When your function ends and you want to write code outside it, you stop indenting.

Another common error is calling a function before you define it. Python reads your script from top to bottom, so the def statement must come before any call to that function. If you try to call a function that does not exist yet, Python raises a NameError.

Forgetting parentheses when you call a function is also easy to do. Typing just greet without parentheses tells Python you want the function object itself, not to run it. You must type greet() with the parentheses to actually execute the code inside.

Frequently Asked Questions

What is the difference between a parameter and an argument?

A parameter is the name you give to a placeholder inside the function definition. An argument is the actual value you pass in when you call the function. In def add(a, b):, a and b are parameters. In add(5, 3), the numbers 5 and 3 are arguments.

Can a function return more than one value?

Yes. You can return multiple values by separating them with commas: return a, b, c. Python packages them into a tuple, and you can unpack them on the other side: x, y, z = my_function(). This lets one function send back several pieces of information at once.

What happens if I call a function with the wrong number of arguments?

Python raises a TypeError and stops running your code. If a function expects two arguments and you give it only one, Python tells you exactly how many it needs. Default parameters are the exception — if a parameter has a default value, you can leave it out when you call the function.

Can I define a function inside another function?

Yes, you can nest functions inside each other. The inner function only exists inside the outer function and cannot be called from outside. This is useful for organizing code and creating helper functions that only one other function needs to use.