The difference of 2 functions

Sep 19, 2014 at 6:59am
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?
Sep 19, 2014 at 8:39am
Line 3 in your first snippet doesn't do anything. when you realise this you can see they're the same. I think :)
Sep 19, 2014 at 12:11pm
In line 3, you are just putting the contents of array[count] into the integer val and returning val instead of array[count].
Sep 19, 2014 at 12:49pm
In line 3,


.. ignore line 3. Because in line 5 the value for 'val' get overwritten anyway.
Sep 19, 2014 at 1:01pm
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.
Sep 19, 2014 at 1:18pm
but they do the same thing do they not?
Sep 19, 2014 at 1:18pm
Unfortunately yes. It's just that the first one tried and failed whereas the second is simply wrong.
Last edited on Sep 19, 2014 at 1:18pm
Topic archived. No new replies allowed.