This has given me an inordinate amount of grief, I must admit.
The action $S$ of a mechanical system is defined by the integral, $S=\int L~dt$, where $L$ is the system's Lagrange function. In turn, if you know the action, you can compute stuff like energy and momenta, using the equations $H=-\partial S/\partial t$ and $\vec{p}=\partial S/\partial\vec{q}$. Sounds easy alright, until you plug in the numbers.
Take the simplest mechanical system, a freely moving point particle in one dimension. Its kinetic energy is defined by $K=mv^2/2$ and the potential energy is always zero, so $L=mv^2/2$. This is a "correct" expression in the sense that $L$ is defined as a function of positions, velocities, and time. Integrating this by $t$ we get $S=mtv^2/2$. Now before we take its partial derivatives, we must express $S$ as a function of the coordinates and time, right? That means $S=mq^2/2t$. Partial differentiation by $t$ gives us $-H=-mq^2/2t^2=-mv^2/2$, while partial differentiation by $q$ gives $p=mq/t=mv$, which is precisely the momentum.
Encouraged by the success so far, you may want to move on to the next problem: motion in a field characterized by a potential that is a linear function of distance. This will be like falling a homogeneous gravitational field. The potential energy of a falling particle will be proportional to the particle's height, i.e., it will decrease as the particle travels downward: $V=-amq$. The kinetic energy is the same as before, thus $E=mv^2/2-amq$. The Lagrangian, therefore, is $L=mv^2/2+amq$.
To make a long story short, no matter how you integrate the darn thing, chances are you'll never get it right: you'll never get back the expected values for the energy and momentum.
Why? The reason is simple: as we reconstruct $L$, and then $S$, from a known solution, it is not at all clear what we should express as a function of time, the coordinates, or the velocities. Since we already know the formulae, we can convert them back and forth, but what principle should guide us to choose, say, between $v$ or $at$, or between $q$ or $at^2/2$? Needless to say, depending on what choices we make, the partial derivatives will be quite different, resulting in values for the energy and momentum that have nothing to do with the real thing.
Fortunately, there's a way around it, by analyzing the problem a little. Let us express the Lagrangian of the system as $L=2K-E$ (no tricks, just some simple arithmetic here.) Integrating by $t$ (remember, $E$ is constant) we get $S=\int 2K~dt-\int E~dt$. Now when we partially differentiate with respect to $t$, we want to get $-E$ back. But this means that the first part, $\int 2K~dt$, must not be a function of time! Similarly, the momentum should be a function of the "$K$ part" only, i.e., the first part of the equation; that means that $\int E~dt$ must be expressed in such a way that the coordinates do not appear in the expression.
In other words, the prescription goes like this: Write up $L$ as $2K-E$, expressed as a function of $t$. Integrate by $t$. Now rewrite the $2K$ part, eliminating time. Partial differentiation with respect to $t$ will give back the total energy (times minus one), while partial differentiation with respect to $q$ will give the momentum.
Let's work it out in some practical cases:
First, a point particle moving freely in space. Once again, its kinetic energy is $K=mv^2/2$, so following our prescription, we take $L=2K-E=mv^2-mv^2/2$ ($v$ constant) and integrate by $t$, to get $S=mtv^2-mtv^2/2$. In the first part, we substitute (from $q=vt$) $t=q/v$, to get $S=mqv-mtv^2/2$. Differentiating with respect to $q$ gives $mv$, which is the correct value for the momentum; differentiating with respect to $t$ gives back $-mv^2/2$, which is the correct value for $-H$.
Next, let us again try the particle falling in a gravitational field: $K=mv^2/2$, $V=-amq$. (This means $v=at$ and $q=at^2/2$, which is what we're verifying.) Thus, $L=2K-E=mv^2-mv^2/2+amq=ma^2t^2-ma^2t^2/2+ma^2t^2/2$, since $v$ is not constant in this case. Integrating by $t$ we get $S=ma^2t^3/3$. (Only the $2K$ part remained in our equation, which is not at all a surprise; our initial conditions mean that the total energy in this case is zero.) To eliminate $t$, we must use $q=at^2/2$ again, from which $t=\sqrt{2q/a}$. Substituting into $S$, we get $S=2m\sqrt{2a}/3\cdot q^{3/2}$. Differentiation by $q$ gives $m\sqrt{2aq}$; noticing that $2aq=2a^2t^2/2=v^2$, we get $mv$, which is the momentum.
Which leads us to the final problem: let's do this exercise again, but this time using initial values, so that $v=v_0+at$ and $q=q_0+v_0t+at^2/2$. Well, you know what? I did the exercise on paper, and that's enough for me. The momentum works out to $mv$, (correct), and the energy works out to $-maq_0+mv_0^2/2$, which is also the correct value.
There is, I must admit, a bit of a dirty secret behind this analysis, however. All this time, I pretended that $S$ is a function (of variables such as time or the coordinates) as opposed to a functional, which maps functions to numbers. The functions, in this case, would be the possible paths of the particle in spacetime, along which we integrate the Lagrangian: $S=\int L~dt$, where the integral must be performed along a path parameterized by the time $t$, between $t_1$ and $t_2$. What we vary is the path, as we seek to find the "right" path for which $S$ i s minimal (or extremal.) But it is also possible to view $S$ as a "proper" function: once $L$ is known, and $t_1=t_0$, $S$ is a function of time and coordinates as we vary $t_2$, i.e., the second integration endpoint. It is in this sense that $S$ was seen as a function in the analysis above.