Question about passing parameters by reference

Hello all, and best wishes for 2022.

I have a question about passing parameters by reference. I am testing passing parameters by reference to a called function, as described here. I have also fiund other postshere on the forum similar to my question.

In my case, I have a call to function1, which in turn calls function2. The variables passed by reference are set in function2, which is passed back to functiion1, and then back to the calling AFL.
I seems that passing by references only works to one level.

Here is sample code

function  MyTest2( retTest2Str, retTest2Num)
{
  str2 = "Test 2 result";
  num2 = 2;

  _TRACE("MyTest2 before: retTest2Str " + "'" + str2 + "'" + "/" + "'" + retTest2Str + "'" + "/" + "'" + VarGetText(retTest2Str) + "'");
  _TRACE("MyTest2 before: retTest2Num " + num2 + "/" + retTest2Num + "/" + VarGet(retTest2Num));

  VarSetText(retTest2Str, str2);
  VarSet(retTest2Num, num2);

  _TRACE("MyTest2 after: retTest2Str " + "'" + str2 + "'" + "/" + "'" + retTest2Str + "'" + "/" + "'" + VarGetText(retTest2Str) + "'");
  _TRACE("MyTest2 after: retTest2Num " + num2 + "/" + retTest2Num + "/" + VarGet(retTest2Num));
}

function  MyTest1(retTest1Str, retTest1Num)
{
  str1 = "Test 1 result";
  num1 = 1;

  _TRACE("MyTest1 before: retTest1Str " + "'" + str1 + "'" + "/" + "'" + retTest1Str + "'" + "/" + "'" + VarGetText(retTest1Str) + "'");
  _TRACE("MyTest1 before: retTest1Num " + num1 + "/" + retTest1Num + "/" + VarGet(retTest1Num));

  MyTest2("str1", "num1");
  VarSetText(retTest1Str, str1); 
  VarSet(retTest1Num, num1);
  
  _TRACE("MyTest1 after: retTest1Str " + "'" + str1 + "'" + "/" + "'" + retTest1Str + "'" + "/" + "'" + VarGetText(retTest1Str) + "'");
  _TRACE("MyTest1 after: retTest1Num " + num1 + "/" + retTest1Num + "/" + VarGet(retTest1Num));
}

testResultStr = "Initial result";
testResultNum = 0;
_TRACE("testResultStr before: " + "'" + testResultStr + "'");
_TRACE("testResultNum before: " + testResultNum);
MyTest1("testResultStr", "testResultNum");
_TRACE("testResultStr after: " + "'" + testResultStr + "'");
_TRACE("testResultNum after: " + testResultNum);

The following output is generated

testResultStr before: 'Initial result'
testResultNum before: 0

MyTest1 before: retTest1Str 'Test 1 result'/'testResultStr'/'Initial result'
MyTest1 before: retTest1Num 1/testResultNum/0

MyTest2 before: retTest2Str 'Test 2 result'/'str1'/''
MyTest2 before: retTest2Num 2/num1/{EMPTY}
MyTest2 after: retTest2Str 'Test 2 result'/'str1'/'Test 2 result'  => This is OK
MyTest2 after: retTest2Num 2/num1/2  => This is OK

MyTest1 after: retTest1Str 'Test 1 result'/'testResultStr'/'Test 1 result'  => This should display 'Test 2 result'
MyTest1 after: retTest1Num 1/testResultNum/1 => This should display 2

testResultStr after: 'Test 1 result'
testResultNum after: 1

Is this a bug, or am I doing something wrong?

Many thanks

Passing by reference is native now and you don't need all of that VarSet.

function Test( arg1, arg2 )
{
 arg1 = 7;
 arg2 = 8;
}

var1 = 1;
var2 = 2;
printf("%g, %g\n", var1, var2 );

Test( &var1, &var2 ); // pass references
printf("%g, %g\n", var1, var2 );
1 Like

Many thanks Thomas,

Good news, makes my life much simpler.
You need to update that webpage I referred to in my original post

@Tomasz would you like to introduce "Method Overloading" in AFL?
Or is there a trick already?

Thanks in advance

This topic was automatically closed 100 days after the last reply. New replies are no longer allowed.