{"id":9204,"date":"2020-02-03T15:04:47","date_gmt":"2020-02-03T14:04:47","guid":{"rendered":"https:\/\/blogs.mentor.com\/colinwalls\/?p=9204"},"modified":"2026-03-26T17:02:11","modified_gmt":"2026-03-26T21:02:11","slug":"many-happy-returns","status":"publish","type":"post","link":"https:\/\/blogs.sw.siemens.com\/embedded-software\/2020\/02\/03\/many-happy-returns\/","title":{"rendered":"Many happy returns"},"content":{"rendered":"<p>It is widely recognized that a very high priority for software developers is writing clear, understandable, and, hence, maintainable code. There are numerous guidelines available to achieve this goal. Some, like MISRA C for example, focus on writing safe, reliable code, and this is often achieved by maximizing readability. Other \u201cstyle guides\u201d concentrate exclusively of readability and clarity. A particular matter, that most guidelines have something to say about, is the choice of a single or multiple returns from a function \u2026<!--more--><\/p>\n<p>One school of thought is that there should be a single return point in a function, which is right at the end. This is advised by MISRA C and other standards and mandated by some certification methodologies. I want to explore the possibilities.<\/p>\n<p>Imagine that we have a function <strong>fun()<\/strong> that takes 3 int parameters <strong>a<\/strong>, <strong>b<\/strong>, and <strong>c<\/strong>. Valid values for these parameters are greater than or equal to 10, 20 and 30 respectively. It would seem logical to check the parameters\u2019 values first, then enter the body of the function\u2019s code. The function returns an error code [<strong>ERRORA<\/strong> etc.] if a parameter is out of range and <strong>SUCCESS<\/strong> otherwise.<\/p>\n<p>Here is how I might code it in an \u201cobvious way\u201d, using multiple returns:<\/p>\n<pre><strong>#define ERRORA 1<\/strong>\n<strong>#define ERRORB 2<\/strong>\n<strong>#define ERRORC 3<\/strong>\n<strong>#define SUCCESS 0<\/strong>\n\n<strong>int fun(int a, int b, int c)<\/strong>\n<strong>{<\/strong>\n<strong>   if (a &lt; 10)<\/strong>\n<strong>      return ERRORA;<\/strong>\n<strong>   if (b &lt; 20)<\/strong>\n<strong>      return ERRORB;<\/strong>\n<strong>   if (a &lt; 30)<\/strong>\n<strong>      return ERRORC;<\/strong>\n\n<strong>   \/\/ main function code is here<\/strong>\n\n<strong>   return SUCCESS;<\/strong>\n<strong>}<\/strong><\/pre>\n<p>This, IMHO, is quite clear, so I would be quite happy with this code. However, I do appreciate that this is a very simple example and parameter verification could be much more complex. Also, if it were certified code, I would not be allowed to code this way. I will try an alternative:<\/p>\n<pre><strong>#define ERRORA 1<\/strong>\n<strong>#define ERRORB 2<\/strong>\n<strong>#define ERRORC 3<\/strong>\n<strong>#define SUCCESS 0<\/strong>\n\n<strong>int fun(int a, int b, int c)<\/strong>\n<strong>{<\/strong>\n<strong>   int return_code;<\/strong>\n\n<strong>   if (a &lt; 10)<\/strong>\n<strong>      return_code = ERRORA;<\/strong>\n<strong>   else<\/strong>\n<strong>      if (b &lt; 20)<\/strong>\n<strong>         return_code = ERRORB;<\/strong>\n<strong>      else<\/strong>\n<strong>         if (a &lt; 30)<\/strong>\n<strong>            return_code = ERRORC;<\/strong>\n<strong>         else<\/strong>\n<strong>         {<\/strong>\n<strong>            return_code = SUCCESS;<\/strong>\n\n<strong>            \/\/ main function code is here<\/strong>\n<strong>         }<\/strong>\n\n<strong>   return return_code;<\/strong>\n<strong>}<\/strong><\/pre>\n<p>Although this code complies with various standards and codes of practice, my view is that it is very hard to follow and could easily become more complex and completely unreadable. There must be another way. Here is one:<\/p>\n<pre><strong>#define ERRORA 1<\/strong>\n<strong>#define ERRORB 2<\/strong>\n<strong>#define ERRORC 3<\/strong>\n<strong>#define SUCCESS 0<\/strong>\n\n<strong>int fun(int a, int b, int c)<\/strong>\n<strong>{<\/strong>\n<strong>   int return_code = SUCCESS;<\/strong>\n\n<strong>   if (a &lt; 10)<\/strong>\n<strong>   {<\/strong>\n<strong>      return_code = ERRORA;<\/strong>\n<strong>      goto exit;<\/strong>\n<strong>   }<\/strong>\n<strong>   if (b &lt; 20)<\/strong>\n<strong>   {<\/strong>\n<strong>      return_code = ERRORB;<\/strong>\n<strong>      goto exit;<\/strong>\n<strong>   }<\/strong>\n<strong>   if (c &lt; 30)<\/strong>\n<strong>   {<\/strong>\n<strong>      return_code = ERRORC;<\/strong>\n<strong>      goto exit;<\/strong>\n<strong>   }<\/strong>\n\n<strong>   \/\/ main function code is here<\/strong>\n\n<strong>exit: <\/strong>\n<strong>   return return_code;<\/strong>\n<strong>}<\/strong><\/pre>\n<p>This code is mostly compliant with standards, as it has a single return, but it does use <strong>goto<\/strong>. A forward jump using a <strong>goto<\/strong> is acceptable to many developers, but I feel it should avoided if at all possible. Here is another way:<\/p>\n<pre><strong>#define ERRORA 1<\/strong>\n<strong>#define ERRORB 2<\/strong>\n<strong>#define ERRORC 3<\/strong>\n<strong>#define SUCCESS 0<\/strong>\n\n<strong>int fun(int a, int b, int c)<\/strong>\n<strong>{<\/strong>\n<strong>   int return_code = SUCCESS;<\/strong>\n\n<strong>   if (a &lt; 10)<\/strong>\n<strong>      return_code = ERRORA;<\/strong>\n<strong>   if (b &lt; 20)<\/strong>\n<strong>      return_code = ERRORB;<\/strong>\n<strong>   if (c &lt; 30)<\/strong>\n<strong>      return_code = ERRORC;<\/strong>\n\n<strong>   if (return_code == SUCCESS)<\/strong>\n<strong>   {<\/strong>\n<strong>      \/\/ main function code is here<\/strong>\n<strong>   }<\/strong>\n\n<strong>   return return_code;<\/strong>\n<strong>}<\/strong><\/pre>\n<p>I think that this is probably the \u201ccleanest\u201d compliant solution, but I am open to suggestions by comment, email or via social media.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>It is widely recognized that a very high priority for software developers is writing clear, understandable, and, hence, maintainable code&#8230;.<\/p>\n","protected":false},"author":71677,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"spanish_translation":"","french_translation":"","german_translation":"","italian_translation":"","polish_translation":"","japanese_translation":"","chinese_translation":"","footnotes":""},"categories":[1],"tags":[315,339,300,564,340,478],"industry":[],"product":[],"coauthors":[],"class_list":["post-9204","post","type-post","status-publish","format-standard","hentry","category-news","tag-certification","tag-development-tools","tag-embedded-software","tag-misra-c","tag-programming-languages","tag-sourcery-codebench"],"_links":{"self":[{"href":"https:\/\/blogs.sw.siemens.com\/embedded-software\/wp-json\/wp\/v2\/posts\/9204","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/blogs.sw.siemens.com\/embedded-software\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blogs.sw.siemens.com\/embedded-software\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blogs.sw.siemens.com\/embedded-software\/wp-json\/wp\/v2\/users\/71677"}],"replies":[{"embeddable":true,"href":"https:\/\/blogs.sw.siemens.com\/embedded-software\/wp-json\/wp\/v2\/comments?post=9204"}],"version-history":[{"count":1,"href":"https:\/\/blogs.sw.siemens.com\/embedded-software\/wp-json\/wp\/v2\/posts\/9204\/revisions"}],"predecessor-version":[{"id":9687,"href":"https:\/\/blogs.sw.siemens.com\/embedded-software\/wp-json\/wp\/v2\/posts\/9204\/revisions\/9687"}],"wp:attachment":[{"href":"https:\/\/blogs.sw.siemens.com\/embedded-software\/wp-json\/wp\/v2\/media?parent=9204"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blogs.sw.siemens.com\/embedded-software\/wp-json\/wp\/v2\/categories?post=9204"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blogs.sw.siemens.com\/embedded-software\/wp-json\/wp\/v2\/tags?post=9204"},{"taxonomy":"industry","embeddable":true,"href":"https:\/\/blogs.sw.siemens.com\/embedded-software\/wp-json\/wp\/v2\/industry?post=9204"},{"taxonomy":"product","embeddable":true,"href":"https:\/\/blogs.sw.siemens.com\/embedded-software\/wp-json\/wp\/v2\/product?post=9204"},{"taxonomy":"author","embeddable":true,"href":"https:\/\/blogs.sw.siemens.com\/embedded-software\/wp-json\/wp\/v2\/coauthors?post=9204"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}