# Why use \[FormalN]?

Through a copy of some code from an answer on this site I discovered the existence of:

\[FormalN]


The documentation describes it as follows:

"Used to represent a formal parameter that will never be assigned a value."


Can you give an example of why, how and where this should be applied?

A simple example of the use of formal symbols is for mathematical tasks that require the use of dummy variables. Most functions like Plot will localize their dummy variables, but not all. For example, LinearModelFit will not work with symbols that already have a value assigned to them:

x = 1;
LinearModelFit[RandomReal[1, {10, 2}], {1, x}, x]


(spits out errors)

This is where the formal symbols come in handy. Because they can never have a value assigned to them (under normal circumstances), they are ideal for this sort of thing:

LinearModelFit[RandomReal[1, {10, 2}], {1, \[FormalX]}, \[FormalX]]


The code above will always run because you can be sure that \[FormalX] is not going to have a value.

edit

As pointed out in a comment, you can just Clear[x] before using a function like LinearModelFit if you're certain that this will not be a problem elsewhere. However, you can come up with very specific situations where things can still go wrong with that. For example, if you put a slider like Slider[Dynamic[x], {0, 1}] in your notebook, manipulating that slider can affect running computations in the kernel because dynamic elements interrupt the kernel when they need to be updated. For example, try this:

Slider[Dynamic[x]]
Do[Print[x]; Pause[1], {10}]


You'll see that the printed values of x change when you manipulate the slider. You can imagine that this could happen during computation that takes several seconds to complete. It's not very likely to happen, but if you use \[FormalX] for your fit you'll know for certain that it won't.

• Is the use of [FormalX] better than, or preferred over: ClearAll[x]; LinearModelFit[RandomReal[1, {10, 2}], {1, x}, x] – nilo de roock Nov 6 '19 at 18:31
• Sjoerd, If I may ask: Are you a Dutch guy studying at Oxford? – nilo de roock Nov 6 '19 at 18:32
• Well, you may not want to clear x, right? Who knows what that messes up in a bigger program. If you're working in your own research notebook it won't matter much, but in a bigger project you may not know what symbols are free to use. Also: yes, I'm Dutch and I do live in Oxford, but I'm long done studying :). – Sjoerd Smit Nov 6 '19 at 19:48

These are typically used when you are making symbolic transformations and you don't want to risk failure from existing global definitions. For example:

Sin'[\[FormalX]]

D[Sin[\[FormalX]], \[FormalX]]

Integrate[Sin[\[FormalX]], {\[FormalX], 0, \[FormalY]}]

Sum[Sin[t], {t, 0, \[FormalX]}]

Limit[Sin[\[FormalX]]/\[FormalX], \[FormalX] -> 0]


All of these will fail if instead of \[FormalX] one uses a symbol that has an assigned numeric value.

In some cases one could use Block instead to prevent contamination, e.g.:

x = 1.23;
Block[{x},
Limit[Sin[x]/x, x -> 0]
]


However, if the output expression needs to include a Symbol, e.g. Cos[\[FormalX]] you might as well start with Formal Symbols and handle both problems at once.

It is worth noting that while one cannot directly assign to formal symbols you can still give them values using scoping constructs like With, Block, and Table:

With[{\[FormalX] = 5}, \[FormalX] + 1]
Block[{\[FormalX] = 5}, \[FormalX] + 1]
Table[\[FormalX] + 1, {\[FormalX], 3}]

6

6

{2, 3, 4}


These are considered special and used by System functions. Bad things could happen if values are assigned to them.

DifferenceRootReduce[n^2 + 1, n]

(* DifferenceRoot[Function[{\[FormalY], \[FormalN]}, {(-2 -
2 \[FormalN] - \[FormalN]^2) \[FormalY][\[FormalN]] + (1 + \[FormalN]^2) \[FormalY][1 + \[FormalN]] == 0, \[FormalY][0] ==
1}]][n] *)

TransferFunctionModel[1]
(* TransferFunctionModel[{{{1}}, 1}, \[FormalS]] *)
`

If you unprotect and assign values to them, you get unexpected or even incorrect results.