# Why are some function names red?

I need to evaluate

Needs["GraphUtilities"]


before I can use functions such as GraphPath[]. When I do so, some functions get colored red, but still work.

Why does this happen and can I change this?

-
I think its worth to mention that Mathematica sometimes, in specific cases, mistakenly colors variables red, though they are not shadowed. It just means that the syntax-checker is not perfect. –  István Zachar Mar 21 '12 at 9:19
@IstvánZachar That's not the same red as shadowing — that usually happens if the scope of a variable is ambiguous or there's a possible collision. Try f[x_] := Module[{x}, x]. You can actually change these colours in Preferences > Appearance > Syntax colouring > Errors and warnings. By default they're all the same damn shade of red (I have different ones for each). CHM, to answer the last line of your question, the above is where you can change this colour. –  The Toad Mar 21 '12 at 14:03
Thanks for the clarification! Though never really payed attention to these, now I have the means to suppress them. –  István Zachar Mar 21 '12 at 14:39
@CHM: which functions are colored red? I seem to recall that sometimes this coloring is just an oversight in declaring some names for packages (at least that's what I remember somebody from WRI responding elsewhere to a query of the same sort). –  murray Mar 21 '12 at 19:22

The red colouring indicates shadowing — i.e., when a symbol originally in a particular context, is exposed to the current context path, thereby clashing with another symbol of the same name in a different context, also on the context path.

Here is a short example that demonstrates this. Try it out in a fresh kernel (call Quit[] before you try it).

(* Define f in the test context *)
Begin["test"]
f[x_] := x^2
End[]

(* Define f in the Global context *)
f[y_] := y^3


So far, all's well and you see the symbols coloured the way you're used to. Now expose testf by adding it to the context path with:

$ContextPath = Prepend[$ContextPath, "test"]


You'll now notice that both the f turned red to indicate that it is being shadowed by a different definition.

### Which definition is used in case of a conflict?

The definition that is used first, in the case of shadowing, is the one belonging to the context that appears first in $ContextPath. This is the same behaviour as in your unix shells, where directories in the $PATH variable are searched in the order they appear.

In the above example, I used Prepend, which would've placed test first in the list. So the definition of f will be testf and you can easily test this:

f[x]
Out[1]= x^2


Now quit your kernel and do the same, except now you place the new context at the end. You can see that the definition used is that of Globalf.

$ContextPath = Append[$ContextPath, "test"]
f[x]
Out[2]= x^3


Incidentally, it is also possible to trigger this shadowing warning (red colour) even if there isn't a real conflict. This can occur if the same context appears at different positions in $ContextPath. To test this, in a clean kernel, define testf as before, and Append and Prepend it to $ContextPath.

### Changing the colour of the warning

To change the colour from red to your favourite, go to Preferences > Appearance > Syntax Coloring > Errors and Warnings and change the colour in the very last option:

To do it programmatically, evaluate:

SetOptions[$FrontEndSession, AutoStyleOptions -> {"SymbolShadowingStyle" -> {FontColor -> Orange}}]  Replace Orange with the color of your choice and $FrontEndSession with \$FrontEnd if you want the change to persist across future sessions (it essentially writes it to the FE's init.m`). See this answer for more on the various style tokens and what they control.

-
Nice touch to link to the Mathematica Journal. –  Yves Klett Mar 21 '12 at 7:05