# How do you set an Optional parameter with a global variable on a Function defined in a Package

In a Package I am writing, I'm trying to define a function with an Optional parameter in it that is set to a global variable.

PlotIndex[IndexLoopPlotMtx_:IndexLoopPlot]:=ListPlot[.....


However, the Optional value IndexLoopPlot is not set until after the package is loaded and other functions are run. Therefore PlotIndex is read in as,

PlotIndex[IndexLoopPlotMtx_:{}]:=ListPlot[.....


In other words, IndexLoopPlotMtx is set to Null, and when the function is run later it tries to plot {} instead of the matrix IndexLoopPlot. Is there a way to get it delay setting the Default until the function is called for the first time from the package?

-

## migrated from math.stackexchange.comApr 12 '12 at 2:42

This question came from our site for people studying math at any level and professionals in related fields.

You'll have to point to the global variable using its full context path as GlobalIndexLoopPlot. Otherwise, the optional variable will be interpreted as YourPackageIndexLoopPlot. The following example shows how:
Quiet@Remove[a, "test*"];
f[x_: Globala] := {x}
`