Weird behavior of variables when replacing for loops with if statement
You define i,y,i as global variables so there is no need to hand them over to different functions. They can be accessed and changed in all parts of your program.

define instead of 

int y; 


change to

long y;


and change 

 void inOut(int &amp;i){ 


to

void inOut(){ 


and remove

   return i;// Its a global var


change the line

unsigned long y = ((sensorValue*i)/600);


to

y = ((sensorValue*i)/600);


Try to avoid the same names for global and local vars. It might happen that the compiler handles it as a redefinition, which might lead to all sort of trouble in complex programs. (Also for you if you look onto an ill documented code some months later)And get rid of the delay it stops processing (also your subroutines) see Blinkwithoutdelay how to do it properly ArduinoIDE->file->examples->2 Digital->Blinkwithoutdelay