Give a function a job.

Inputs go in. A useful result comes back.

add(a, b) 7

Try changing either number. Whole numbers from −99 to 99.

View transcript See the function

Same function.
Different inputs.

a and b name the two inputs. The function adds them, then return sends the answer back.

Change the numbers above. The job stays the same.

The Python behind the idea
def add(a, b):
    return a + b

add(3, 4) returns 7.

The interactive illustration calculates this one sum in your browser. It doesn’t run a Python interpreter.

Your goal: Explain how to define Python functions, use parameters and return values, and design functions effectively.

Follow the idea

Watch the inputs become a result.

Follow the visual as you scroll. Scroll back to revisit an earlier idea.

01 / 03

First, define the recipe.

def add(a, b): defines a function with two parameters. Defining it does not run its body. The names a and b will receive values when the function is called.

Explore the source ↗

02 / 03

Describe what comes back.

return a + b sends the sum back to the caller. It does not print anything by itself. That separation lets another part of the program use the result.

Explore the source ↗

03 / 03

Now give it real inputs.

add(2, 3) calls the function: a is 2 and b is 3. The returned value, 5, is assigned to total. The final line prints it.

Explore the source ↗

A path through the topic

A suggested syllabus for the course you can create in Nemonic.

  1. Defining Functions in Python

    Understand the syntax of function definitions using 'def' and how to name functions.

  2. Using Parameters

    Learn how to pass inputs to functions through parameters to customize behavior.

  3. Return Values

    Discover how to return results from functions using the 'return' statement.

  4. Function Design Principles

    Explore best practices for writing clear, focused, and reusable functions.

  5. Practical Example: Fibonacci Function

    Walk through a function that prints Fibonacci numbers to understand function flow.

Read the sample

Original Nemonic sample · AI-generated narration · 51 seconds

Imagine a function as a vending machine. You input coins and select a snack; the machine processes your request and delivers the snack. In Python, you define a function with 'def', give it a name, and specify parameters inside parentheses—these are like your coins and snack choice. Inside the function, Python runs the code you wrote to process those inputs. When done, you use 'return' to send back a result, like the snack from the machine. For example, a Fibonacci function takes a number n and prints all Fibonacci numbers less than n. This shows how functions can perform tasks repeatedly with different inputs. Designing functions to do one thing well makes your code easier to maintain and reuse, just like a reliable vending machine that always gives you the right snack.

Try it yourself

Try defining a function named 'square' that takes one parameter 'x' and returns its square. What will 'square(5)' return? The function should multiply 'x' by itself and return the result. So, calling 'square(5)' returns 25 because 5 times 5 equals 25. This exercise helps you practice defining functions with parameters and return values.

Sources & further learning

Written for Nemonic with AI assistance and automated source checks. Sources inform the lesson; their publishers do not endorse Nemonic. Updated 2026-09-27.

Keep exploring

Make it your own

Keep your curiosity moving.