VarGet() always accesses GLOBAL variables, was: not working within a procedure

This is returning empty

_TRACE("!CLEAR!"); 

procedure abc(){
abc1=100; abc2=200;
for( i = 1; i < 3; i++ ) 
{ 
_Trace("abc="+VarGet( "abc"+i )); 
} 
}
abc();

But below works

_TRACE("!CLEAR!"); 
abc1=100; abc2=200;
for( i = 1; i < 3; i++ ) 
{ 
_Trace("abc="+VarGet( "abc"+i )); 
}

VarSet/VarGet create/access global variables!
So you have to define abc… as global if you want to read them via VarGet.

_TRACE( "!CLEAR!" );

procedure abc()
{
    global abc1, abc2;

    abc1 = 100;
    abc2 = 200;

    for( i = 1; i < 3; i++ )
    {
        _TRACE( "abc=" + VarGet( "abc" + i ) );
    }
}

abc();
1 Like

Thanks this solved the problem.

Basic query about varset & varget ….

--VarGet()--

function VargetTesting(Nloop,f1,f2,f3)
{
    for(i=1; i<=Nloop; i++ ) 
    {
    .......
    .......
    Plot(VarGet( "f"+i ),"",colorRed,styleLine | styleNoRescale); // plot nothing...
    }
}

VargetTesting(3,50,55,60);

--VarSet()--

function VarsetTesting(LoopNo,f1,f2,f3)
{
    for(i=1; i<=LoopNo; i++)
    {
    VarSet( "T"+i, "f"+i (..wrong..)  ); // retrieve f1,f2,f3 value automatic via loop … how?
    .......
    .......
    }
}

VarsetTesting(3,50,55,60);

As written above - VarGet always accesses GLOBAL variables. Function arguments are local, not global and this makes your code incorrect.

@Tomasz,
yes,.. if done this way then plot perfect....

global ff1,ff2,ff3;
ff1=f1;ff2=f2;ff3=f3;

//loop
Plot(VarGet( "ff"+i ),.........); //plot ...

but may be silly of me to think there's a way to use function declaration "f1”(..above example..) like this .. VarGet( "f"+i ) ; .....

function VargetTesting(f1)
{
//global ff1;
// without initializing ff1=f1;
VarGet( "f"+i );
}

VarAvailability_001

Why is variable "giraffe" not initialized within the first function helloWorldBeforeGiraffe() but properly accessed and returned within the second function helloWorldAfterGiraffe() even though VarSet( "giraffe", 60 ) is called chronologically BEFORE both function calls ? The only difference is that function definition is once before and in the second case after the VarSet( "giraffe", 60 ) call. Thanks for your help and more understanding

1 Like

Variable giraffe has to be known before function definition in that case.
Also it is not really good code writing practice to hide variables.

What you may do (to remove error) is this

function helloWorldBeforeGiraffe()
{
  tmpWithinFunction_001 = VarGet("zebra");
  tmpWithinFunction_002 = VarGet("giraffe");
  printf( "%g, %g", tmpWithinFunction_001, tmpWithinFunction_002);
  tmp_001 = 1;
}

VarSet("zebra", 50);
VarSet( "giraffe", 60);

helloWorldBeforeGiraffe();

or

function helloWorldBeforeGiraffe()
{
  global zebra, giraffe;
  tmpWithinFunction_001 = zebra;
  tmpWithinFunction_002 = giraffe;
  printf( "%g, %g", tmpWithinFunction_001, tmpWithinFunction_002);
  tmp_001 = 1;
}

VarSet("zebra", 50);
VarSet("giraffe", 60);

helloWorldBeforeGiraffe();

But better this

function helloWorldBeforeGiraffe( zebra_str, giraffe_str )
{
  tmpWithinFunction_001 = VarGet(zebra_str);
  tmpWithinFunction_002 = VarGet(giraffe_str);
  printf( "%g, %g", tmpWithinFunction_001, tmpWithinFunction_002);
  tmp_001 = 1;
}

VarSet("zebra", 50);
VarSet("giraffe", 60);

helloWorldBeforeGiraffe( "zebra", "giraffe" );

or

function helloWorldBeforeGiraffe( zebra, giraffe )
{
  tmpWithinFunction_001 = zebra;
  tmpWithinFunction_002 = giraffe;
  printf( "%g, %g", tmpWithinFunction_001, tmpWithinFunction_002);
  tmp_001 = 1;
}

VarSet("zebra", 50);
VarSet("giraffe", 60);

helloWorldBeforeGiraffe( zebra, giraffe );

Also please add code via code tags not via picture only. It is tiresome to rewrite function for explaining since it is you looking for help.

1 Like

thanks for the help. I will post the code the next time as code and not as image no problem.