If a function has a finite limit at infinity, does that imply its derivative goes to zero? I've been thinking about this problem: Let $f: (a, +\infty) \to \mathbb{R}$ be a differentiable function such that $\lim\limits_{x \to +\infty} f(x) = L < \infty$. Then must it be the case that $\lim\limits_{x\to +\infty}f'(x) = 0$?
It looks like it's true, but I haven't managed to work out a proof. I came up with this, but it's pretty sketchy:
$$
\begin{align}
\lim_{x \to +\infty} f'(x) &= \lim_{x \to +\infty} \lim_{h \to 0} \frac{f(x+h)-f(x)}{h} \\
&= \lim_{h \to 0} \lim_{x \to +\infty} \frac{f(x+h)-f(x)}{h} \\
&= \lim_{h \to 0} \frac1{h} \lim_{x \to +\infty}[f(x+h)-f(x)] \\
&= \lim_{h \to 0} \frac1{h}(L-L) \\
&= \lim_{h \to 0} \frac{0}{h} \\
&= 0
\end{align}
$$
In particular, I don't think I can swap the order of the limits just like that. Is this correct, and if it isn't, how can we prove the statement? I know there is a similar question already, but I think this is different in two aspects. First, that question assumes that $\lim\limits_{x \to +\infty}f'(x)$ exists, which I don't. Second, I also wanted to know if interchanging limits is a valid operation in this case.
 A: Not true.  Here's how to construct a counterexample:
Let $f$ be a smooth function which satisfies the following:


*

*$f$ is zero everywhere except on intervals of the form $$ \left(n-\frac{1}{2n},n+\frac{1}{2n}\right), n \in \mathbb{N} $$

*On those intervals, smoothly rise from zero until $f(n)=\frac{1}{n}$, then fall back to zero.

*You can find the average slope on the interval $(n-\frac{1}{2n},n)$ is 2, and so by the Mean Value Theorem, this slope will be achieved on that interval.


Therefore, $f'(x)$ will not have limit zero since it will reach as high as 2 near each positive integer.
Addendum: interchanging limits is usually quite dangerous business.  The most important theorems in real analysis involve, at their core, special situations under which we are allowed to interchange limits.
A: The answer is: No. Consider $f(x)=x^{-1}\sin(x^3)$ on $x\gt0$. The derivative $f'(x)$ oscillates between roughly $+3x$ and $-3x$ hence $\liminf\limits_{x\to+\infty}\,f'(x)=-\infty$ and $\limsup\limits_{x\to+\infty}\,f'(x)=+\infty$.
A: There is a famous theorem known as Barbalat's lemma, which states the additional condition for $\lim_{x \to \infty} f'(x) = 0$. According to the lemma, $f'(x)$ should be uniformly continuous on $[a, \infty)$. In many applications, the uniform continuity of $f'(x)$ is shown by proving $f''(x)$ exists and is bounded on $[a, \infty)$.
(See Wikipedia https://en.wikipedia.org/wiki/Lyapunov_stability#Barbalat.27s_lemma_and_stability_of_time-varying_systems for the statement of Barbalat's lemma and its applications in stability analysis).
A: Take a function that is $0$ except in a small neighborhood of each positive integer; at $n\in\Bbb Z^+$ it has a smooth bump of height and width $1/n$ whose rising part has a maximum slope of $n$. This function is differentiable and has limit $0$ at infinity, but its derivative has no limit at infinity.
A: Because all counterxamples given here are oscillating, perhaps someone might wonder whether the proposition is true if we require the funcion to be monotonic. The answer is still no. Simply consider the inverse of the function, monotonic on $(0,\infty)$, given here $f(x)=\frac{1}{x}+\sin\left(\frac{1}{x}\right)$.
Also: consider the cumulative distribution function corresponding to 
a probability density function that has no limit as $x\to +\infty$. For example, the integral of the density constructed here. Or, using the same idea with a Cauchy distribution we get other example:
$$F(x)= \frac{2}{\pi}\sum_{k=1}^\infty \frac{\tan^{-1}\left((x-k) \pi 2^k\right)}{2^k} $$

This tends to $1$, but its derivative $f(x)=F'(x)= 2 \sum_{k=1}^\infty (\pi^2 2^{2k} (x-k)^2 +1)^{-1} $ has no limit, because $f(n)>2$ $\forall n \in \mathbb{N}$.

A: Let a function oscillate between $y=1/x$ and $y=-1/x$ in such a way that it's slope oscillates between $1$ and $-1$.  Draw the picture.  It's easy to see that such functions exist.  Then the function approaches $0$ but the slope doesn't approach anything.
One could ask: If the derivative also has a limit, must it be $0$?  And there, I think, the answer is "yes".
