HELP!! Problem with external functions and the darn void loop()

Hey, so i'm messing around with some IR sensors for a school project, and I'm trying to get the average values of the past 50 analog sensor readings from 3 different IR sensors in a function outside of the loop(). In my code I've called these variables avgLeft, avgMid, avgRight.

I've identified the average value variables as global variables. So in theory I should be able to do a Serial.print() outside of the function that I have written the code in (ie the loop()). Unfortunately when I run the code, it does not work this way. It seems that the only thing that I can see in the serial monitor is the serial print i've placed inside the for loop of the function called "findAvgValues()"

So the Serial.print("in function loop") does not show up in the serial monitor,
neither does the Serial.println("before for loop").

Any ideas as to why this is not working?? Shouldnt "in function loop" show up after the program has run through the "findAvgValues" function?? And shouldnt the string "before for loop" show up before the for loop starts to execute??

Here is the code...

int leftIR = A8;
int midIR = A10;
int  rightIR = A15;
int SIZE = 50;

float avgLeft = 0;
float avgMid = 0;
float avgRight = 0;


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

void loop()
{
  
  findAvgValues();
  Serial.print("in function loop");
}

void findAvgValues()
{
    Serial.println("before for loop"); 
    for(int i = 0; i < SIZE; i++)
  {
    /* Create avg of values, this will only pass 1 number to rest of program
     over longer period of times */
    float leftOut = analogRead(leftIR);
    //Serial.println(leftOut);
    float midOut = analogRead(midIR);
    //Serial.println(midOut);
    float rightOut = analogRead(rightIR);
    //Serial.println(rightOut);
    Serial.print(i);
    Serial.println("     in for loop");
    
    float leftTotal = 0;
    float midTotal = 0;
    float rightTotal = 0;
    leftTotal += leftOut;
    midTotal += midOut;
    rightTotal += rightOut;
  
 
      if (i == (SIZE-1))
      {
        Serial.println("in if statement");
        
        avgLeft = leftTotal / SIZE;
        avgMid = midTotal / SIZE;
        avgRight = rightTotal / SIZE;
    
        i = 0;
        leftTotal = 0;
        midTotal = 0;
        rightTotal = 0;
      }
   }
}

I don't get what you are doing here:

    float leftTotal = 0;
    float midTotal = 0;
    float rightTotal = 0;
    leftTotal += leftOut;
    midTotal += midOut;
    rightTotal += rightOut;

Why zero them every time?

And as for the "if" - why not just move the whole thing outside the for loop?

Ok, your right about the variables. I had them as global variables at one point also but to try and fix the issue at hand I brought them back into the avgValue function scope to see if that would do anything. I just forgot to bring them out.

And i guess i'm not sure why I have that if statement in the for loop. I"ll take it out.

Thanks for the help!

Any idea on the main issue of not being able to see the Serial.print strings in any other part of the program but inside the for loop??

OK.

void findAvgValues()
{
  Serial.println("before for loop"); 
  for(int i = 0; i < SIZE; i++)
  {
 ...

    if (i == (SIZE-1))
    {
...
      i = 0;
...
    }
  }
}

Explain when you think the function findAvgValues will exit?

Well, after the for loop runs its final condition (SIZE - 1), the program should return back to loop function.

Or do I need to add a return; in the last statement?? I always thought in void functions I did not need the return.

OHHHHH, nevermind i see it now. my darn if statement takes i back to 0 keeping the for loop going forever.

Thank you very much!!