Free Lesson

What Is a Function?

A function is a rule that takes an input and gives back exactly one output, like a machine that turns 4 into 9 every single time. In math it is written f(x); in programming it is a named, reusable block of code that takes inputs and returns a result.

Try one below: feed numbers or words into a function machine, see the same rule as math and as code, then crack hidden machines in the guess-the-rule game.

Interactive lab

The function machine

Pick a machine, type an input and run it. The same rule is shown as math and as Python or JavaScript code, and every run lands in the input-output table. Everything runs in your browser.

Input

?

Rule

Multiply by 2, then add 1

f(x) = 2x + 1

Output

?

Same machine, written as

f(x) = 2x + 1

Run the machine to fill in a value.

f is the function's name, x is the input, and f(x) is the output.

Input-output table

Run a few inputs to build the table.

Guess the rule

A hidden machine is waiting. Feed it inputs, watch the outputs, then pick the rule.

Starter, machine 1 of 2 (takes numbers)

    Test at least one input, then pick the rule:

    Function or not? Quick quiz

    Five questions on f(x), input-output tables, code and the vertical line test.

    1. If f(x) = 2x + 1, what is f(3)?
    2. Inputs 1, 2, 2 give outputs 5, 6, 7. Is this a function?
    3. Inputs 1, 2, 3 all give the output 4. Is this a function?
    4. In def greet(name): return "Hi " + name, what is name?
    5. A vertical line touches a graph at two points. What does that tell you?

    Two meanings, one idea

    Functions in math vs programming

    AspectIn mathIn programming
    Looks likef(x) = 2x + 1def f(x): return 2 * x + 1
    Inputx, a number from the domainx, a parameter (the value passed in is the argument)
    RuleA formulaLines of code in the function body
    Outputf(x), exactly one value per inputThe value after return (or nothing)
    Using itSubstitute: f(3) = 7Call it: f(3) returns 7
    Extra powersNone, it only maps inputs to outputsCan also print, save files or change things (side effects)

    Anatomy

    The parts of a function in code

    def double(x):
        return x * 2
    
    print(double(5))  # 10
    def
    Keyword that starts a function in Python (function in JavaScript).
    double
    The function's name. You call it by this name.
    (x)
    The parameter: a placeholder for the input.
    return x * 2
    The body works out the answer; return sends it back.
    double(5)
    A call. 5 is the argument, and the call becomes 10.

    Is it a function?

    The one-output rule and the vertical line test

    In a table

    Look for the same input listed twice with different outputs. Inputs 1, 2, 3 giving 4, 4, 4 is still a function; input 2 giving both 6 and 7 is not.

    On a graph

    Slide a vertical line across the graph. If it ever crosses the graph twice, one x has two y values, so it is not a function. A line or a U-shaped parabola passes; a circle fails.

    How to

    How to write a function

    1. Step 1

      Name the job

      Pick a verb-like name that says what the function does, such as double, shout or to_fahrenheit.

    2. Step 2

      List the inputs

      Put the parameters in brackets after the name: def double(x): in Python or function double(x) { in JavaScript.

    3. Step 3

      Write the rule and return

      In the body, work out the answer and send it back with return, for example return x * 2.

    4. Step 4

      Call it

      Use the name with a real value in brackets, double(5), and the call is replaced by the returned output, 10.

    Learning to code with AI?

    Code Card turns your Claude Code, Codex, and OpenClaw sessions into a public developer profile with activity graphs, token breakdowns, badges, and a shareable card. Start with npx code-card.

    Make your Code Card

    FAQ

    Function questions, answered

    What is a function in simple terms?

    A function is a rule that takes an input and gives back exactly one output. Think of a machine: put 4 into a doubling machine and 8 comes out, every time.

    What is a function in math?

    In math, a function pairs every input (x) with exactly one output (y), written f(x). For f(x) = 2x + 1, the input 3 always gives 7. The set of allowed inputs is the domain and the possible outputs are the range.

    What is a function in programming?

    In programming, a function is a named, reusable block of code. You give it inputs called parameters, it runs its steps, and it can send a value back with return. You run it by calling its name, like double(5).

    How do you tell if something is a function?

    Check that no input has two different outputs. In a table, look for a repeated input with different outputs. On a graph, use the vertical line test: if any vertical line crosses the graph more than once, it is not a function.

    What is the difference between a parameter and an argument?

    A parameter is the named input slot in the definition, like x in def double(x). An argument is the actual value you pass when you call it, like 5 in double(5).

    Are math functions and programming functions the same thing?

    They share the idea of input, rule and output. Programming functions can also do things besides returning a value, such as printing or saving a file, and some return nothing at all.