Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

"not all control paths return a value"

Posted on 2001-08-09
3
Medium Priority
?
539 Views
Last Modified: 2006-11-17
Hi guys,

I'm fond of writing loops with a return statement in the body, usually when there need not be any other return statement...for example,

int someFunction()
{
for(int i=0; i<arraySize; i++)
    if(array[i] == TRUE)
         return i;
}

You usually get a compiler warning for this sort of thing. I typically argue that I'm well aware that not all paths return a value because, say, I know that there will be at least one array[i] that will be true (or something like that)

My question is, is this a bad coding practise? It is unabasahed laziness on my part, that's for sure, but how harmful is it?

Thanks

Senkwe
0
Comment
Question by:chandas
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
3 Comments
 

Expert Comment

by:vfinn
ID: 6367612
It's not really bad but it's probably best to give a default return at the bottom
This gets rid of the warning and is safer in case of unexpected behaviour (e.g. arraySize being a strange value, etc..)

The alternative is to have a variable iReturnValue which you set in the for loop and then always return that, but that means you have to use a break in the for loop or you lose speed which is probably worse
0
 
LVL 14

Accepted Solution

by:
AlexVirochovsky earned 150 total points
ID: 6367662
>>is this a bad coding practise?
No, I don't think so. I saw many codes and wrote many
times such code, BUT remember: you must free all
memory before return. Next code will be memory leek:

char *sz = new char[255];
...
for(int i=0; i<arraySize; i++)
   if(array[i] == TRUE)
        return i;
 delete [] sz;

return -1;
..
If you want get rid of worning, set in last line
return -1; as im my example.
0
 
LVL 9

Expert Comment

by:jasonclarke
ID: 6367668
You should strive to produce warning free code, the way to handle this situation is to use an assertion (of some sort):

int someFunction()
{
   for(int i=0; i<arraySize; i++)
      if(array[i] == TRUE)
          return i;

   assert(0); // Should Never Reach this point
   return 0;  // Dummy Return
}

this makes your assumption explicit, you are not leaving it to other programmers to decipher your assumption.

0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Unlike C#, C++ doesn't have native support for sealing classes (so they cannot be sub-classed). At the cost of a virtual base class pointer it is possible to implement a pseudo sealing mechanism The trick is to virtually inherit from a base class…
IntroductionThis article is the second in a three part article series on the Visual Studio 2008 Debugger.  It provides tips in setting and using breakpoints. If not familiar with this debugger, you can find a basic introduction in the EE article loc…
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.

705 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question