The difference of 2 functions

Hello. What's the difference of this code:

1
2
3
4
5
6
7
	int pop()
	{
		int val = array[count];
		--count;
		val = array[count];
		return val;
	}

To this code:

1
2
3
4
5
	int pop()
	{
		--count;
		return array[count];
	}

How come the first function changes the value of the stack by simply returning a value?
Line 3 in your first snippet doesn't do anything. when you realise this you can see they're the same. I think :)
In line 3, you are just putting the contents of array[count] into the integer val and returning val instead of array[count].
In line 3,


.. ignore line 3. Because in line 5 the value for 'val' get overwritten anyway.
The difference is that one doesn't work, and the other almost works but gives in to the dark evil forces that add extra lines to people's code.
but they do the same thing do they not?
Unfortunately yes. It's just that the first one tried and failed whereas the second is simply wrong.
Last edited on
Topic archived. No new replies allowed.