Solved

"not all control paths return a value"

Posted on 2001-08-09
3
527 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 50 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

Get HTML5 Certified

Want to be a web developer? You'll need to know HTML. Prepare for HTML5 certification by enrolling in July's Course of the Month! It's free for Premium Members, Team Accounts, and Qualified Experts.

Question has a verified solution.

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

Written by John Humphreys C++ Threading and the POSIX Library This article will cover the basic information that you need to know in order to make use of the POSIX threading library available for C and C++ on UNIX and most Linux systems.   [s…
What is C++ STL?: STL stands for Standard Template Library and is a part of standard C++ libraries. It contains many useful data structures (containers) and algorithms, which can spare you a lot of the time. Today we will look at the STL Vector. …
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
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…

617 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