What is a Function?
Think of functions like a vending machine. When you enter in a code, you get a soda. Same with a function, when you enter in an input number, you get an output number. Try it out!
Just like a vending machine maps inputs to outputs, a function is a mapping between a set of inputs and a set of outputs.
Notice a couple things:
- A function (or vending machine) take in an input and returns an output.
- Every input has only ONE output - when you input A1, you only get Big Red, nothing else. And when you input 3, you only get 9, nothing else.
- Multiple inputs can have the same output. To get Inca Kola, you can put in A2, B2, or C2. And to get 9, you can input 3 or -3.
- Every possible input on our keypad has an output. There are some numbers/codes that aren't on our keypads, like E1 or -15, but that's okay. We define a set of inputs, like our keypad, and we just need to make sure every input is covered.
In non-vending machine language, we write functions as something like
We represent our input with a variable, like , and our output is a calculation that uses our input, or variable. So, something like this:
So, when we input 2, all our 's are replaced with 2, and our output comes out to be 11:
Just like our vending machines!