c++returnvoid

Why would you put an explicit "return;" at the end of a void function?


Is there has any different by putting or not putting return in the the last line of function?

void InputClass::KeyDown(unsigned int input)
{
    // If a key is pressed then save that state in the key array
    m_keys[input] = true;
    return;
}

Solution

  • No, there is no difference !

    return in void functions is used to exit early on certain conditions.

    Ex:

    void f(bool cond)
    {
        // do stuff here
        if(cond)
            return;
        // do other stuff
    }