A function is a block of code you write once and run many times
A function in Python is a set of instructions grouped together under a single name. Instead of writing the same code over and over, you write it once as a function, then call that function by name whenever you need it. This saves time, makes your code easier to read, and makes it simpler to fix mistakes — you only have to fix them in one place.
The basic shape is always the same: you define the function with the def keyword, give it a name, list what information it needs (called parameters), then write the instructions it should follow. After that, you call the function by typing its name with parentheses.
Key Takeaways
- Every function starts with def, followed by the function name, parentheses, and a colon, then the code indented underneath.
- Parameters are the pieces of information a function needs to work — you list them inside the parentheses when you define the function.
- A return statement sends a result back to the code that called the function, and without it the function produces no output.
- You call a function by typing its name followed by parentheses, with the actual values (called arguments) inside the parentheses.
- Functions let you reuse code, organize your work into smaller pieces, and test each piece separately.
The basic structure: def, name, parentheses, colon, indented code
Every function follows the same pattern. You start with the word def, then the name you want to give the function, then parentheses (which may be empty or contain parameter names), then a colon. Everything after that colon must be indented — Python uses indentation to show what code belongs inside the function.
Here is the simplest possible function:
def greet(): print("Hello, world!")
This function is named greet, takes no parameters (the parentheses are empty), and contains one instruction: print the text "Hello, world!". To run this function, you type greet() anywhere after you have defined it.
A function with more than one instruction looks the same way — just keep indenting and adding lines:
def greet(): print("Hello!") print("Welcome to Python.")
Parameters: telling a function what information it needs
Most functions need information to work with. A parameter is a placeholder for that information. You list parameters inside the parentheses when you define the function, separated by commas if there is more than one.
Here is a function that takes one parameter:
def greet(name): print("Hello, " + name + "!")
The word name inside the parentheses is the parameter. When you call this function, you provide the actual value — called an argument — inside the parentheses. So greet("Alice") runs the function with name set to "Alice", and it prints "Hello, Alice!". If you call greet("Bob"), it prints "Hello, Bob!".
A function can have multiple parameters:
def add(number1, number2): result = number1 + number2 print(result)
This function takes two parameters, adds them together, and prints the result. You would call it like add(5, 3), and it would print 8.
Return statements: sending a result back to the code that called the function
A return statement sends a value back to the code that called the function. Without a return statement, a function does something (like print text) but does not give you a value you can use later.
Here is the difference. This function prints a result but does not return it:
def add(number1, number2): result = number1 + number2 print(result)
This function returns the result, so you can store it or use it in another calculation:
def add(number1, number2): result = number1 + number2 return result
Now you can do things like total = add(5, 3), and the variable total will hold the value 8. You can also nest functions: print(add(5, 3)) will call add, get back 8, and print it. A function stops running as soon as it hits a return statement, so any code after the return does not execute.
Calling a function: using the name and parentheses
Once you have defined a function, you call it by typing its name followed by parentheses. If the function takes parameters, you put the actual values (arguments) inside the parentheses, separated by commas.
Here is a complete example:
def multiply(a, b): return a * b answer = multiply(4, 7) print(answer)
The function multiply is defined first. Then it is called with the arguments 4 and 7. The function returns 28, which is stored in the variable answer. Finally, print(answer) outputs 28.
You can call a function as many times as you want, with different arguments each time:
print(multiply(2, 3)) print(multiply(10, 5)) print(multiply(7, 8))
This calls the same function three times with different values and prints each result.
Default parameters: giving a parameter a value if none is provided
Sometimes you want a parameter to have a default value — a value it uses if the person calling the function does not provide one. You set this up by putting an equals sign and the default value after the parameter name in the function definition.
Here is an example:
def greet(name="Friend"): print("Hello, " + name + "!")
Now you can call greet() with no arguments, and it uses "Friend" as the default: greet() prints "Hello, Friend!". You can also override the default by providing your own value: greet("Alice") prints "Hello, Alice!".
Default parameters are useful when a function has a common case but you still want the option to change it. If you have multiple parameters and only some have defaults, the ones with defaults must come after the ones without.
Common mistakes and how to avoid them
The most common mistake is forgetting to indent the code inside the function. Python uses indentation to know what belongs inside the function and what does not. If you do not indent, Python will give you an error.
Another common mistake is forgetting the colon after the parentheses. The colon tells Python that an indented block is coming next. Without it, you get a syntax error.
A third mistake is confusing parameters (what you list when you define the function) with arguments (what you provide when you call the function). Parameters are placeholders; arguments are the actual values. This confusion usually does not cause an error, but it makes your code harder to understand.
Finally, remember that a function does not run until you call it. Defining a function just tells Python what to do when you call it later. If you define a function but never call it, nothing happens.
Frequently Asked Questions
What is the difference between a parameter and an argument?
A parameter is the name you give to a piece of information when you define the function. An argument is the actual value you provide when you call the function. In def add(a, b):, the parameters are a and b. When you call add(5, 3), the arguments are 5 and 3.
Can a function return more than one value?
Yes. You can return multiple values separated by commas, and Python will bundle them together. For example, return x, y returns both x and y. When you call the function, you can unpack them: result1, result2 = my_function().
What happens if I call a function before I define it?
Python will give you an error saying the function is not defined. Python reads your code from top to bottom, so the function definition must come before the call. You can define multiple functions at the top of your file, then call them in any order below.
Can a function call another function?
Yes. Inside a function, you can call any other function that has already been defined. This is useful for breaking large tasks into smaller pieces. For example, a function that calculates the area of a circle might call a separate function that calculates the square of a number.