stevehopwoodforex.com
https://www.stevehopwoodforex.com/phpBB3/
Print view

Not all control paths return a value - advice please
https://www.stevehopwoodforex.com/phpBB3/viewtopic.php?t=5152
Page 1 of 1
Author:  Nufty [ Wed Apr 26, 2017 1:18 pm ]
Post subject:  Not all control paths return a value - advice please

Hi

Its been a few years since I looked at coding, and I thought I would start fresh. It began well too, fixed most errors in a simple EA (not one to trade.. just learning) but I am stuck on this error

Code: Select all

//My logic to determine if we can trade
//+------------------------------------------------------------------+
int logic()
{
      // Long signal 
      if ((Open[3] < Close[3]) && (Open[2] < Close[2]) && (Open[1] < Close[1]) && (Open[0] > Close[1]))
      {

      return(1);

      }

 else
   
      // Short signal 
      if ((Open[3] > Close[3]) && (Open[2] > Close[2]) && (Open[1] > Close[1]) && (Open[0] < Close[1]))

         {
         return(0);

         }
         else Print("ERROR");

}
For the life of me , I cant see my failings.

Any advice is appreciated

Thanks
Nufty
Author:  mejayusa [ Wed Apr 26, 2017 2:43 pm ]
Post subject:  Not all control paths return a value - advice please

The compiler is complaining because in the primary else block the code will return a value only if a specific condition is met. Compiler doesn't know how the code will behave at runtime and is trying to tell you that your method might not return a value if it goes into the second else statement.
Author:  SteveHopwood [ Wed Apr 26, 2017 5:35 pm ]
Post subject:  Not all control paths return a value - advice please

Here is an example of what mejayusa is saying, in a function I coded earlier. Never mind what its' function is:

Code: Select all

int GetTypeIndex(int ticket)
{
   //Returns the levelsIndex of the trade, calculated from its OrderComment()
   
   //Just in case
   if (!OrderSelect(ticket, SELECT_BY_TICKET, MODE_HISTORY))
      return(-1);

   string comment = OrderComment();
   
   for (int cc = 0; cc <= ArraySize(tradeComments) - 1; cc++)
   {
      if (StringFind(OrderComment(), tradeComments[cc], 0) > -1)
      return(cc);
   }//for (int cc = 0; cc <= ArraySize(tradeComments) - 1; cc++)
   
   

   //Something is wrong
   return(-1);
   
}//int GetTypeIndex(int ticket)
The compiler will whinge if I remove the return(-1); at the end of the function, because the earlier loop might not find what it is looking for and so not return anything.

Always include a final default return whenever you code a function that is not a void function.

:xm:
Author:  Nufty [ Wed Apr 26, 2017 10:42 pm ]
Post subject:  Not all control paths return a value - advice please

Thank you mejayusa and Steve for your replies

Having another go now :)

Regards

Nufty
Author:  Nufty [ Wed Apr 26, 2017 11:29 pm ]
Post subject:  Not all control paths return a value - advice please

It compiled

I think thats my first ever :)

Thanks again

Now... to code something I actually think might work :lol: :lol:
Author:  SteveHopwood [ Thu Apr 27, 2017 8:20 am ]
Post subject:  Not all control paths return a value - advice please

:clap: :clap: :clap: :clap: :clap: :clap: :clap: :clap: :clap: :clap: :clap: :clap: :clap:
All times are UTC Page 1 of 1