Warning, (in ...) `...` is implicitly declared local
Warning, `...` is implicitly declared local to procedure `...`
Description
Examples
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:
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;
Warning, (in powersofanumber) `i` is implicitly declared local
powersofanumber≔procnlocali;forito4doprint⁡n^iend doend proc
powersofanumber⁡3
3
9
27
81
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:
Use a local variable declaration. For more about this, see procedure.
powersofanumber := proc(n) local i; for i to 4 do n^i; end do; end proc;
powersofanumber≔procnlocali;forito4don^iend doend proc
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;
For more about this, see local_scopes.
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
f:=proc(a) x:=x+a end proc;
Warning, (in f) `x` is implicitly declared local
f≔procalocalx;x ≔ x+aend proc
Call f.
f⁡3
x+3
Note that x did not change.
x
0
Solution:
In this example, to use f to modify the global variable x, x must be declared global.
restart
f:=proc(a) local x; x:=x+a end proc;
Note that x changes.
Example 3
In the following function, defined with an arrow, an indexing variable is used (j).
f≔x↦seq⁡x,j=1..5
Warning, (in f) `j` is implicitly declared local
Solution
To avoid seeing the warning message, declare j as a local variable.
For introductory information on functions defined using arrows, see Functional Operators.
See Also
for...while...do
procedure
local_scopes
Download Help Document