Arduino Functions could return An Array Variable?

I have some troubles to compilate the follow code, why?
If you have programmed in JAVA anytime, you know that this kind of code is possible to operate in JAVA, why is not possible in Arduino IDE?

So please Pay attenction:

void setup() {
// put your setup code here, to run once:

}

void loop() {
// put your main code here, to run repeatedly:
int B[5]={0,0,0,0,0};
int C=0;
C=funcionX(); //This ocde line is ok
B=funcionY(); // but this code line is not ok.
}

int funcionX() //this function is ok
{
return 3;
}

int[5] funcionY() // But this function is not ok
{
int A[5]={1,22,333,4444,55555};
return A;
}

How can I make functions with Array return in Arduino?

This is how I do it:

void setup() {
  Serial.begin(9600);
  int B[5]; //create an array
  funcionY(B); //pass the array to the function, which will modify it
  Serial.println(B[0]);
}

void loop() {
}

void funcionY(int anArray[]) {
  anArray[0] = 42;
}

pert:
This is how I do it:

void setup() {

Serial.begin(9600);
  int B[5]; //create an array
  funcionY(B); //pass the array to the function, which will modify it
  Serial.println(B[0]);
}

void loop() {
}

void funcionY(int anArray[]) {
  anArray[0] = 42;
}

In what way is your uncommented example connected to returning arrays from functions?

Whandall:
In what way is your uncommented example connected to returning arrays from functions?

In that it's the most "Arduino" way to achieve the underlying goal. It's the model established by Arduino core functions such as the Stream class's readBytes() and readBytesUntil(), the Client class's read(), etc.

Reading data into a supplied buffer is a different thing IMHO.

IngenieroDFZ:
I have some troubles to compilate the follow code, why?
If you have programmed in JAVA anytime, you know that this kind of code is possible to operate in JAVA, why is not possible in Arduino IDE?

I'm not a java programmer (barely an object oriented programmer) but reasonably confident that an array on itself in java is an object; simply because it has associated members like length. In C++ it is not an object.

So as indicated in the first reply, this is C++, not java.

OP, if you want to program an arduino, then you will have to learn C++. Google "C++ tutorial", and "differences between java and C++". Although the code looks similar and they share certain features, they are very different languages underneath.

The direct answer to your question is that C++ declarations are a little different, and C++ has pointers and pointer arithmetic, which Java does not. In C++, an identifier of array type is treated as a pointer to the zeroth element of that array. You can declare a function returning a pointer to int, but not an int array (I think).

Furthermore, declarations are written to look like the use of the variable. For instance, if you were to use a variable of type 'pointer to int' named p to get an int, then you would write *p. Consequently, the declaration for a function returning a pointer to int is

int *functionZ() {;

Beyond that, it starts to get complicated. Go do a C++ tutorial.

sterretje:
I'm not a java programmer (barely an object oriented programmer) but reasonably confident that an array on itself in java is an object; simply because it has associated members like length. In C++ it is not an object.

In java, everything that isn't a primitive type is an object. Furthermore, all object identifiers are heap references - you don't have that thing you can do in C where one struct is 'inside' another struct in the sense of it being contiguous memory. Objects aren't allocated on stack, they aren't static (except for a few system-level 'native' objects). There are no pointers in the C sense, and no pointer arithmetic - you don't directly deal with memory at all the way you do in C, where memory is essentially a big array of byte.

The payoff for (effectively) mallocing everything and not being able to touch memory directly with pointers is that Java takes care of free()ing objects - you have to work hard to get a memory leak in Java. The garbage collection algorithm is pretty good: it systematically collects anything that is no longer "reachable" by any live code, it's not just a simple reference count. It also has a runtime security model, which means that you can write an appserver that executes code that you did not write, and ensure that that code can't touch anything it's not supposed to.

It looks deceptively similar to C, but the runtime model, the memory model, and the coding techniques are actually quite different.

I think maybe what you're looking for is

int B[5]={0,0,0,0,0};
process_array(B);
for(byte j= 0; j < 5; j++) print(B[j]);
...

void process_array(int *local_array_name)
{
for(byte j = 0; j < 5; j++) (local_array_name[j] = j);
)

Prints 0,1,2,3,4

PaulMurrayCbr:
In java, everything that isn't a primitive type is an object. Furthermore, all object identifiers are heap references - you don't have that thing you can do in C where one struct is 'inside' another struct in the sense of it being contiguous memory. Objects aren't allocated on stack, they aren't static (except for a few system-level 'native' objects). There are no pointers in the C sense, and no pointer arithmetic - you don't directly deal with memory at all the way you do in C, where memory is essentially a big array of byte.

The payoff for (effectively) mallocing everything and not being able to touch memory directly with pointers is that Java takes care of free()ing objects - you have to work hard to get a memory leak in Java. The garbage collection algorithm is pretty good: it systematically collects anything that is no longer "reachable" by any live code, it's not just a simple reference count. It also has a runtime security model, which means that you can write an appserver that executes code that you did not write, and ensure that that code can't touch anything it's not supposed to.

It looks deceptively similar to C, but the runtime model, the memory model, and the coding techniques are actually quite different.

Thanks.

Is there any way to tell java to do garbage collection on request and it will do it immediately? I think you can tell it that you want it to do a garbage collection, but I don't think that it will do it immediately.

sterretje:
Is there any way to tell java to do garbage collection on request and it will do it immediately? I think you can tell it that you want it to do a garbage collection, but I don't think that it will do it immediately.

System.gc();

suggests that a garbage collection be performed but does not force it. The implementation of garbage collection is Java virtual machine dependent and the Sun/Oracle implementation has changed several times over the years. What is guaranteed is that a garbage collection will have been performed before any out of memory exception is thrown.

sterretje:
Is there any way to tell java to do garbage collection on request and it will do it immediately? I think you can tell it that you want it to do a garbage collection, but I don't think that it will do it immediately.

Basically, in Java that layer of operation of the JRE is none of your business. Of course, system-type code in appservers will do that, and a jre may have launch options and config files, but most of the time for most programming you don't worry about that find of thing and you are not really supposed to.

Having said that - you can manipulate memory and do some extremely interesting things with reference objects. There were three kinds last time I looked at it.

Each reference object holds a reference to some other object, and the reference to that other object may be dropped by the garbage collector it the GC's discretion. However, there is a contract that the GC is supposed t abide by. Each reference may be attached to a queue object which will be notified when the reference is cleared.

SoftReference - the GC is supposed to leave soft references uncleared for as long as it can, and to start clearing them when it begins to run out of memory. You use them to cache things that you can fetch again if you need to, eg: bitmaps.

WeakReference - the GC clears weak references aggressively. However, they may only be cleared when the referenced object is no longer reachable by any live thread (including by a soft reference). Furthermore, if more than one WeakReference can reach an object, then all of those references get cleared as an atomic operation together. This means that when a WeakReference is cleared, the object that it refers to is gone and cannot be retreived by any thread. You use this, for instance, for object/relational mappings where you want to be certain that if a given database record is loaded into memory, there will only be one object present at any time for that primary key.

PhantomReference - Phantom references always come back as having been cleared. However, the queue object will only be notified that the reference has been cleared once the GC has actually recovered the memory for whatever it was pointing at. This might be useful if you are interested in monitoring memory usage. Using a reference queue with phantom references is an alternative to polling System.freeMemory() at intervals.

So … it's possible to do quite sophisticated things. The method is that you subclass these classes to instrument them. But, it's not something that a programmer is usually interested in doing.

@DKWatson, see replay #2

Juraj:
@DKWatson, see replay #2

Aye. Neglected to scroll down the code. Apologies.