Solved

"not all control paths return a value"

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

Industry Leaders: 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

Suggested Solutions

Title # Comments Views Activity
IdTCPClient1->Disconnect(); not working 3 87
thread-safe code in c++ 2 122
C++ mouse_event mouse look 7 112
c++, dynamic object by json 1 60
Article by: SunnyDark
This article's goal is to present you with an easy to use XML wrapper for C++ and also present some interesting techniques that you might use with MS C++. The reason I built this class is to ease the pain of using XML files with C++, since there is…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.

726 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