Post Reply 
variable initial value - bug? (rev. 8151)
09-26-2015, 03:03 PM
Post: #1
variable initial value - bug? (rev. 8151)
Hi,

I created very simple program.
It just returns value of k variable, which is initially set to 1.

After first execution it returns 1 (as expected), but when it runs second time (without editing and compiling) it returns 0.
The k variable is local, so it should not remember value after program finishes.
Besides it is set initially to 1, so why the second run returns 0?
Then, if you go in edit mode and re-run the program it again will return 1.

Is it a bug?

Code:
local k=1;

EXPORT VAR_TEST()
BEGIN
 PRINT();
 PRINT(k);
 IF k=1 THEN
  k:=0;
 END;
END;

BR,
Piotr
Find all posts by this user
Quote this message in a reply
09-26-2015, 03:19 PM (This post was last modified: 09-26-2015 03:19 PM by DrD.)
Post: #2
RE: variable initial value - bug? (rev. 8151)
For starters, you need to assign the variable k the value of 1. You show:

local k=1;

It needs to be:

local k:=1;

-Dale-
Find all posts by this user
Quote this message in a reply
09-26-2015, 03:22 PM
Post: #3
RE: variable initial value - bug? (rev. 8151)
It did not help.

BR,
Piotr
Find all posts by this user
Quote this message in a reply
09-26-2015, 04:09 PM
Post: #4
RE: variable initial value - bug? (rev. 8151)
Now if you place the local statement within the program block it will be local only to the program, instead of the program's environment. This should work as you expected:

Code:
 
EXPORT VAR_TEST()
BEGIN
  local k:=1;  // Moved local statement here
  PRINT();
  PRINT(k);
  IF k=1 THEN
    k:=0;
  END;
END;
Find all posts by this user
Quote this message in a reply
09-26-2015, 04:11 PM (This post was last modified: 09-26-2015 04:13 PM by xset.)
Post: #5
RE: variable initial value - bug? (rev. 8151)
(09-26-2015 03:03 PM)komame Wrote:  Besides it is set initially to 1, so why the second run returns 0?
Then, if you go in edit mode and re-run the program it again will return 1.

Is it a bug?

No. It is local not to current invocation of the function, but to the current module (kinda private member). So it is normal that it stay alive between calls !
Find all posts by this user
Quote this message in a reply
09-26-2015, 04:33 PM
Post: #6
RE: variable initial value - bug? (rev. 8151)
If I am understanding you correctly, you are wondering why the value is not set to 1 on each execution.

If so, the reason here is simple. The variable is initialized *on compile time*. Nothing with your "local k:=1" is executed when you run your function. Rather, when you enter and then exit the editor you will see the value initialized because your program was parsed, functions created and loaded into the system and so on.

Basically, the only difference between EXPORT k:=1 and LOCAL k:=1 is that that the rest of the system can see the first one, while only the functions in your file can see the second.

TW

Although I work for HP, the views and opinions I post here are my own.
Find all posts by this user
Quote this message in a reply
09-26-2015, 06:24 PM
Post: #7
RE: variable initial value - bug? (rev. 8151)
Quote:Basically, the only difference between EXPORT k:=1 and LOCAL k:=1 is that that the rest of the system can see the first one, while only the functions in your file can see the second.

Does this mean that all local variables defined as program global scope are kept in memory and contain the last values set during the program execution?

I have noticed that sometimes going into the Program Catalog after editing my program its size is 15kb (the program creates a long list and saves it to a variable), but after I run it its size is increased to 20KB. After editing once again size is reduced to 15kb.
Is the reason for this is that after the end of the execution all the time in the memory are held values of some variables and they are only reinitialized after editing/parsign?

Piotr
Find all posts by this user
Quote this message in a reply
09-26-2015, 06:58 PM
Post: #8
RE: variable initial value - bug? (rev. 8151)
(09-26-2015 06:24 PM)komame Wrote:  Does this mean that all local variables defined as program global scope are kept in memory and contain the last values set during the program execution?

Correct. They stay and remain until you either delete the program, or reparse it and the contents are initialized again. If you don't specifically initialize, it will have a real number 0 as the content.

Quote:Is the reason for this is that after the end of the execution all the time in the memory are held values of some variables and they are only reinitialized after editing/parsign?

I believe so, yes.

TW

Although I work for HP, the views and opinions I post here are my own.
Find all posts by this user
Quote this message in a reply
09-28-2015, 06:55 AM
Post: #9
RE: variable initial value - bug? (rev. 8151)
Hello,

It's a question of scope.

local k:=1; // or k=1, the syntax is flexible there

since your statement is at the PROGRAM level, the scope of the k variable is program scope. So, this variable is accessible for the WHOLE program, every function thereof.
The variable is NOT accessible from outside of the program (unless fully qualified: program_name.k)

k will stay in memory until the program is deleted or changed.

Note that the local here is optional (default). The other option for a Program scope variable is EXPORT.
EXPORT k:=1;
Creates a variable k which will act EXACTLY like local k:=1; with one exception. You can use it from outside the program without having to fully qualify it.
That variable WILL appear in the variable catalog and you can type k on the command line and get its value.


What you are looking for is a Function scope local variable:
function(a)
begin
local k:=1;
return k+a;
end;

Here, k scope, and life expectancy is limited to the call to function. the variable is created when the local statement is encountered (it does not exist before), and it dies with the code block in which it is embedded.

so:
function(a)
if a<1 then
begin
local k:= 1;
return a+k;
else
local k:= 2;
return a+k;
end;
end;

Will create 1 or the other k variables, with values 1 or 2 depending on the case.
but once the end of the if clause is reached, then k stops to exist as its program block ends.

You can also create, in a function, a new program block, with a new k variable, which will 'take over' the k name for as long as its alive, with all the potential for trouble that it can bring, as in:

local a;
for a:=0 to 100 do
if a>50 then
local b:= a+1;
local a:= b+a;
print (a); // at a=50, prints 103 (b= a+1=52; new a= 52+51=103
// (yes, at the time of evaluation of the b+a, the new a
//does not YET exists))
end;
end;

Cyrille

Although I work for the HP calculator group, the views and opinions I post here are my own. I do not speak for HP.
Find all posts by this user
Quote this message in a reply
Post Reply 




User(s) browsing this thread: 1 Guest(s)