Functions: Inputs & Outputs
Welcome back! In Level 1, we learned about variables — containers that hold values. Now we're going to see how variables can be connected in a powerful way: through functions. Think of a function as a machine that takes something in, does something to it, and gives something back.
What is a Function?
Imagine you have a vending machine. You put in a coin (the input), press a button, and out comes a snack (the output). The machine itself is like a function: it has a rule that says "if you give me a coin, I'll give you a snack." In math, a function is a rule that takes an input and produces exactly one output.
We write functions using a special notation. For example:
( f(x) = 2x + 1 )
Here, ( f ) is the name of the function (like "snack machine"), ( x ) is the input variable (like the coin), and ( 2x + 1 ) is the rule (like "give a snack worth twice the coin plus one"). The output is the result of applying the rule to the input.
Identifying Input and Output
Every function has an input and an output. Let's look at a few examples:
-
Example 1: ( g(t) = t - 3 )
- Input: ( t ) (any number)
- Output: ( t - 3 ) (the input minus 3)
-
Example 2: ( h(y) = y^2 )
- Input: ( y )
- Output: ( y^2 ) (the input multiplied by itself)
-
Example 3: ( f(x) = 5 )
- Input: ( x ) (any number)
- Output: always 5, no matter what the input is. This is a constant function.
Notice that the input is always the variable inside the parentheses, and the output is whatever the rule gives.
Evaluating a Function
To evaluate a function means to find the output for a specific input. We do this by replacing the input variable with the given number and then simplifying.
Example: Evaluate ( f(x) = 2x + 1 ) for ( x = 3 ).
- Replace ( x ) with 3: ( f(3) = 2(3) + 1 )
- Multiply: ( 2 \times 3 = 6 )
- Add: ( 6 + 1 = 7 )
- So ( f(3) = 7 ). The output is 7.
Let's try another: Evaluate ( g(t) = t - 3 ) for ( t = 10 ).
- Replace ( t ) with 10: ( g(10) = 10 - 3 )
- Subtract: ( 10 - 3 = 7 )
- So ( g(10) = 7 ).
One more: Evaluate ( h(y) = y^2 ) for ( y = 4 ).
- Replace ( y ) with 4: ( h(4) = 4^2 )
- Square: ( 4 \times 4 = 16 )
- So ( h(4) = 16 ).
Creating a Table of Input-Output Pairs
A table is a great way to see how a function behaves for different inputs. We list inputs in one column and the corresponding outputs in another.
Example: Make a table for ( f(x) = 2x + 1 ) for inputs ( x = 0, 1, 2, 3 ).
| Input (x) | Output (f(x) = 2x + 1) |
|---|---|
| 0 | 2(0) + 1 = 1 |
| 1 | 2(1) + 1 = 3 |
| 2 | 2(2) + 1 = 5 |
| 3 | 2(3) + 1 = 7 |
So the pairs are: (0,1), (1,3), (2,5), (3,7).
Another example: Make a table for ( g(t) = t - 3 ) for inputs ( t = 0, 1, 2, 3 ).
| Input (t) | Output (g(t) = t - 3) |
|---|---|
| 0 | 0 - 3 = -3 |
| 1 | 1 - 3 = -2 |
| 2 | 2 - 3 = -1 |
| 3 | 3 - 3 = 0 |
Pairs: (0,-3), (1,-2), (2,-1), (3,0).
Why This Matters for AI
AI models are like giant functions. They take input data (like an image or a sentence) and produce output (like a label or a translation). For example, a model that recognizes cats takes a picture (input) and gives a prediction like "cat" or "not cat" (output). The model's internal rules are much more complex than ( 2x + 1 ), but the idea is the same: input goes in, output comes out.
Key Takeaways
- A function is a rule that maps each input to exactly one output.
- The input is the variable inside the parentheses (like ( x ) in ( f(x) )).
- To evaluate a function, replace the input variable with a number and simplify.
- A table of input-output pairs helps us see the function's behavior.
- AI models are essentially very complex functions that turn input data into output predictions.