POV-Ray : Newsgroups : povray.off-topic : Teach yourself C++ in 21 days : Re: Teach yourself C++ in 21 strange malfunctions Server Time
29 Jul 2024 16:22:41 EDT (-0400)
  Re: Teach yourself C++ in 21 strange malfunctions  
From: Warp
Date: 17 Apr 2012 15:51:14
Message: <4f8dc9b2@news.povray.org>
Invisible <voi### [at] devnull> wrote:
> I'm still a bit baffled that this is a valid thing to do, however...

  There's some baggage from C that would have better been dropped, but
as you may know, backwards compatibility can be a real b***h sometimes...

  It can be argued using the same logic as with uninitialized variables,
though: There are situations where execution never reaches the end of the
function, and hence having a 'return' statement there would be useless.
In most situations putting such a useless 'return' there is only a minor
inconvenience, but in theory it could be more complicated. Also, it makes
the function larger, so the 80's C hackers didn't like that (in the same
way as they didn't like the idea of spending a clock cycle to initialize
a variable with a value that would immediately be overwritten anyways).

  This would be an example of such a function:

std::string foo(int parameter)
{
    assert(parameter >= 0 && parameter < 4);
    switch(parameter)
    {
     case 0: return "Hello";
     case 1: return "there";
     case 2: return "world";
     case 3: return "!";
    }

    // Execution will *never* reach here
    // It's valid to omit a 'return'
}

-- 
                                                          - Warp


Post a reply to this message

Copyright 2003-2023 Persistence of Vision Raytracer Pty. Ltd.