It might be enlightening to see this in a more general setting than just functions that map some kind of number to some other kind of number. Functions are a very important tool in most programming languages1, where you often work with far more complicated data than just numbers2. For instance, in Haskell,
f :: String -> Int
is a function that takes a character string (e.g. "hullo") and outputs an integer number. This function might be defined3 as
f("How much is 5+6?") = 11
f("How many letters has the alphabet?") = 26
f("What is the answer to life, the universe, and everything?") = 42
f(x) = error("I haven't understood your question. You said: " ++ x)
In an interactive interpreter, this might work as follows:
ghci> let f("How much is 5+6?") = 11; f("How many letters has the alphabet?") = 26; f("What is the answer to life, the universe, and everything?") = 42; f(x) = error("I haven't understood your question. You said: " ++ x)
ghci> f("What is the answer to life, the universe, and everything?")
42
ghci> f("How much is 5+6?")
11
ghci> f("How many years is my age?")
*** Exception: I haven't understood your question. You said: How many years is my age?
However, if I tried to define the function in a way that's not compatible with the type signature4 String -> Int
, the interpreter would complain right away.
ghci> let f :: String->Int; f("How do you write out '7'?") = "Seven."
<interactive>:16:56:
Couldn't match expected type `Int' with actual type `[Char]'
In the expression: "Seven."
In an equation for `f': f ("How do you write out '7'?") = "Seven."
because "Seven."
, unlike 7
, is not actually an integer number but again a character string.
1Most programming languages are very sloppy with their mathematical notation, they have functions with "side-effects", like printing something to the screen or sending an E-Mail to somebody, which doesn't make any sense for mathematical functions. In Haskell, this is generally banned (it has a
special "magic trick" to do such stuff) except for error messages (which abort the entire program, so the function doesn't need to bother to actually return a number in the fourth case).
2Of course, you also work with much more complicated data than numbers in mathematics – only, those kind of objects are rather harder to understand than character strings, I reckon.
3Note that you normally wouldn't need to write all those parentheses in Haskell, I just used them so it looks more familiar.
4What's called "types" in programming languages is almost (but not quite) the same as the sets you're dealing with in mathematics.