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 7592775
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T20:59:28+00:00 2026-05-30T20:59:28+00:00

Assume I have the following function: // Precondition: foo is ‘0’ or ‘MAGIC_NUMBER_4711’ //

  • 0

Assume I have the following function:

// Precondition:  foo is '0' or 'MAGIC_NUMBER_4711'
// Returns:       -1 if foo is '0'
//                 1 if foo is 'MAGIC_NUMBER_4711'
int transmogrify(int foo) {
    if (foo == 0) {
        return -1;
    } else if (foo == MAGIC_NUMBER_4711) {
        return 1;
    }
}

The compiler complains “missing return statement”, but I know that foo never has different values than 0 or MAGIC_NUMBER_4711, or else my function shall have no defined semantics.

What are preferable solutions to this?
Is this really an issue, i.e. what does the standard say?

  • 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-30T20:59:29+00:00Added an answer on May 30, 2026 at 8:59 pm

    Sometimes, your compiler is not able to deduce that your function actually has no missing return. In such cases, several solutions exist:

    Assume the following simplified code (though modern compilers will see that there is no path leak, just exemplary):

    if (foo == 0) {
        return bar;
    } else {
        return frob;
    }
    

    Restructure your code

    if (foo == 0) {
        return bar;
    }
    return frob;
    

    This works good if you can interpret the if-statement as a kind of firewall or precondition.

    abort()

    if (foo == 0) {
        return bar;
    } else {
        return frob;
    }
    abort(); return -1; // unreachable
    

    Return something else accordingly. The comment tells fellow programmers and yourself why this is there.

    throw

    #include <stdexcept>
    
    if (foo == 0) {
        return bar;
    } else {
        return frob;
    }
    
    throw std::runtime_error ("impossible");
    

    Disadvantages of Single Function Exit Point

    flow of control control

    Some fall back to one-return-per-function a.k.a. single-function-exit-point as a workaround. This might be seen as obsolete in C++ because you almost never know where the function will really exit:

    void foo(int&);
    
    int bar () {
        int ret = -1;
        foo (ret);
        return ret;
    }
    

    Looks nice and looks like SFEP, but reverse engineering the 3rd party proprietary libfoo reveals:

    void foo (int &) {
        if (rand()%2) throw ":P";
    }
    

    This argument does not hold true if bar() is nothrow and so can only call nothrow functions.

    complexity

    Every mutable variable increases the complexity of your code and puts a higher burden on the cerebral capacity on your code’s maintainer. It means more code and more state to test and verify, in turn means that you suck off more state from the maintainers brain, in turn means less maintainer’s brain capacity left for the important stuff.

    missing default constructor

    Some classes have no default construction and you would have to write really bogus code, if possible at all:

    File mogrify() {
        File f ("/dev/random"); // need bogus init because it requires readable stream
        ...
    }
    

    That’s quite a hack just to get it declared.

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

Sidebar

Related Questions

Assume I have a method/function with the following signature: foo($bar = 0) Inside foo
Lets assume I have the following Java code: public String foo() { // returns
Lets assume we have the following code: abstract class Base1 { protected int num;
Possible Duplicate: Why is this function returning “undefined”? Let's assume I have the following
I've been wondering about the following issue: assume I have a C style function
Assume I have the following example: Example One $('.my_Selector_Selected_More_Than_One_Element').each(function() { $(this).stuff(); $(this).moreStuff(); $(this).otherStuff(); $(this).herStuff();
Assume you have several arbitrary classes like below: class Foo { $foovar; public function
I have a static library. This library have the following function defined int WriteData(LPTSTR
I have the following typedef function prototype: typedef void (*sa_sigaction_t)(int, siginfo_t *, void *);
Assume I have the following string: Hellotoevryone<img height=115 width=150 alt= src=/Content/Edt/image/b4976875-8dfb-444c-8b32-cc b47b2d81e0.jpg />Iamsogladtoseeall. This

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.