Warning, j is implicitly declared local to procedure d - Maple Help

Home : Support : Online Help : Warning, j is implicitly declared local to procedure d

Warning, ... is implicitly declared local to procedure ...

 Description Maple displays this warning when you do not declare a variable as local or global, and it is assumed to be local. Unless otherwise declared, each variable to which an assignment is made, or that appears as the controlling variable in a loop, is assumed to be local. All other undeclared variables are assumed to be global. No warnings are displayed for variables assumed to be global.   For details, see the procedure help page.

Examples

Create a procedure that increments $x$. For example, $x$ can be a global counter that increments every time $f$ is called.

 > $x≔0;$
 ${x}{≔}{0}$ (2.1)
 > $f≔\mathbf{proc}\left(a\right)\phantom{\rule[-0.0ex]{0.3em}{0.0ex}}x;\phantom{\rule[-0.0ex]{0.3em}{0.0ex}}x:=x+a\phantom{\rule[-0.0ex]{0.0em}{0.0ex}}\mathbf{end proc};$
 ${f}{≔}{\mathbf{proc}}\left({a}\right)\phantom{\rule[-0.0ex]{0.5em}{0.0ex}}{\mathbf{local}}\phantom{\rule[-0.0ex]{0.5em}{0.0ex}}{x}{;}\phantom{\rule[-0.0ex]{0.5em}{0.0ex}}{x}{;}\phantom{\rule[-0.0ex]{0.5em}{0.0ex}}{x}{≔}{x}{+}{a}\phantom{\rule[-0.0ex]{0.5em}{0.0ex}}{\mathbf{end proc}}$ (2.2)

Call $f$.

 > $f\left(3\right);$
 ${x}{+}{3}$ (2.3)

Note that $x$ did not change.

 > $x;$
 ${0}$ (2.4)

Solution

In some cases the warning can be ignored. However, it is better to declare all variables local or global explicitly.

And in this example, to use $f$ to modify the global variable $x$, $x$ must be declared global.

 ${0}$ (2.5)

 > $\mathrm{restart}$
 > $x≔0;$
 ${x}{≔}{0}$ (2.6)
 >
 ${f}{≔}{\mathbf{proc}}\left({a}\right)\phantom{\rule[-0.0ex]{0.5em}{0.0ex}}{\mathbf{global}}\phantom{\rule[-0.0ex]{0.5em}{0.0ex}}{x}{;}\phantom{\rule[-0.0ex]{0.5em}{0.0ex}}{x}{≔}{x}{+}{a}\phantom{\rule[-0.0ex]{0.5em}{0.0ex}}{\mathbf{end proc}}$ (2.7)

Call $f$.

 > $f\left(3\right);$
 ${3}$ (2.8)

Note that $x$ changes.

 > $x;$
 ${3}$ (2.9)