What is $ sign in Mathematica?

22

3

Is it a special sign or it can be used as anything else? For creating variables for example.

$x=4

Misery

Posted 2013-05-14T12:56:36.017

Reputation: 2 390

6Nothing really special; usually it is used for denoting global constants, e.g. $MachineEpsilon, but you can certainly start your variables with a dollar sign. – J. M.'s ennui – 2013-05-14T13:00:21.130

Thanks a lot. At last something useful. – Misery – 2013-05-14T13:01:51.570

3

See also tutorial/BasicObjects in the free documentation... :)

– cormullion – 2013-05-14T13:16:34.567

Answers

25

$ is probably the only non-alphanumeric ascii character without a special meaning in Mathematica and thus the only one you could use as a delimiter for various parts within a variable name.

A warning is due: Because it is so unique, it is also used internally for the same purpose, e.g. Module and Unique will generate variable names ending in $+ an arbitrary integer:

Unique[]
Module[{x},x]

In some cases, also Function will generate variable names ending in $ for its arguments. You can read more about this here. To stay out of potential problems I would not use variables ending in $ or $ + integer.

Another case where $ seems to be used as some kind of internal convention are symbols which serve as global variables like $FrontEnd,$Context, $Path ..., but you can avoid conflicts by using a lowercase letter for the second letter of the variable name.

Other than the mentioned cases, I wouldn't expect problems when using $ within variable names. It should be mentioned that due to the possibility to use non-ascii letters within variable names, people often use such characters for similar purposes, e.g.:

long\[Bullet]name

which will look nicer in the frontend, but won't render nice in the Wolfram Workbench editor.

Albert Retey

Posted 2013-05-14T12:56:36.017

Reputation: 22 455

@Ajasja: thanks for the link, I remember now that I read that once, but couldn't remember... – Albert Retey – 2013-05-14T16:38:51.133

1You're welcome. You were right. Using $ at the end of a name can cause problems: f = Function[{x}, Function[{y}, x + y]]; o2 = f[2 y$]; o1 = f[2 y]; {o1[a], o2[a]} – Ajasja – 2013-05-14T16:47:13.777

1

Here's another example of Function using $ internally, causing confusion to the user: http://mathematica.stackexchange.com/q/25563/5

– rm -rf – 2013-05-20T21:58:28.910