Thought Leadership

One return from a function: a good idea?

All C/C++ functions have a single point of entry and it is widely thought that a single point of exit is logical. Indeed, a number of programming standards [MISRA C for example] insist on this approach. The logic is that a single return statement makes for clearer, easier to read code. Additionally, a single exit point means that there is less chance of failing to execute function exit code, which may deallocate resources etc. Such an error leads to memory leaks and the like. The contrary argument is that an early return might avoid the need for some convoluted logic to direct the execution flow to the end of the function – a nest of if … else constructs can be hard to read.

I have been pondering an alternative approach that might deliver the best of both worlds …

When I come across complex if … else constructs, my mind always turns to the use of a switch statement, as this almost always results in clearer code. Compilers are also likely to generate efficient code from a switch, which is a bonus. I figured that I might use a switch to code a function as a state machine. Here is my quick and dirty outline code for this:

#define PARAMCHECK 1
#define ALLOCATERESOURCES 2
#define PARAMERR 3
#define RESOURCEERR 4
#define PROCESS 5
#define PROCESSERR 6
#define COMPLETE 7

int fun(int p)
{
   int Exit=0;
   int State=PARAMCHECK;
 
   while (!Exit)
   {
      switch (State)
      {
      case PARAMCHECK:
         // check parameters ...
         if (OK)
            State = ALLOCATERESOURCES;
         else
            State = PARAMERR;
         break;
      case ALLOCATERESOURCES:
         // allocate resources ...
         if (OK)
            State = PROCESS;
         else
            State = RESOURCEERR;
         break;
      case PROCESS:
         //
         // *** do main processing here ***
         //
         // occasional error checking:
         if (ERROR)
         {
            State = PROCESSERR;
            break;
         }
         //
         State = COMPLETE;
         break;
      case PARAMERR:
      case RESOURCEERR:
         Exit = TRUE;
         break;
      case PROCESSERR:
      case COMPLETE:
         // deallocate resources ...
         Exit = TRUE;
         break;
      }
   }
 
   return State;
}

Does it make sense? Feel free to pull my code apart via email or social media.

Colin Walls

I have over thirty years experience in the electronics industry, largely dedicated to embedded software. A frequent presenter at conferences and seminars and author of numerous technical articles and two books on embedded software, I am a member of the marketing team of the Mentor Graphics Embedded Systems Division, and am based in the UK. Away from work, I have a wide range of interests including photography and trying to point my two daughters in the right direction in life. Learn more about Colin, including his go-to karaoke song and the best parts of being British: http://go.mentor.com/3_acv

More from this author

Comments

3 thoughts about “One return from a function: a good idea?

Leave a Reply

This article first appeared on the Siemens Digital Industries Software blog at https://blogs.sw.siemens.com/embedded-software/2017/06/26/one-return-from-a-function-a-good-idea/