Avoiding Macro Conflicts

 

Because SPL macros are global to a session, potential conflicts can arise between a macro definition and function variables.

 

#define a 10

 

myfun(x)

{

    a = sin(x) + x;

 

    return(a*a);

}

 

Since a is a macro, myfun expands as follows:

 

myfun(x)

{

    10 = sin(x) + x;

 

    return(10*10);

}

 

and an error results because a value cannot be assigned to a numerical constant. To avoid this situation, variables that are local to a function can be declared with the local keyword.

 

myfun(x)

{

    local a;

 

    a = sin(x) + x;

 

    return(a*a);

}

 

Now a is recognized as a local variable of myfun and macro substitution does not take place.

 

Assignments and arrays can be specified with the local statement.

 

myfun(x)

{

    local i, a = 10, c[x, x+1];

 

    loop (i = 1..x)

    {

        c[i, i] = a; 

        a++

    } 

 

    return(c)

}

 

myfun(3) returns the array

 

  {{10,  0,  0, 0},

   { 0, 11,  0, 0},

   { 0,  0, 12, 0}} 

 

 

Declared arrays are initialized to 0. Unlike C/C++, assignments and array declarations are not limited to constant expressions in the local statement.