Can Arrays be passed to functions?

For Example,

int iValue[] = {0,0,0,0}
int return = 0;

iValue[1] = 1000;
return = MyFunction(iValue[1])

int MyFunction(int Something)
{
return 10*Something;
}

Is that allowed?

The problem is that the first time I set iValue[1] to 1000 it works fine, but later if I change iValue[1] to 2000 the function still uses 1000.

I have used Serial.print() and the new value is set for iValue[1], but useing Serial.print() inside the function, is shows Something as 1000. Why is it not useing the new value?

I think debugging this will be simpler if you upload the entire code.

As far as I can tell now, there is no reason why your function uses 1000 every time.

As for your question. 'Yes'

If you're going to show us an example that doesn't work as expected, it would help if it were an actual cut/paste copy of something that is able to compile in the "verify" step. Yours is invalid syntax on a number of fronts. Wrapping your code in tags will also format things in such a way as to be clear, and help us try your code.

That said, you can't pass whole arrays by value, but you can pass a pointer to the original array. This allows your function to modify elements of the array, so when your caller resumes, it has the new values.

#define countof(a)  (sizeof(a) / sizeof(*(a)))

int iValue[] = { 0, 0, 0, 0 };

void update(int* array)
{
    array[0] = 12;
    array[1] = 56;
    array[2] += 2;
}

void dump(int* array, int count)
{
    for (int i = 0; i < count; i++)
    {
        if (i > 0) Serial.print(", ");
        Serial.print(array[i]);
    }
    Serial.println();
}

void setup()
{
    Serial.begin(9600);
    dump(iValue, countof(iValue));
    update(iValue);
    dump(iValue, countof(iValue));
}

void loop() { ; }

Note that in my dump() routine, I also pass in the number of elements in the array being passed. A pointer does not know what it's pointing to, it just is an arbitrary address in memory. My update() routine does not know how big the array is, either. It just assumes there's at least three elements, [0], [1] and [2].

I wonder if the problem is the use of the keyword return as a variable name. the following code works as exptected

int iValue[] = {  0,0,0,0};

void setup(){
  Serial.begin(9600);      
}

void loop()
{
  int ret;
  for(int i = 1000; i < 4000; i+= 1000) {
    iValue[1] = i;
    ret = MyFunction(iValue[1]);
    Serial.println(ret);
    delay(1000);
  }
}


int MyFunction(int Something)
{
  Serial.print(Something);
  Serial.print( " ");
  Serial.print(Something * 10);
  Serial.print( " ");
  return 10*Something;
}

Thanks guys, ok - bad example.

Here is my code, but got to warn you it isn't pritty ::slight_smile:

#include <stdlib.h>

char buffer1[] = "0";
char buffer2[] = "000";

int Sensor_In[] = {0,1,2,3}; // Sensor input pins
int PWM_Out[] = {9,10,11}; // PWM Pins
int i = 0; // Used for loops
int iAPin[] = {0,0,0,0};
int iPercent = 0;
int iPWM = 0;
int iReceived = 0;
int iValue=0;
int iSpan[]={0,0,0,0};
int iTemp=0;
int c=0;
int iTank=0;

float fPWM = 0.0;
float fPercent = 0.0;
  
#define OFFSET 118
#define CPI 15
#define SAMPLE_RATE 6
#define SAMPLE_TIME 10
#define REPORT_TIME 2000
// #define HIGH_SATURATION 949
// #define LOW_SATURATION 49

void setup()
{
  for(i=0; i<4; i++)
    {
    pinMode(Sensor_In[i],INPUT);
    }  
  for(i=9; i<12; i++)
    {
    pinMode(PWM_Out[i],OUTPUT);
    }  
  Serial.begin(9600);
}

void loop()
{
    // Listen for Incoming commands
    if(Serial.available() > 0)
      {
      Serial.println("Incomeing Data ");
      c = Serial.read();
      if(c >= '0' && c <= '3') // Tank Number
        {
        buffer1[1] = (char)c;
        iTank = atoi(buffer1);  
        }
      while(iReceived < 3)
        {  
        c = Serial.read();
        if(c >= '0' and c <= '9')
          {
          buffer2[iReceived++] = (char)c;
          }
        else
          iReceived++;
        }
      iValue = atoi(buffer2);
      Serial.print("Tank Size ");
      Serial.println(iValue);
      iReceived = 0;
      if(iValue > 59 && iValue < 630) // Tank size, Calculate iSpan
        {
        iSpan[iTank] = (iValue*CPI)/10;//-OFFSET;
        Serial.print("Span ");
        Serial.println(iSpan[iTank]); 
        }
      }
            
    // Data Start
    iTemp=0;
    for(i=0; i<4; i++)
      {
      // Print Raw Counts
      iAPin[i] = Sample(i);
      Serial.print(iAPin[i]);
      Serial.print(",");
      // Convert to Inches
      iTemp = CtoI(iAPin[i]);
      Serial.print(iTemp);
      Serial.print(",");
      // Convert to Percent
      if(iSpan[i] > 0)
        {
        iTemp = CtoP(iAPin[i],iSpan[i]);
        Serial.print(iTemp);
        }
      else
        {
        Serial.print(0);
        }
      if(i != 3)
        Serial.print(",");
      }
   // Data End
   Serial.println(";");
   delay(REPORT_TIME);
}

// Sample
int Sample(int PinNum)
{
    int s=0;
    int iLoop = 0; 
    for(iLoop=0; iLoop<SAMPLE_RATE; iLoop++)
      {
      s = s+analogRead(Sensor_In[PinNum]);
      delay(SAMPLE_TIME);
      }
  if(s > 0)
    s=s/SAMPLE_RATE;
    
  else
    s=0;
    
  return s;
} 

// Convert to Inches
int CtoI(int Value)
{
  float fInches = 0.0;
  if(Value <= OFFSET)
    return 0;
  fInches = ((float)Value-(float)OFFSET)/(float)CPI;
  fInches = fInches*10.0;
  return (int)fInches;
}
  
// Convert to Percent 00.0 presision expressed as 000
int CtoP(int Value,int Span)
{
  Serial.print("Span = ");
  Serial.println(Span);
  float fPercent = 0.0;
  fPercent = (((float)Value-(float)OFFSET)/Span)*1000.0;
  //if(fPercent < 0.0)
  //  fPercent = 0.0;
  //if(fPercent > 1000.0)
  //  fPercent = 1000.0;
    
  return (int)fPercent;  
}

For now I am just getting it to test my ideas, then I will go in and clean it up.

Everything works except the CtoP() function, (and if you are wondering why I am multiplying by 1000 instead of 100 is so the display panel will show "0.0%" to "100.0%") .

The line that calls the function iTemp = CtoP(iAPin,iSpan); half works, the function will use the new value of iAPin but not the new value of iSpan*, after it is first set.*
The data sent to is 4 digits, the first being the tank # the next 3 being the Tank size in inches, so If I set tank 0 to be 60.0 inches, I send "0600". iSpan is set correctly and I get the correct % for thank 0, Span = 900. but if I change the inches of the tank to say 30.0" I send "0300" but this time the iSpan[0] is now set to 450, but when the CtoP() is called, Span is still set at 900.
Hope that made sense.

I think your code is mixing array values and indices :

for(i=9; i<12; i++){
pinMode(PWM_Out*,OUTPUT);*

  • } [/font]*
    Should be
  • for(i=0; i<3; i++){*
    pinMode(PWM_Out*,OUTPUT);
    _
    }_
    _
    [/font]*_

I think your code is mixing array values and indices :

for(i=9; i<12; i++){
pinMode(PWM_Out*,OUTPUT);*

  • }*
    Should be
    for(i=0; i<3; i++){
    pinMode(PWM_Out*,OUTPUT);
    _
    }_
    _
    [/quote]*_
    I was under the impression that when setting the pinmodes, I had to use the actual pin number. I want to use pin 9,10,11 as PWM out pins.
    I'm not concerned with that part yet anyways, thats later, so I will comment those out for now.

Hi Jassper, yes you do need to use the pin number, but arrays don't work the way you have this expression coded.

int PWM_Out[] = {9,10,11};
for(i=9; i<12; i++){
pinMode(PWM_Out*,OUTPUT);*
}
the code above does not call pinMode with consecutive values from 9 to 11 – in fact PWM_Out will return meaningless values because the only valid values are at or below PWM_Out[2].
I am not sure if any of your other array expressions have a similar problem but you may want to double check each one.
Good luck

I am not sure if any of your other array expressions have a similar problem but you may want to double check each one.

Good luck

Humm, You say that like I don't have a clue. :-[ which I probably don't ;D It's been awhile for C, some of it is slowly comeing back.

Anyways, I will get this mess cleaned up a bit and structured better, then see what issues I have.

Thanks!

Humm, You say that like I don't have a clue.

Nope, my reply was intended to point out an area of misunderstanding that may lead to you finding the solution to your problem. I would never call anyone clueless and have the utmost respect for everyone with the courage to post code here. :wink:

Nope, my reply was intended to point out an area of misunderstanding that may lead to you finding the solution to your problem. I would never call anyone clueless and have the utmost respect for everyone with the courage to post code here.

I know, just being light harted. :sunglasses: no foul.

Well, it seems to have been this line,
buffer1[1] = (char)c;
Should have been,
buffer1[0] = (char)c;

At least that is all I changed and it now works. ::slight_smile:

So your were right mem, it was in my Array handling.
Thanks for the direction!