Skip to main content

Python Functions: Define, Call, and Return Values

Beginner25 min6 exercises80 XP
0/6 exercises

Imagine you have a favorite recipe for pancakes. Every Sunday morning, you follow the same steps: mix flour, crack eggs, pour batter, flip. You don't reinvent the recipe each time — you just follow the one you already know.

Functions in Python work exactly like recipes. You write the instructions once, give them a name, and then call that name whenever you need those instructions to run again. Write once, use anywhere.

In this tutorial, you'll learn how to create your own functions, pass data into them, and get results back. By the end, you'll be writing clean, reusable code like a real developer.

How to Define Your First Function

To create a function in Python, you use the def keyword, followed by a name, parentheses, and a colon. Everything indented underneath belongs to that function.

Defining a function
Loading editor...

If you run the code above, nothing happens. That's because defining a function is like writing down a recipe. It doesn't cook anything until you actually follow the steps.

To actually run the function, you call it by writing its name followed by parentheses:

Calling a function
Loading editor...

Now you see the output. The parentheses () after greet are what tell Python to actually run the function. Without them, Python just looks at the recipe without cooking.

You can call a function as many times as you want. Each call runs all the code inside the function from top to bottom.

Calling a function multiple times
Loading editor...

What Are Parameters and Arguments?

Our greet() function always says the same thing. That's like a recipe that only makes plain pancakes. What if you want blueberry pancakes? You need a way to pass in ingredients.

In Python, you pass data into a function using parameters. A parameter is a variable name listed inside the parentheses of the function definition. When you call the function, the actual value you send in is called an argument.

A function with one parameter
Loading editor...

Here, name is the parameter — a placeholder. When we call greet('Alice'), the string 'Alice' is the argument — the actual value that fills the placeholder.

How Do Return Values Work?

So far, our functions just print things. But most of the time, you want a function to calculate something and hand the result back to you. That's what return does.

Think of it this way: print() is like shouting the answer out loud. return is like writing the answer on a piece of paper and handing it to whoever asked.

Returning a value
Loading editor...

The return statement does two things: it sends a value back to wherever the function was called, and it immediately exits the function. Any code after return inside the same function will not run.

Code after return is unreachable
Loading editor...
Printing (side effect)
def add(a, b):
    print(a + b)

# Can't reuse the result
add(3, 5)
Returning (gives back a value)
def add(a, b):
    return a + b

# Can store, reuse, combine
result = add(3, 5)
print(result * 2)

Using Multiple Parameters

Functions can accept as many parameters as you need. Just separate them with commas. When calling, the arguments are matched to parameters in order — the first argument goes to the first parameter, and so on.

A function with three parameters
Loading editor...

The order matters. If you swap the arguments around, you'll get nonsense results or an error. introduce(25, 'Alice', 'London') would try to use 25 as a name.

What Are Default Parameter Values?

Sometimes you want a parameter to have a fallback value — a sensible default that's used when the caller doesn't provide one. You set this with an = sign in the definition.

Default parameter value
Loading editor...

When we call greet('Alice'), the greeting parameter uses its default value 'Hello'. When we call greet('Bob', 'Hey'), we override the default with 'Hey'.

Can Functions Call Other Functions?

Absolutely! In fact, this is one of the most powerful ideas in programming. You can build small, focused functions and then combine them like building blocks.

Functions calling functions
Loading editor...

Here, sum_of_squares calls square twice. Each small function does one simple job. Together, they solve a bigger problem. This approach makes your code easier to read, test, and fix.

Using a helper function
Loading editor...

Practice Exercises

Say Hello
Write Code

Write a function called say_hello that takes a name parameter and prints Hello, <name>! (with the exclamation mark). For example, say_hello('World') should print Hello, World!.

Loading editor...
Predict the Output
Predict Output

Read the code below carefully. What will it print? Type the exact output.

def double(x):
    return x * 2

result = double(7)
print(result)
print(double(3))
Loading editor...
Area of a Rectangle
Write Code

Write a function called area that takes width and height as parameters and returns their product (the area of a rectangle). Do not print — just return the value.

Loading editor...
Fix the Broken Function
Fix the Bug

This function is supposed to return the larger of two numbers, but it has a bug. Find and fix it so bigger(3, 7) returns 7 and bigger(10, 5) returns 10.

def bigger(a, b):
    if a > b:
        return a
    return b
    return a

Wait — actually the logic looks right but the test is failing. Look carefully at each line.

Loading editor...
Temperature Converter
Write Code

Write a function called celsius_to_fahrenheit that takes a temperature in Celsius and returns it converted to Fahrenheit. The formula is: F = C * 9/5 + 32. Return the result as a float (the formula naturally produces one).

Loading editor...
Build a Grade Calculator
Write Code

Write a function called get_grade that takes a numeric score (0-100) and returns a letter grade as a string:

  • 90 and above: 'A'
  • 80-89: 'B'
  • 70-79: 'C'
  • 60-69: 'D'
  • Below 60: 'F'
  • Loading editor...