Scope and return values in C++

When the function terminates, the
following steps happen:

  • The function’s return value is
    copied into the placeholder that was
    put on the stack for this purpose.

  • Everything after the stack frame
    pointer is popped off. This destroys
    all local variables and arguments.

  • The return value is popped off the
    stack and is assigned as the value
    of the function. If the value of the
    function isn’t assigned to anything,
    no assignment takes place, and the
    value is lost.

  • The address of the next instruction
    to execute is popped off the stack,
    and the CPU resumes execution at
    that instruction.

The stack and the heap

Leave a Comment