Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7488705
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T15:00:58+00:00 2026-05-29T15:00:58+00:00

All my functions look like this: short Function() { short ret = 0; ret

  • 0

All my functions look like this:

short Function()
{
   short ret = 0;

   ret = FunctionA();   
   if(ret != 0) return ret;

   ret = FunctionB();
   if(ret != 0) return ret;

   ret = FunctionC();
   if(ret != 0) return ret;

   return 0;
}

Is there a nicer way to write this? Without having to repeat

if(ret != 0) return ret;

all the time?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-29T15:00:59+00:00Added an answer on May 29, 2026 at 3:00 pm

    If using a short-circuited || like suggested in the other answer is not an option, you could define a macro for that:

    #define TRY(var, x) if ((var = (x))) return var
    

    Then in your code:

    short Function()
    {
      short ret;
    
      TRY(ret, FunctionA());
      TRY(ret, FunctionB());
      TRY(ret, FunctionC());
    
      return 0;
    }
    

    NOTE: You should be very careful when deciding to use macros, but in this case I think it can be a clean way to solve the problem.
    It must be mentioned, though, that these statements conceil the fact that the function could return early at every one of them. If you have open resource handles (file descriptors, pointers to malloced data, …), they will leak. You and everyone working with the code should be aware of this and use appropriate error handling and cleanup routines for more complex cases than this one.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

normally my ajax functions look something like this: function ajaxCallback(url,functionToRun) { if (window.XMLHttpRequest) {//
If you take a look at the clock_gettime() function, which is available in all
Is this proposition true? For all functions f f(a+b) = f(a) + f(b) .
Hi I have a plugin.dll which contains this all npp functions I have embeded
I have a set of nested ul's that look like this: <ul id=educationList> <li
Should I validate input parameters on all functions I create? Input isn't passed from
If you code in C and configure your compiler to insist that all functions
To be truly standards-compliant, must all functions in C (except for main) have a
We have multiple vb projects.We want to put error handlers in all functions, and
How can I check whether all JavaScript functions have loaded properly on a page?

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.