In the definition of a functor, why is it necessary that $F(id_{A})=id_{F(A)}$?

Consider the following counterexample.

Let $\mathcal{C}$ be the category with one object $C$ and one morphism $\mathrm{id}_C$. Let $\mathcal{D}$ be the category with one object $D$ and two morphisms $\mathrm{id}_D : D \to D$ and $f : D \to D$, where composition is defined by $f \circ f = f$.

Define $F : \mathcal{C} \to \mathcal{D}$ by $$F(C)=D \qquad \text{and} \qquad F(\mathrm{id}_C)=f$$ Then $F$ satisfies all the requirements of being a functor, except for the requirement that $F(\mathrm{id}_C)=\mathrm{id}_{D}$, since $F(\mathrm{id}_C)=f\ne\mathrm{id}_D$.


Added: As per your question in the comments, here's an example where $F$ is surjective on morphisms: take $\mathcal{C}$ to be a category with two objects $C_1$ and $C_2$ and only identity morphisms, take $\mathcal{D}$ as before, define $F(C_1)=F(C_2)=D$, $F(\mathrm{id}_{C_1})=f$ and $F(\mathrm{id}_{C_2})=\mathrm{id}_D$.


If $M$ is a category with one object $O$, then $(\mathrm{Hom}(O, O), ∘)$ is a monoid, and conversly, every monoid $M$ can be made into a one-object category the morphisms of which are the elements of that monoid, and composition is monoid product. Furthermore, monoid morphisms correspond to functors between the one object categories constructed in this way. This is often a simple and useful example to keep in mind

Now take any semigroup morphism of two monoids that isn't a monoid morphism (ie. it satisfies $f(ab) = f(a)f(b)$, but not $f(e) = e$), and you'll get your counterexample. To construct these, it's easiest to note that the unit must map to an idempotent, and go from there: for example, $(ℕ, \cdot) → (ℕ, \cdot)$, $n ↦ 0$, or $(ℕ, \cdot) → (ℕ × ℕ, \cdot)$, $n ↦ (n, 0)$ will work.