/* H O W T H E C A L L S T A C K W O R K S Ron Kessler Created 1/1/2017 On windows the stack is 1MB in size and cannot be changed */ #include "stdafx.h" #include using namespace std; // function prototypes int add (int x, int y); //STEP 1: The main() method is pushed onto the stack by the O/S when we launch this app int main () { //the local variables on stored in the main() stack frame int result; //STEP 2: When this line runs the add function is pushed onto the stack in its // own stack frame and all parameters/locals are stored with it. The add frame also holds //the memory location of the main() function so when add finishes, control can return ti main(). result = add (50, 25); //STEP 4 With add() gone, main() is now on top of the stack so the app resumes executing until main finishes. cout << "The sum is: " << result << '\n'; return 0; } int add (int x, int y) { int answer = x + y; //local var stored in the add() frame return x + y; //return value can be held in the CPU registers or in the stack frame } //STEP 3 Add is popped off the call stack here.