You write:
I know we are popping off an object off stack each time it is called we are returning a double at the end when the base case is reached.Is stack being modified each time it is called?
So you know an object is popped off the stack, which means the stack changes. So why would you think the stack as not modified?
Are you maybe confusing the contents of the variable stack
, with the object it references? The contents of the variable stack
do not change - that contents is a reference to an object (or type NSMutableArray
). However the internal state of the referenced object change, each time removeLastObject
is called.
Think of it like a house - which has a street address (the reference to the object), and some people in it (the internal state of the object) - as people enter and leave the house (items pushed and popped from the stack) then the internal state keeps changing, but the street address of the house never changes.
HTH.