Pretty sure formal parameter doesn't help in this case. I never seen this concept in introductory material, but I looked it up and its seems like just a symbol with extra condition attached -- "can't be used on left side of = or :=".
This example supports this notion -- it fails exactly with the same error message as code in original post:
ClearAll[P, Y];
P[n_Integer, p_] = BinomialDistribution[n, p];
Y[d_] = Expectation[\[FormalX], \[FormalX] \[Distributed] P[d, 0.5]];
DiscretePlot[Y[\[FormalX]], {\[FormalX], 0, 20}, ExtentSize -> 0.8]
Using Module
works. Thank you very much. Maybe this idea makes sense -- a mode where "Module logic" is applied to every expression automatically, unless I specifically denote symbol as "belonging to outside". Smth like:
ClearAll[::P, ::Y];
P[n_Integer, p_] = ::BinomialDistribution[n, p];
Y[d_] = ::Expectation[x, x \[Distributed] ::P[d, 0.5]];
::DiscretePlot[::Y[x], {x, 0, 20}, ExtentSize -> 0.8]
Hmm... doesn't look very readable, but very explicit and hard to make the error I am having trouble with.