Go Down

Topic: function of type integer array (Read 1 time) previous topic - next topic


Jun 10, 2007, 01:59 pm Last Edit: Jun 10, 2007, 02:00 pm by haesslich Reason: 1
i want to randomize an array of 8 integer numbers.
an elegant way would be a function from the type of an array of integer numbers which returns a randomized array.

in this case i could call the randomize function easily:
ExistingArray = RandomFunction;

i just right now do not remember how to do this.
i tried a declaration in the style of
Code: [Select]
int[8] randomize(int k){
 for (int i=0; i<k; i++){
   randomize[i] = random(0,1);
 return randomize;

but this is not quite correct ;)


It's probably easier to pass the array as a parameter into the function - any values you assign to it within the function will carry over to the calling function.  If you want to return an array from a function, you start needing to deal with allocation and deallocation of memory.


You might find something of use in this approach too.



Jun 24, 2007, 11:41 am Last Edit: Jun 24, 2007, 12:14 pm by tonybarry Reason: 1
I would be very grateful if forum gurus could post a short piece of code that does send an array to a function, and then return a modified array back to the calling routine.  That is, similar to the code provided by haesslich (but which works correctly).

i.e. a piece of code like this but which has correct syntax:-

void(loop) {
int sourceValues[6] = {0,1,2,3,4,5};

int[6] FunctionWhichOperatesOnSourceValueArray(int[6]) {
byte counter = 0;
for (counter = 0; counter < 6; counter ++) {
return array somehow



Jun 25, 2007, 11:00 pm Last Edit: Jun 25, 2007, 11:01 pm by mellis Reason: 1
You don't actually need to return the array from the function.  Just modify the one that got passed in, and the changes will be reflected in the calling function.


Code: [Select]
int pins[] = { 2, 3, 4 };

void incrementArray(int arr[], int length)
 for (int i = 0; i < length; i++) {
   arr[i] = arr[i] + 1;

void setup()
 incrementArray(pins, 3);
 // now pins is { 3, 4, 5 }

void loop()
 // ...

Go Up

Please enter a valid email to subscribe

Confirm your email address

We need to confirm your email address.
To complete the subscription, please click the link in the email we just sent you.

Thank you for subscribing!

via Egeo 16
Torino, 10131