# Is it safe to assign a variable and function of the same name for different things?

I'm writing out a notebook that goes through the van der Waals Equation of State for gases, and I run into a situation where I want to assign (simplified) Tc[b_]:=5b, use that to solve for b in terms of Tc with

b[Tc_]/.Solve[Tc==Tc[b],b][[1]]

Is it ok to assign Tc[b_] and still use Tc as a variable?

• If it is ok, is it possible to assign Tc[b_] and leave Tc blue instead of black, like other variables?

What about assigning Tc[b_] and Tc to different things?

How do the mechanics behind assigning things work in Mathematica such that I can assign Tc and not bother Tc[b_]?

Here's my full code, it works fine, I'm just wondering down the road if evaluating like this will work in a more complicated situation.

RGas = Quantity["MolarGasConstant"];

Pvan[V_, T_] := (R*T)/(V - b) - a/V^2
Pvan[V_, T_]' := D[Pvan[V, T], V]
Pvan[V_, T_]'' := D[Pvan[V, T]', V]

CriticalValues =
Solve[{Pvan[V, T] == P, Pvan[V, T]' == 0 == Pvan[V, T]''}, {V, T, P}]

Pc[a_, b_] = P /. CriticalValues[[1]]
Vc[a_, b_] = V /. CriticalValues[[1]]
Tc[a_, b_] = T /. CriticalValues[[1]]

Zc[Pc_, Vc_, Tc_] := (Pc[a, b]*Vc[a, b])/(R*Tc[a, b])
Zc[Pc, Vc, Tc]

abCritical = Solve[{Tc == Tc[a, b], Pc == Pc[a, b]}, {a, b}]
a[Tc_, Pc_] = a /. abCritical[[1]] /. R -> RGas;
b[Tc_, Pc_] = b /. abCritical[[1]] /. R -> RGas;
νc[Tc_, Pc_] := b[Tc, Pc]/4 /. R -> RGas;
σ[Tc_, Pc_] := ((3*νc[Tc, Pc])/(4*π*N₀))^(1/3) /.R -> RGas;

Solve[Eliminate[{P == Pvan[V, T], TR == T/Tc[a, b], VR == V/Vc[a, b],
PR == P/Pc[a, b]}, {P, T, V}], PR] // FullSimplify

It follows the first 3 pages of this.

• You may have trouble due to the fact that heads are evaluated before bodies. However, apart from this and other possible mistakes this choice may encourage, it is a viable approach. See the documentation for OwnValues and DownValues for more information. Feb 18, 2013 at 3:05
• Using OwnValues and DownValues I was able to evaluate some toy cases. I'm finding that anything which sets a OwnValues will supercede any DownValues (as you said, heads before bodies), so x=5 means x[y_] can't be used. My code works fine though because it never sets an OwnValues for b and Tc, it just uses them as symbols to manipulate. There's also UpValues or tags, which allow you to delay an evaluation of the left hand side, which I didn't know. There's also the Optional assignment, which allows you to define default values for variables. Feb 18, 2013 at 4:06
• FYI there are SubValues as well (and several other types of values not very useful to know at this stage), but these are undocumented. A subvalue rule is an assignment of the form h[args1___][args2___][...][argsn___] :> body. Feb 18, 2013 at 4:17

My personal opinion is that no, it is not safe to do this. I devoted a section in my book to this situation, where I warned against this, and in the 5 years since I wrote it I have seen nothing to contradict those conclusions I made there.

If you adopt this practice in your code, you will be inviting trouble in the future. Usually, things like this are rather hard to debug. This is because it normally does not come to mind that the problem can be caused by such a collision. It will surely be harder to maintain, and to remember after a while.