Test Arrays as Parameters

In this section, we'll demonstrate the use of arrays as parameters by running the "arrtest2" function, which showcases how arrays are passed and modified within functions.

Example:

package ArrayParTest "Test Array as Parameter";

function arrtest1(array string bb)
    int k = sizeof(bb);
    print k + "\n";
    bb[] = "Test second last row";
    bb[] = "Test last row";

    return bb;
end

function arrtest2()
    array string xx = {"Test1", "test2", "test3"};

    array string b2 = arrtest1(xx);
    b2[] = "b2 altered"; // Demonstrating that xx and b2 are different arrays.

    int i;
    int x = sizeof(b2);
    for (i = 1, x)
        print "\n" + b2[i];
    end for

    print "\n" + "-" * 80;

    // Arrays as parameters are transferred by reference, so altering it in the "arrtest1" function
    // should be effective in the source as well.
    x = sizeof(xx);
    for (i = 1, x)
        print "\n" + xx[i];
    end for

    return 1;
end

Console Output After Running arrtest2():

3

Test1
test2
test3
Test second last row
Test last row
b2 altered
--------------------------------------------------------------------------------
Test1
test2
test3
Test second last row
Test last row

In this example, we define two functions: arrtest1 and arrtest2. arrtest2 demonstrates how arrays are passed as parameters and how changes made to the array within arrtest1 affect the source array in arrtest2. The console output showcases the behavior of these arrays and their modifications.