functions not seem to be working

hi i have used the followin code for my line following robot built up using IR sensors:

int ir1  =  9;
int ir2  =  11;
int ir3  =  8;
int ir4=7;
int ir5=6;
int ir6=5;
int obstacle1=10;
int obstacle2=4;
int fwleft= 13;
int revleft=12;
int fwright= 14;
int revright= 15;
int val1=0;
int val2=0;
int val3=0;
int val4=0;
int val5=0;
int val6=0;
int pwm=0;
int flag=0;
void setup()
{
  pinMode(ir1, INPUT);
  pinMode(ir2, INPUT);
  pinMode(ir3, INPUT);
  pinMode(ir4, INPUT);
  pinMode(ir5, INPUT);
  pinMode(ir6, INPUT);
  pinMode(fwright, OUTPUT);
  pinMode(revright, OUTPUT);
  pinMode(fwleft, OUTPUT);
  pinMode(revleft, OUTPUT);
}
void loop()
{
  int obs1,obs2;
  obs1=analogRead(obstacle1);
  obs2=analogRead(obstacle2);
  val1=digitalRead(ir1);
  val2=digitalRead(ir2);
  val3=digitalRead(ir3);
  val4=digitalRead(ir4);
  val5=digitalRead(ir5);
  val6=digitalRead(ir6);
  if(val1==LOW && val2==LOW && val3==LOW && val4==LOW && val5==LOW && val6==LOW)
  {
   
    for(pwm = 0 ; pwm <= 60; pwm++) // fade in (from min to max) 
  {
    digitalWrite(revleft, pwm);
    digitalWrite(revright, LOW);
    analogWrite(fwright, pwm);
    analogWrite(fwleft, LOW);
  }
  flag=1;
  }
      
 if(val1==HIGH && val2==HIGH && val3==LOW && val4==LOW && val5==HIGH && val6==HIGH)
 {
  for(pwm = 0 ; pwm <= 80; pwm++) // fade in (from min to max) 
  { 
    digitalWrite(revright, LOW); 
    analogWrite(fwleft, pwm);
    digitalWrite(revleft, LOW);
    analogWrite(fwright, pwm);
  }
  flag=2;
 }
if(val1==HIGH && val2==LOW && val3==LOW && val4==HIGH && val5==HIGH && val6==HIGH)
 {
    for(pwm = 0 ; pwm <= 50; pwm++) // fade in (from min to max)
      { 
        digitalWrite(fwright, LOW); 
        analogWrite(fwleft, pwm);
        analogWrite(revright, pwm);
        digitalWrite(revleft, LOW);
      }
      flag=3;
 }
 
  if(val1==LOW && val2==LOW && val3==LOW && val4==HIGH && val5==HIGH && val6==HIGH)
 {
  for(pwm = 0 ; pwm <= 50; pwm++) // fade in (from min to max) 
  { 
    digitalWrite(revleft, pwm);
    digitalWrite(revright, LOW);
    analogWrite(fwright, pwm);
    analogWrite(fwleft, LOW);
  }
  flag=2;
 }



 if(val1==LOW && val2==HIGH && val3==HIGH && val4==HIGH && val5==HIGH && val6==HIGH)
 {
  for(pwm = 0 ; pwm <= 50; pwm++) // fade in (from min to max) 
  { 
    digitalWrite(revleft, pwm);
    digitalWrite(revright, LOW);
    analogWrite(fwright, pwm);
    analogWrite(fwleft, LOW);
  }
  flag=2;
 }






if(val1==LOW && val2==LOW && val3==HIGH && val4==HIGH && val5==HIGH && val6==HIGH)
 {
  for(pwm = 0 ; pwm <= 60; pwm++) // fade in (from min to max) 
  {
    digitalWrite(revleft, pwm);
    digitalWrite(revright, LOW);
    analogWrite(fwright, pwm);
    analogWrite(fwleft, LOW);
  }
  flag=4;
 }
if(val1==HIGH && val2==HIGH && val3==HIGH && val4==LOW && val5==LOW && val6==HIGH)
 {
    for(pwm = 0 ; pwm <= 50; pwm++) // fade in (from min to max)
  { 
    digitalWrite(fwright, LOW); 
    analogWrite(fwleft, pwm);
    analogWrite(revright, pwm);
    digitalWrite(revleft, LOW);
  }
  flag=5;
 }
 
 if(val1==HIGH && val2==HIGH && val3==HIGH && val4==LOW && val5==LOW && val6==LOW)
 {
    for(pwm = 0 ; pwm <= 50; pwm++) // fade in (from min to max)
  { 
    digitalWrite(fwright, LOW); 
    analogWrite(fwleft, pwm);
    analogWrite(revright, pwm);
    digitalWrite(revleft, LOW);
  }
  flag=5;
 }



if(val1==HIGH && val2==HIGH && val3==HIGH && val4==HIGH && val5==LOW && val6==LOW)
{
  for(pwm = 0 ; pwm <= 60; pwm++) // fade in (from min to max)
  { 
    digitalWrite(fwright, LOW); 
    analogWrite(fwleft, pwm);
    analogWrite(revright, pwm);
    digitalWrite(revleft, LOW);
  }
  flag=6;
}

if(val1==HIGH && val2==HIGH && val3==HIGH && val4==HIGH && val5==HIGH && val6==LOW)
{
  for(pwm = 0 ; pwm <= 50; pwm++) // fade in (from min to max)
  { 
    digitalWrite(fwright, LOW); 
    analogWrite(fwleft, pwm);
    analogWrite(revright, pwm);
    digitalWrite(revleft, LOW);
  }
  flag=6;
}


if(val1==HIGH && val2==HIGH && val3==HIGH && val4==HIGH && val5==HIGH && val6==HIGH)
{
  if(obs1==0 && obs2==0)
  {
    for(pwm = 0 ; pwm <= 80; pwm++) // fade in (from min to max) 
  { 
    digitalWrite(revright, LOW); 
    analogWrite(fwleft, pwm);
    digitalWrite(revleft, LOW);
    analogWrite(fwright, pwm);
  }
  }
  if(obs1==0 && obs2!=0)
  {
    for(pwm = 0 ; pwm <= 35; pwm++) // fade in (from min to max)
  { 
    digitalWrite(fwright, LOW); 
    analogWrite(fwleft, pwm);
    analogWrite(revright, pwm);
    digitalWrite(revleft, LOW);
  }
  }
  if(obs1!=0 && obs2==0)
  {
    for(pwm = 0 ; pwm <= 35; pwm++) // fade in (from min to max)
      { 
        digitalWrite(fwright, LOW); 
        analogWrite(fwleft, pwm);
        analogWrite(revright, pwm);
        digitalWrite(revleft, LOW);
      }
  }
}
}

/*

 /*void reverse()
{
  
   for(pwm = 0 ; pwm <= 200; pwm+=5) // fade in (from min to max) 
  { 
    digitalWrite(fwright, LOW);
    digitalWrite(revleft, LOW);
    analogWrite(fwleft, pwm);
    analogWrite(revright, pwm);
  }
}*/

the problem is my line follower is working fine if i don use functions..if i use functions such as front,reverse,etc...its not working when it follows line and goes out of control??is the delay encountered in calling a function and returnin is responsible for this??if so, how to rectify it?[code][/code]

Well, for one, your loop count "pwm" is a single global.
That could give you grief if you leave it like that when you have you functions.

Could you please click on the "modify" button by your post, select all the sketch code, then click on the "Code" (#) button in the editor toolbar.
My scroll finger hurts.

My scroll finger hurts.

It hurts so badly that I can feel the pain! :o ;D

is the delay encountered in calling a function and returnin is responsible for this??

It doesn't really seem likely, does it?

Perhaps if you'd posted the code that didn't work, we'd be able to help (post using the # button, please), though it looks like some loops and arrays would help a lot here.
Reduction of scope of some of the variables would seem to make sense, as Groove has pointed out.

are u guyz telling me tat instead of using a global variable i shud use a local variable in every function?

No, we're telling you you should use global variables where global variables are appropriate, and local variables where local variables are appropriate.

Try to use global variables sparingly. And locals as much as you can. Learn about passing values to subroutines (functions), and getting them back from them, and your "need" for globals will diminish.

To keep myself sane, I usually have some global variable called tmp1, tmp2, etc. I use them for things that "should" be done with locals, but it can get silly. If you KNOW that whatever you're saving in a variable JUST FOR A MOMENT will NOT be needed again, past the next, say, 5 lines of code, then go ahead and put it in one of your global "tmp" variables. But you'll be surprised how many times a bug traces back to using something from a global "tmp" which got changed when you didn't think it had had a chance. If you call those variables "tmp", you will at least be a little sensitive to the possibility that you're putting too much faith in what's in one of them at a critical point.