25
$\begingroup$

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

$x=4 
$\endgroup$
3
  • 6
    $\begingroup$ Nothing really special; usually it is used for denoting global constants, e.g. $MachineEpsilon, but you can certainly start your variables with a dollar sign. $\endgroup$ Commented May 14, 2013 at 13:00
  • $\begingroup$ Thanks a lot. At last something useful. $\endgroup$ Commented May 14, 2013 at 13:01
  • 3
    $\begingroup$ See also tutorial/BasicObjects in the free documentation... :) $\endgroup$ Commented May 14, 2013 at 13:16

1 Answer 1

27
$\begingroup$

$ 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.

$\endgroup$
3
  • $\begingroup$ @Ajasja: thanks for the link, I remember now that I read that once, but couldn't remember... $\endgroup$ Commented May 14, 2013 at 16:38
  • 1
    $\begingroup$ You'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]} $\endgroup$ Commented May 14, 2013 at 16:47
  • 1
    $\begingroup$ Here's another example of Function using $ internally, causing confusion to the user: mathematica.stackexchange.com/q/25563/5 $\endgroup$ Commented May 20, 2013 at 21:58

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.