What is a Function?

A function is like a machine: it has an input and an output.
And the output is related somehow to the input. 
Examples
But we are not going to look at specific functions ...
... instead we will look at the general idea of a function.
Names
First, it is useful to give a function a name. The most common name is "f", but you can have other names like "g" ... or even "marmalade" if you want.
And it is also nice to name what goes into the function, this is put inside parentheses () after the name of the function:
So f(x) shows you the function is called "f", and "x" goes in
And you will often see what a function does with the input:
f(x) = x^{2}^{} shows you that function "f" takes "x" and squares it.
So, with the "f(x) = x^{2}" function, an input of 4 becomes an output of 16. In fact we can write f(4) = 16.
Note: Sometimes a functon has no name, and you might just see something like y = x^{2}
Relating
At the top I said that a function was like a machine. But a function doesn't really have belts or cogs or any moving parts  and it doesn't actually destroy what you put into it!
In fact, a function relates an input to an output.
Saying "f(4) = 16" is like saying 4 is somehow related to 16. Or 4 → 16

Example: this tree grows 20 cm every year, so the height of the tree is related to its age using the function h:
h(age) = age × 20
So, if the age is 10 years, the height is h(10) = 200 cm 
We will look more at this idea after we have answered the question ...
What type of things do functions process?
"Numbers" seems an obvious answer, but ...


... which numbers? For example, the treeheight function h(age) = age×20 makes no sense for an age less than zero. 

... it could also be letters ("A"→"B"), or ID codes ("A6309"→"Pass") or stranger things. 
So we have to use something more general, and that is where sets come in:

A set is a collection of things, such as numbers.
Here are some examples:
Set of even numbers: {..., 4, 2, 0, 2, 4, ...}
Set of clothes: {"hat","shirt",...}
Set of prime numbers: {2, 3, 5, 7, 11, 13, 17, ...}
Positive multiples of 3 that are less than 10: {3, 6, 9}
Each individual thing in a set (such as "4" or "hat") is called a member, or element. 
So, a function takes elements of a set, and gives back (usually different) elements of a set. Which leads to our formal definition:

Formal Definition of a Function
A function relates each element of a set
with exactly one element of another
set
(possibly the same set). 



"exactly one" means that a function is single valued. It will not give back 2 or more results for the same input. So "f(2) = 7 or 9" is not right! 

Every element in "X" is related to some element in "Y". We say that the function covers "X" (relates every element of) 
Also notice in the illustration that there are two elements in "X" that relate to the same element in "Y". This is fine. No rule against it.
And lastly, notice that there are some elements of "Y" that are not related to at all. That is also fine.
That is all normal behaviour for a general function, but some types of functions have stricter rules, to find out more you can read Injective, Surjective and Bijective

Vertical Line Test
On a graph, the idea of single valued means that no vertical line would ever cross more than one value.
If it did cross more than once it would not be a function. 
Domain, Codomain and Range
In the illustration above
 the set "X" is called the Domain,
 the set "Y" is called the Codomain, and
 the set of elements that get pointed to in Y (the actual values produced by the function) is called the Range.
We have a special page on Domain, Range and Codomain if you want to know more.
Ordered Pairs
You can write the input and output of a function as an "ordered pair", such as (4,16).
They are called ordered pairs because the input always comes first, and the output second.
So, (4,16) means that the function takes in "4" and gives out "16"
And a function can then be defined as a set of ordered pairs:
Example: {(2,4), (4,5), (7,3)} is a function that says "2 is related to 4", "4 is related to 5" and "7 is related 3".
Also, notice that the domain is {2,4,7} and the range is {4,5,3}
But the function has to be single valued, so we also say
"if it contains (a, b) and (a, c), then b must equal c"
Which is just a way of saying that an input of "a" cannot produce two different results.
Example: {(2,4), (2,5), (7,3)} is not a function because {2,4} and {2,5} means that 2 could be related to 4 or 5, in other words not single valued
Conclusion
 a function relates inputs to outputs
 a function takes elements from a set (the domain) and relates them to elements in a set (the codomain).
 all the outputs (the actual values related to) is called the range
 an input produces only one output (not this or that)
 an input and its matching output are together called an ordered pair
 so a function can also be seen as a set of ordered pairs
That is All for Now
Happy functioning!
