Not all control paths return a value - advice please

Post Reply
User avatar
Nufty
Trader
Posts: 39
Joined: Fri Feb 15, 2013 9:21 pm

Not all control paths return a value - advice please

Post by Nufty »

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
mejayusa
Trader
Posts: 21
Joined: Thu Oct 30, 2014 10:35 pm

Not all control paths return a value - advice please

Post by mejayusa »

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.
User avatar
SteveHopwood
Owner
Posts: 9904
Joined: Tue Nov 15, 2011 8:43 am
Location: Misterton - an insignificant village in England. Very pleasant to live in.

Not all control paths return a value - advice please

Post by SteveHopwood »

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:
Read the effing manual, ok?

Afterprime is the official SHF broker. Read about them at https://www.stevehopwoodforex.com/phpBB3/viewtopic.php?p=175790#p175790.

I still suffer from OCCD. Good thing, really.

Anyone here feeling generous? My paypal account is always in the market for a tiny donation. pianodoodler@hotmail.com is the account.

To see The Weekly Roundup of stuff you guys might have missed Click here

My special thanks to Thomas (tomele) for all the incredible work he does here.
User avatar
Nufty
Trader
Posts: 39
Joined: Fri Feb 15, 2013 9:21 pm

Not all control paths return a value - advice please

Post by Nufty »

Thank you mejayusa and Steve for your replies

Having another go now :)

Regards

Nufty
User avatar
Nufty
Trader
Posts: 39
Joined: Fri Feb 15, 2013 9:21 pm

Not all control paths return a value - advice please

Post by Nufty »

It compiled

I think thats my first ever :)

Thanks again

Now... to code something I actually think might work :lol: :lol:
User avatar
SteveHopwood
Owner
Posts: 9904
Joined: Tue Nov 15, 2011 8:43 am
Location: Misterton - an insignificant village in England. Very pleasant to live in.

Not all control paths return a value - advice please

Post by SteveHopwood »

:clap: :clap: :clap: :clap: :clap: :clap: :clap: :clap: :clap: :clap: :clap: :clap: :clap:
Read the effing manual, ok?

Afterprime is the official SHF broker. Read about them at https://www.stevehopwoodforex.com/phpBB3/viewtopic.php?p=175790#p175790.

I still suffer from OCCD. Good thing, really.

Anyone here feeling generous? My paypal account is always in the market for a tiny donation. pianodoodler@hotmail.com is the account.

To see The Weekly Roundup of stuff you guys might have missed Click here

My special thanks to Thomas (tomele) for all the incredible work he does here.
Post Reply

Return to “Coders Hangout”