The i variable is not reset to 2 every time numPrime is called from goldBach. It is set to 2 only when the program starts, or when you explicitly set it to 2 in your own code.