Let S be a semigroup, ℍ be the skew field of quaternions, and ψ: S→S be an anti-endomorphism. We determine the general solution of the functional equation
g(xy) - g(xψ(y)) = 2g(x)g(y), x,y ∈ S,
where g: S→ℂ is the unknown function. And when S = M is a monoid, we solve the functional equation
g(xy) + g(xψ(y)) = 2g(x)g(y), x,y ∈ M,
where g: M→ℍ is the unknown function.