user35098: I am trying to write a function that takes integer values as an input and gives an array as an output. I have tried the following code but it doesn't show the output result in the serial monitor:
int *phi (int cur,int vol,int t) {
  int z[4];
  z[0]=1;
  z[1]=cur;
  z[2]=vol;
  z[3]=t;
//z=cur+vol+t;
  return z;
}


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

}

void loop() {

int result;
result=phi(1,2,5);  
Serial.begin(9600);
Serial.print(result);

}


user20245: It appears you have created an array which only has scope with in the method "phi".  
In practical terms, the array "z" is a local variable declared inside of the method and, as such, will likely end up in temporary RAM.  When the method has completed, the RAM reserved for that method's local variables is released and is available for other purposes.
To fix this you could make "z" a static or you could give "z" global scope.

user313: Defining the array in phi() returns a pointer to unassigned memory - it is no longer reserved once phi() returns. The next function call or interrupt (which you have no control over) will likely clobber the data.
A better choice would be to define z in the caller (loop() in this case) as an uninitialized array and pass it to phi() to fill and return. This is the more usual case, as it:

avoids using globals, 
permits multiple calls to the function without clobbering previously returned data (you would pass different arrays in this case), and
it avoids statically defined arrays that may not be needed after the caller returns, in a memory-starved environment (this may not be applicable if loop() is the caller).

In short, it gives the calling function the greatest flexibility, while committing the least amount of memory.

35098: <p>I am trying to write a function that takes integer values as an input and gives an array as an output. I have tried the following code but it doesn't show the output result in the serial monitor:</p>

<pre><code>int *phi (int cur,int vol,int t) {
  int z[4];
  z[0]=1;
  z[1]=cur;
  z[2]=vol;
  z[3]=t;
//z=cur+vol+t;
  return z;
}


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

}

void loop() {

int result;
result=phi(1,2,5);  
Serial.begin(9600);
Serial.print(result);

}
</code></pre>
 20245: <p>It appears you have created an array which only has scope with in the method "phi".  </p>

<p>In practical terms, the array "z" is a local variable declared inside of the method and, as such, will likely end up in temporary RAM.  When the method has completed, the RAM reserved for that method's local variables is released and is available for other purposes.</p>

<p>To fix this you could make "z" a static or you could give "z" global scope.</p>
 313: <p>Defining the array in phi() returns a pointer to unassigned memory - it is no longer reserved once phi() returns. The next function call or interrupt (which you have no control over) will likely clobber the data.</p>

<p>A better choice would be to define <code>z</code> in the caller (loop() in this case) as an uninitialized array and pass it to phi() to fill and return. This is the more usual case, as it:</p>

<ul>
<li>avoids using globals, </li>
<li>permits multiple calls to the function without clobbering previously returned data (you would pass different arrays in this case), and</li>
<li>it avoids statically defined arrays that may not be needed after the caller returns, in a memory-starved environment (this may not be applicable if loop() is the caller).</li>
</ul>

<p>In short, it gives the calling function the greatest flexibility, while committing the least amount of memory.</p>
