Arguments are passed by value by default, was: Matrix not keeping

image
Hi everyone,
I am now a registered user.

I am creating several matrixes.
To load them I have a function called several times but when entering on subsequent calls the previous values loaded into the matrix is lost.
image

Must be something simple but eludes me at this stage of my learning. I have spent considerable time searching for the solution but with no luck.

Thanks in advance,
Gary.

global BasicCandles;
global basicNames;
global basicBullOrBear;
global basicCount;

global SimplePatterns;
global simpleNames;
global simpleBullOrBear;
global simpleCount;

global ComplexPatterns;
global complexNames;
global complexBullOrBear;
global complexCount;

basicCount = simpleCount = complexCount = 0;

procedure createMatrix()
{
    rows = 200;
    cols = 2;

    BasicCandles = Matrix( rows, cols, 0 );
    basicNames = "";
    basicBullOrBear = "";

    SimplePatterns = Matrix( rows, cols, 0 );
    simpleNames = "";
    simpleBullOrBear = "";

    ComplexPatterns = Matrix( rows, cols, 0 );
    complexNames = "";
    complexBullOrBear = "";

}


procedure addToMatrix( matrixId,  patternName, rank, bullOrBear )
{
    global i;
    
    _trace ("1  " + MxToString( matrixId ) );

    if( matrixId == basicCandles )
    {
        basicNames = basicNames + patternName + ",";
        basicBullOrBear = basicBullOrBear + bullOrBear + ",";
        basicCount++;
        nameI = basicCount;
    }
    else
        if( matrixId == simplePatterns )
        {
            simpleNames = simpleNames + patternName + ",";
            simpleBullOrBear = simpleBullOrBear + bullOrBear + ",";
            simpleCount++;
            nameI = simpleCount;
        }
        else   // matrixId == complexPatterns
        {
            complexNames = complexNames + patternName + ",";
            complexBullOrBear = complexBullOrBear + bullOrBear + ",";
            complexCount++;
            nameI = complexCount;
        }

    matrixId[i][0] = rank;
    matrixId[i][1] = nameI;
    _trace ("2  " + MxToString( matrixId ) );
    i++;
}
procedure loadTables()
{
    i = 0;
    addToMatrix( BasicCandles,  "White Candle", 1, 0 );
    addToMatrix( BasicCandles,  "Black Candle", 2, 0 );
    addToMatrix( BasicCandles,  "Doji", 3, 0 );
    addToMatrix( BasicCandles,  "Four Price Doji", 4, 0 );

I appear to have solved my problem by referring to the matrix directly;
Not sure why, but for now I will continue.

Cheers.

if( matrixId == basicCandles )
    {
        basicNames = basicNames + patternName + ",";
        basicBullOrBear = basicBullOrBear + bullOrBear + ",";
        basicCount++;
        nameI = basicCount;
        basicCandles[i][0] = rank;
        basicCandles[i][1] = nameI;
        _trace( "2  " + MxToString( basicCandles ) );
    }

image

It is NORMAL and CORRECT behavior.

If you assign the matrix to a NEW VARIABLE you are creating COPY of matrix1. And when you change the COPY of matrix, the COPY is modified, not the original. It works the same with ANY variable.

x = 1;
y = x; 
y = 1; // you are changing Y not X

If you are calling function, arguments are passed BY VALUE, not reference and any changes to arguments passed made inside the function, don't affect "outside" of function:

function myFun( x )
{
 x = 7;
}

z = 9;
myFun( z ); // calling function will NOT change z variable

Passing by reference is possible with recent versions (6.38 recommended).
If you want to pass by reference, the reference must be passed explicit (& operator - consult the READ ME / RELEASE NOTES).

1 - actually copy is not made at the time of assignment, but later (and not always). For speed, AmiBroker re-uses same pointer increases reference count and only makes a copy of matrix (and arrays) when you WRITE to matrix that is referenced by multiple variable identifiers. So as long as symbol is only read, there is nearly zero performance cost on any variable assignment.

5 Likes

Thanks Tomasz,
that clarifies it for me.

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