how to write a function for array

 
int list =1;
int Func1 (double list) {
int b = 2*list;
return b;
}

but when there is an array of list

int list[4] ={1,2,3,4};

how can i modify the function so it works for array

Thanks

 
Arpit T: but when there is an array of list how can i modify the function so it works for array
int function( int& array[] ) { ... };
 
Fernando Carreiro #:

thanks

so it should be like

int Func1 (double &list[]) {
int b = 2*list[];
return b;
}
 
Arpit T #: so it should be like

Don't post code that will not even compile. (2*list[] is bable.)

 
William Roeder #:

Don't post code that will not even compile.

This wont compile as i am learning and asking on forum how to make it writeable (correction) so it can be compiled,

 
Arpit T #: thanks so it should be like

No! If you are passing an array as an argument, treat it as such. It is an array of many elements. You can't just multiply it by "2" and just get a single value.

You can only return a single value from a function but you can alter the contents of an array passed to the function.

If you want to return an array value, then do something like this:

void someotherfunction(void)
{
   int list[] = { 1, 2, 3, 4 }, results[];
      
   function( list, results );
};

bool function( int& alist[], int& blist[] )
{
   int size = ArraySize( alist );
   if( size > 0 )
   {
      if( ArrayResize( blist, size ) >= size )
      {
         for( int i = 0; i < size; i++ )
            blist[i] = alist[i] * 2;
            
         return true;
      };
   };
   
   return false;
};

This way, you can read the values from "alist" and assign different values to "blist". The above code is untested and uncompiled, only to serve as an example.

EDIT: Please note that I edited this post multiple times.

 
Fernando Carreiro #:

No! If you are passing an array as an argument, treat it as such. It is an array of many elements. You can't just multiply it by "2" and just get a single value.

You can only return a single value from a function but you can alter the contents of an array passed to the function.

If you want to return an array value, then do something like this:

This way, you can read the values from "alist" and assign different values to "blist". The above code is untested and uncompiled, only to serve as an example.

EDIT: Please note that I edited this post multiple times.

wonderful worked!! thanks