Warning, (in sineExpr) t is implicitly declared local - Maple Help

Home : Support : Online Help : Warning, (in sineExpr) t is implicitly declared local

Warning, (in ...) ... is implicitly declared local

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.   In some cases the warning can be ignored. However, it is better to declare all variables local or global explicitly.    Note: In Maple 2018 and earlier versions, the warning message was slightly different:  Warning, ... is implicitly declared local to procedure ...

Examples

Example 1

Create a procedure that uses a for loop to find the sum of the first 4 powers of the input:

 > powersofanumber := proc(n) for i to 4 do print(n^i); end do; end proc;
 ${\mathrm{powersofanumber}}{≔}{\mathbf{proc}}\left({n}\right)\phantom{\rule[-0.0ex]{0.5em}{0.0ex}}{\mathbf{local}}\phantom{\rule[-0.0ex]{0.5em}{0.0ex}}{i}{;}\phantom{\rule[-0.0ex]{0.5em}{0.0ex}}{\mathbf{for}}\phantom{\rule[-0.0ex]{0.5em}{0.0ex}}{i}\phantom{\rule[-0.0ex]{0.5em}{0.0ex}}{\mathbf{to}}\phantom{\rule[-0.0ex]{0.5em}{0.0ex}}{4}\phantom{\rule[-0.0ex]{0.5em}{0.0ex}}{\mathbf{do}}\phantom{\rule[-0.0ex]{0.5em}{0.0ex}}{\mathrm{print}}{}\left({n}{^}{i}\right)\phantom{\rule[-0.0ex]{0.5em}{0.0ex}}{\mathbf{end do}}\phantom{\rule[-0.0ex]{0.5em}{0.0ex}}{\mathbf{end proc}}$ (2.1)
 > $\mathrm{powersofanumber}\left(3\right)$
 ${3}$
 ${9}$
 ${27}$
 ${81}$ (2.2)

Since a variable (i) is used in the procedure and not declared as either global or local, it is implicitly declared local.  To avoid this message, make a local variable declaration explicitly.

Solution 1:

 > powersofanumber := proc(n) local i; for i to 4 do n^i; end do; end proc;
 ${\mathrm{powersofanumber}}{≔}{\mathbf{proc}}\left({n}\right)\phantom{\rule[-0.0ex]{0.5em}{0.0ex}}{\mathbf{local}}\phantom{\rule[-0.0ex]{0.5em}{0.0ex}}{i}{;}\phantom{\rule[-0.0ex]{0.5em}{0.0ex}}{\mathbf{for}}\phantom{\rule[-0.0ex]{0.5em}{0.0ex}}{i}\phantom{\rule[-0.0ex]{0.5em}{0.0ex}}{\mathbf{to}}\phantom{\rule[-0.0ex]{0.5em}{0.0ex}}{4}\phantom{\rule[-0.0ex]{0.5em}{0.0ex}}{\mathbf{do}}\phantom{\rule[-0.0ex]{0.5em}{0.0ex}}{n}{^}{i}\phantom{\rule[-0.0ex]{0.5em}{0.0ex}}{\mathbf{end do}}\phantom{\rule[-0.0ex]{0.5em}{0.0ex}}{\mathbf{end proc}}$ (2.3)

Solution 2:

You can declare a variable as local right where it is used.  In this case, since i is used in the loop, you can declare i local in the loop control clause:

 > powersofanumber := proc(n) for local i to 4 do n^i; end do; end proc;
 ${\mathrm{powersofanumber}}{≔}{\mathbf{proc}}\left({n}\right)\phantom{\rule[-0.0ex]{0.5em}{0.0ex}}{\mathbf{local}}\phantom{\rule[-0.0ex]{0.5em}{0.0ex}}{i}{;}\phantom{\rule[-0.0ex]{0.5em}{0.0ex}}{\mathbf{for}}\phantom{\rule[-0.0ex]{0.5em}{0.0ex}}{i}\phantom{\rule[-0.0ex]{0.5em}{0.0ex}}{\mathbf{to}}\phantom{\rule[-0.0ex]{0.5em}{0.0ex}}{4}\phantom{\rule[-0.0ex]{0.5em}{0.0ex}}{\mathbf{do}}\phantom{\rule[-0.0ex]{0.5em}{0.0ex}}{n}{^}{i}\phantom{\rule[-0.0ex]{0.5em}{0.0ex}}{\mathbf{end do}}\phantom{\rule[-0.0ex]{0.5em}{0.0ex}}{\mathbf{end proc}}$ (2.4)

Example 2

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.5)
 > f:=proc(a) x:=x+a 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}{≔}{x}{+}{a}\phantom{\rule[-0.0ex]{0.5em}{0.0ex}}{\mathbf{end proc}}$ (2.6)

Call $f$.

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

Note that $x$ did not change.

 > $x$
 ${0}$ (2.8)

Solution:

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

 ${0}$ (2.9)
 > $\mathrm{restart}$
 > $x≔0$
 ${x}{≔}{0}$ (2.10)
 > f:=proc(a) local x; x:=x+a 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}{≔}{x}{+}{a}\phantom{\rule[-0.0ex]{0.5em}{0.0ex}}{\mathbf{end proc}}$ (2.11)

Call $f$.

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

Note that $x$ changes.

 > $x$
 ${0}$ (2.13)

Example 3

In the following function, defined with an arrow, an indexing variable is used (j).

 > $f≔x→\mathrm{seq}\left(x,j=1..5\right)$
 ${f}{≔}{x}{↦}{\mathrm{seq}}{}\left({x}{,}{j}{=}{1}{..}{5}\right)$ (2.14)

Solution

To avoid seeing the warning message, declare j as a local variable.

 > $f≔x→\mathrm{seq}\left(x,j=1..5\right)$
 ${f}{≔}{x}{↦}{\mathrm{seq}}{}\left({x}{,}{j}{=}{1}{..}{5}\right)$ (2.15)

For introductory information on functions defined using arrows, see Functional Operators.