[chuck-users] passing by reference

eduard eduard.aylon at gmail.com
Sun Nov 27 14:14:00 EST 2005


Hello all,

I am trying to pass an integer by reference, but I can't get it  
right. Could any one help me on this?
What I tried works fine for arrays, but not for integers only. Code  
is below.

Thanks

Eduard

//////////////////////////////////////////////////////////////////////// 
///////////////////////////////
// main
//////////////////////////////////////////////////////////////////////// 
///////////////////////////////

int a[10];

for(0 => int i; i < 10 ; i++) 10 - i => a[i];
PrintArray( a );  // passing array by reference works fine or am I  
not referencing?
IsInArray( a, 8, 10); // ok
15 => int value;
<<<"value before modification is: ", value >>>;
ModifyValue( value, 35 );  // seems it's not correct
<<<"value after modification is: " , value >>>;
ModifyArray( a ); // ok
PrintArray( a ); // ok

//////////////////////////////////////////////////////////////////////// 
///////////////////////////////
// functions
//////////////////////////////////////////////////////////////////////// 
///////////////////////////////


fun void PrintArray( int @ array[] )
{
     for( 0 => int i ; i < 10; i++ )
         <<<"a[",i,"] = ", array[i]>>>;
}

fun int IsInArray(int @ array[], int num, int length )
{
    if( length > array.cap() ) array.cap() => length;
    if( !length ) return 0; // array is still empty
    for( 0 => int i; i < length; i++ )
    {
         if( num == array[i] )
         {
             <<<num, " found in pos ", i >>>;
             return 1; // num found in array
         }
    }
    <<< num, " not found in array" >>>;
    return 0; // not found
}

fun void ModifyValue( int @ val, int newValue )
{
     <<< "val is: " , val >>>;
     newValue => val;
     <<< "val is: " , val >>>;
}

fun void ModifyArray( int @ array[] )
{
     for(0=>int i; i < array.cap(); i++)
         array[i]*5 => array[i];
}

// end of code




More information about the chuck-users mailing list