Skip to Content
Nextra 4.0 is released 🎉
FunctionsFunctions

Functions

In this section you’ll learn how to define and call functions, pass them data with parameters and arguments, and get results back with return values.

What are functions?

A function is a reusable block of code that does one job. You give it a name, and whenever you need that job done, you call the function. Functions help you:

  • Organize your code
  • Make it more readable
  • Avoid repetition

Defining a function

You define a function using the def keyword, then the name you want for the function, then parentheses. When you define a function, you’re writing the code that will run later when you call it.

Here’s a simple function that greets a user:

def greet(): print("Hello, welcome to Python!")

We defined a function named greet that prints a welcome message when you call it.

Calling a function

To call a function, use its name followed by parentheses:

greet() # Output: Hello, welcome to Python!

When you call a function, the code inside it is executed. So calling greet() runs that code and prints Hello, welcome to Python! to the console.

Function parameters

Functions can take inputs so they can work with different data each time you call them. Those inputs are called parameters in the function definition. Here’s a function that takes one parameter:

def greet_user(name): print(f"Hello, {name}! Welcome to Python!")

The greet_user function has one parameter, name. When you call the function, you pass in a value—that value is called an argument. So in this call, "Alice" is the argument:

greet_user("Alice") # Output: Hello, Alice! Welcome to Python!

When we call greet_user("Alice"), the argument "Alice" is used for the name parameter, and the function prints a personalized greeting.

Return values

Functions can send a value back to the code that called them. That’s called returning a value. You use the return statement inside the function; the value you return is the return value.

Here’s a function that adds two numbers and returns the result:

def add_numbers(a, b): return a + b

You call the function and store the return value in a variable:

result = add_numbers(3, 5) print(result) # Output: 8

The add_numbers function has two parameters, a and b. When we call add_numbers(3, 5), the arguments are 3 and 5. The function returns 3 + 5, which is 8. That return value is stored in the variable result, and we print it on the next line.

Why return a value instead of just printing inside the function? So you can use the result elsewhere—in other calculations or in other parts of your code. For example, to add two numbers and then multiply the result by another number:

sum_result = add_numbers(3, 5) # sum_result will be 8 final_result = sum_result * 2 # final_result will be 16 print(final_result) # Output: 16

Keeping each function focused on one task (add, then multiply) makes your code easier to read and reuse. It’s good practice to write functions that do one task and return a value—you’ll see how useful that is as you build bigger programs.

Last updated on