Ed and Aaron, thank you very much for explaining the practical benefits of injectivity and surjectivity.
I have taken what I learned from you and cast it into my own thoughts and experiences. Please let me know of any errors.
Motivation
When I go hiking, I want to be able to retrace my steps and get back to my starting point.
When I go to a store, I want to be able to return home.
When I swim out from the shore, I want to be able to get back to the shore.
Going somewhere and then coming back to where one started is important in life.
And it is important in mathematics.
And it is important in functional programming.
Domain, Codomain, and Inverse
If a function maps a set of elements (the domain) to a set of values (the codomain) then it is often useful to have another function that can take the elements in the codomain and send them back to their original domain values. The latter is called the inverse function.
In order for a function to have an inverse function, it must possess two important properties, which I explain now.
The Injective Property
Let the domain be the set of days of the week. In Haskell one can create the set using a data type definition such as this:
data Day = Monday | Tuesday | Wednesday | Thursday | Friday | Saturday | Sunday
Let the codomain be the set of breakfasts. One can create this set using a data type definition such as this:
data Breakfast = Eggs | Cereal | Toast | Oatmeal | Pastry | Ham | Grits | Sausage
Now I create a function that maps each element of the domain to a value in the codomain.
Here is one such function. The first line is the function signature and the following lines is the function definition:
f :: Day -> Breakfast
f Monday = Eggs
f Tuesday = Cereal
f Wednesday = Toast
f Thursday = Oatmeal
f Friday = Pastry
f Saturday = Ham
f Sunday = Grits
An important thing to observe about the function is that no two elements in the domain map to the same codomain value. This function is called an injective function.
[Definition] An injective function is one such that no two elements in the domain map to the same value in the codomain.
Contrast with the following function, where two elements from the domain -- Monday and Tuesday -- both map to the same codomain value -- Eggs.
g :: Day -> Breakfast
g Monday = Eggs
g Tuesday = Eggs
g Wednesday = Toast
g Thursday = Oatmeal
g Friday = Pastry
g Saturday = Ham
g Sunday = Grits
The function is not injective.
Can you see a problem with creating an inverse function for g :: Day -> Breakfast
?
Specifically, what would an inverse function do with Eggs? Map it to Monday? Or map it to Tuesday? That is a problem.
[Important] If a function does not have the injective property then it cannot have an inverse function.
In other words, I can't find my way back home.
The Surjective Property
There is a second property that a function must possess in order for it to have an inverse function. I explain that next.
Did you notice in the codomain that there are 8 values:
data Breakfast = Eggs | Cereal | Toast | Oatmeal | Pastry | Ham | Grits | Sausage
So there are more values in the codomain than in the domain.
In function f :: Day -> Breakfast
there is no domain element that mapped to the codomain value Sausage.
So what would an inverse function do with Sausage? Map it to Monday? Tuesday? What?
The function is not surjective.
[Definition] A surjective function is one such that for each element in the codomain there is at least one element in the domain that maps to it.
[Important] If a function does not have the surjective property, then it does not have an inverse function.
[Important] In order for a function to have an inverse function, it must be both injective and surjective.
Injective + Surjective = Bijective
One final piece of terminology: a function that is both injective and surjective is said to be bijective. So, in order for a function to have an inverse function, it must be bijective.
Recap
If you want to be able to come back home after your function has taken you somewhere, then design your function to possess the properties of injectivity and surjectivity.
Best Answer
Whether a mapping is a function can often be a matter of simply how you define two things:
For example, consider $f : \mathbb{R} \to \mathbb{R}$ as definned by $f(x) = 1/x$.
This is not a function as-is: $x=0$ presents an issue. However, we can fix this in two ways, either being valid:
$$f(x) = \left\{\begin{matrix} 1/x & x \neq 0\\ 0 & x=0 \end{matrix}\right.$$
Boom, $f$ is defined for all values of the domain (and only maps each element of it to a single element obviously) and thus is a "general function" in the sense you noted.
Without either of these premises, at best $f$ is what we call a "partial function." Partial functions need not assign a value to each element of the domain. What this "general function" you were introduced to is really more often called is a "total function," where every element of the domain is assigned a value. Note that all total functions are partial functions: just because they don't have to assign a value to all of the domain, doesn't mean they can't.
Is there a notion of "injective" for partial functions? Yup!
Let us remember the definition of injective: for all $a,b$, $f(a)=f(b)$ implies $a=b$. In that sense, you could consider a partial function injective under the same definition provided $f$ is defined at those values.
So can the mapping be injective, even if not defined at all elements of the domain? Well ... yes. But it's just a slightly different notion of injective than it is for total functions. You could say it's injective but only in the sense in which partial functions are injective.
Similarly, you could define similar notions for "partial" surjectivity and in turn "partial" bijectivity - just include a premise that, wherever we have the mapping take on a value, it must also be defined there.
Beyond this, though, I have nothing to really offer you.
Why?
It's because it depends on the context whether you would say it's injective in the sense of "total function" injectivity, or in the sense of "partial function" injectivity. To say that total functions are just a subset of partial functions also carries with it that all "total injective" functions are "partial injective," but not all "partial injective" functions are "total injective" - because for a function to be "total injective" would mean it would have to be a total function in the first place, and not all partial functions are total functions.
You can probably see already how confusing a matter this is for me to approach, and it's probably not much better on your end, is it? I mean, the definitions for it are so similar that it muddies the waters a hell of a lot.
So context is important.
If, for example, this is being discussed in a homework assignment, and you've not been introduced to partial functions yet, then to say a function is injective in the "partial" sense and not the "total" sense is basically just a different way to say "it's not injective in the way that matters in this context," right? (Namely if the "function" isn't really a total function, it's definitely not a "total injective" function.)
Though of course you could also leave a note in said homework to the professor explaining why it's "partial injective" but not "total injective." I like doing that sometimes in cases where minor ambiguities or contradictions pop up just so my work has complete clarity, even if it's a minor detail.
So, in summary:
The mapping $f : \mathbb{R} \to \mathbb{R}$ defined by $f(x) = 1/x$ is not a "total function," but it is a "partial function." It could be tweaked to be a total function though.
The notion of "injective" for partial functions is the same as for total functions, with the slight change that when we assume $f$ is given a value, e.g. $f(a)$, it is assumed said value is defined. (We ignore undefined values.)
There are similar notions of surjective and bijective for partial functions.
Whether you want to say a partial function is injective depends on context. If you're under the expectation that the mapping in question be a total function to be injective, and you have a mapping that is injective but only a partial (not total) function, you won't say it's injective since you implicitly also want it to be total.
If you ever need clarifications on context and what is expected, ask your professor/teacher/whoever for the best clarity.