The discussion about "well-defined" is perhaps a bit obscure. Here's the problem:
Remember that in general, an element of $G$ may have many different "names." For example, if $n=10$, then the element $x^{11}$ is equal to $x$; in fact, $x$ has infinitely many different "names" as a power of $x$:
$$\cdots = x^{-9} = x^1 = x^{11} = x^{21} = \cdots$$
The problem is that the definition of $f$ as given depends on the name of the element! That is, if we are furiously working and somebody hands us a power of $x$, say, $x^{3781}$, we are supposed to, unthinkingly, map it to $y^{3781}$. The problem is that $x^{3781}$ is the same element as $x$, which we are supposed to send to $y^1$. That means that unless $y^1=y^{3781}$, what we have is not really a function: because the same input, $x$ (who, when being teased by bullies is called "$x^{3781}$") may be sent to $y^1$ or to $y^{3781}$, depending on what "name" we just heard for it.
Checking that the value of the function is the same regardless of what name we are using for an element, even though the function is defined in terms of the name, is called "checking that the function is 'well-defined.'"
An example of a function that is not well-defined would be one in which the input is an integer, and the output is the number of symbols used to express that integer. For example, $f(3)$ would be $1$ (because 3
is only one symbol), but $f(4-1)$ would be $3$ (because we are using 4
, -
, and 1
). This is not well defined as a function of the integers, because $3$ is the same as $4-1$, but $f$ assigns it two different outputs.
So in order for the expression given to actually define a function, we need the following to be true:
$$\text{if }x^i=x^j,\text{ then }y^i = f(x^i)\text{ is equal to }y^j=f(x^j).$$
Now, $x^i=x^j$ if and only if $i\equiv j\pmod{n}$; and $y^i=y^j$ if and only if $i\equiv j\pmod{m}$. Therefore, we need:
$$\text{if }i\equiv j\pmod{m},\text{ then }i\equiv j\pmod{n}.$$
In other words: every number that is a multiple of $m$ must be a multiple of $n$.
This is equivalent to $n|m$.
In fact, you noticed that in what you wrote, because $x^n=e$, so we need $f(x^n)$ to be the same as $f(x^0)$.
Once we know that $n|m$, then $f$ is well defined. Once it is well-defined, we can start verifying that it is indeed a homomorphism (it is). Technically, it's incorrect to start working to see if it is a homomorphism before you even know whether or not it is a function.
Note that the condition actually works if we allow $G$ or $H$ to be infinite cyclic groups, if we call infinite cyclic groups "groups of order $0$". Then $i\equiv j\pmod{0}$ means $i=j$, every number divides $0$, but the only multiple of $0$ is $0$. So if $G$ is infinite cyclic then the value of $n$ does not matter; if $H$ is infinite cyclic then we must have $G$ infinite cyclic.
I'll sketch the case of $n=p^k\ (k>1)$, taking for granted that $(\mathbf Z/p\mathbf Z)^\times$ is cyclic.
Note that $\bigl\lvert\mathbf Z/p^k\mathbf Z\bigr\rvert=\varphi(p^k)=p^{k-1}(p-1)$.
One shows first, using the binomial formula that, if $p$ is an odd prime,
$$(1+p)^{p^k}=1+a_k p^{k+1},\quad \gcd(a_k, p)=1$$
From this fact we deduce $1+p$ has order $\;p^{k-1}\bmod p^k$.
Second, the multiplicative group of $\mathbf Z/p\mathbf Z$ is cyclic, of order $p-1$, hence there exists an integer $a$, $1<a<p$ of order $p-1\bmod p$, hence its order modulo $p^k$ is a multiple of $p-1$, so that one of its powers $b$ has order $p-1$.
Third, $b(1+p)$ has order $\operatorname{lcm}(p-1,p^{k-1})$, and as $p-1$ and $p^{k-1}$ are coprime, this order is equal to $(p-1)p^{k-1}$. Thus $(\mathbf Z/p^k\mathbf Z)^\times$ is generated by the class of $\;b(1+p)\bmod p^k$.
Best Answer
We can suppose the cyclic groups are $\mathbf Z/m\mathbf Z$ and $\mathbf Z/n\mathbf Z$ respectively. A homomorphism from the first to the second is determined by the choice of the image $\bar x$ of $\bar 1$, subject to the condition $m \bar x=0$, i.e. $$\DeclareMathOperator\Hom{Hom}\Hom(\mathbf Z/m\mathbf Z,\mathbf Z/n\mathbf Z) ≃ \operatorname{Ann}_{\mathbf Z/n\mathbf Z}(m).$$ Let $d=\gcd(m,n)$, $m'=\dfrac md$, $n'=\dfrac nd$. Since $m'$ and $n'$ are coprime, $$\operatorname{Ann}_{\mathbf Z/n\mathbf Z}(m)=n'\mathbf Z/n\mathbf Z ≃ \mathbf Z/d\mathbf Z.$$ Furthermore,