Functional Derivative of action Consider the action of free Klein-Gordon theory
$S[\phi]=\frac{1}{2}\displaystyle\int d^4y(\partial_\mu\phi(y)\partial^\mu\phi(y)-m^2\phi^2(y))$
Integrating by parts in the first term gives me 
$S[\phi]=-\frac{1}{2}\displaystyle\int d^4y(\phi(y)\partial_\mu\partial^\mu\phi(y)+m^2\phi^2(y))$
Now I take the functional derivative $\frac{\delta S[\phi]}{\delta\phi(x)}$ to get 
$\frac{\delta S[\phi]}{\delta\phi(x)}=-\frac{1}{2}(\partial_\mu\partial^\mu\phi(x)+2m^2\phi(x))$
Setting this equal to zero should give me the Klein-Gordon equation, but the first term is off by a factor of 2. What's wrong with what I've done? 
I know there are other ways to do this. I want to know what's wrong with this way.
 A: Let me give a pedagogical answer. You're confused about the functional derivative
$$ \frac{\delta}{\delta \phi(y)}\int dx\, \phi(x) \partial^2 \phi(x).$$
We can compute such derivatives by perturbing a functional:
$$ F[\phi + \epsilon \chi] = F[\phi] + \epsilon \int dx \, \frac{\delta F[\phi]}{\delta \phi(x)}\chi(x) + O(\epsilon^2).$$
Now let $F[\phi] = \int dx\, \phi(x) \partial^2 \phi(x)$. Then
$$  F[\phi + \epsilon \chi] - F[\phi] = \epsilon \int dx \left[ \chi(x) \partial^2 \phi(x) + \phi(x) \partial^2 \chi(x) \right] + O(\epsilon^2). $$
But this is not of the correct form, due to the $\partial^2 \chi(x)$ term. However, you can always integrate by parts (exercise):
$$ \int dx \,  \phi(x) \partial^2 \chi(x)  = \int dx \,  \chi(x) \partial^2 \phi(x) \, + \, \text{boundary terms}.$$
By assumption, the boundary terms do not contribute. Bringing everything together, we can rewrite the formula above as
$$ F[\phi + \epsilon \chi] - F[\phi]= 2 \times \epsilon \int dx \, \chi(x) \partial^2 \phi(x)  + O(\epsilon^2).$$
At this point, we conclude that
$$ \frac{\delta F[\phi]}{\delta \phi(x)} = 2 \partial^2 \phi(x). $$
A: Consider $$ A = \frac{\delta}{\delta \phi(x)} \int d^{4}y \phi(y) \partial^{2} \phi(y) $$
Using the Leibniz property,
$$A = \int d^4 y \Big[\frac{\delta \phi(y)}{\delta \phi(x)} \partial^{2} \phi(y)  + \phi(y) \frac{\delta}{\delta \phi(x)} \partial^{2} \phi(y)\Big] $$
Using the fact that partial and functional derivatives commute, we have:
$$ A = \int d^4 y \Big[\delta^{(4)} (y-x) \partial^{2} \phi(y)  + \phi(y)\partial^{2} \Big( \frac{\delta}{\delta \phi(x)}  \phi(y) \Big)\Big]$$
Thus,
$$ A =  \partial^{2} \phi(x)  +  \int d^{4}y \phi(y)\partial^{2} \Big( \delta^{(4)}(x-y) \Big)$$
In the second term, the second derivative is on the wrong function. It should be on $\phi(y)$. 
Integrate by parts and use the fact that both $\phi$ and $\delta^{(4)}$ disappear at the boundaries to show that
$$A = 2 \partial^{2} \phi(x) $$
