Set Variables to 0?

Why would you set the variable to 0? What does this do? Here's an example:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
#include <iostream>

using namespace std;
int square(int num1, int num2);

int main()
{
	int number1, number2;

	cout << "Enter 2 numbers. " << endl;
	cin >> number1 >> number2;

	int sum = square(number1, number2);
		cout << "Your numbers squared and added together are " << sum << endl;

	return 0;
}
int square(int num1, int num2)
{
	int sum = 0; //Why would you this variable to 0?

	for (int i = num1; i <= num2; i++)
	{
		int sq = i * i;
		sum += sq;
	}
	return sum;
}

I don't understand why you would do this. Thank you in advance.
If you don't, its value could be anything and the sum would start at that value instead of 0, as you would except. If you are looking for a more mathematical reason, it is because sum is, as its name implies, storing a sum, and so the initial value ought to be the empty sum (which is 0; the sum of no numbers is nothing).

Reason is simple, if the variable is not initialized then the variable may show any garbage value. In case if variable "sum" fails to compute a value it will return a garbage value. Also it may return incorrect value even if the sum is being computed. So to get some meaningful result at the end we initialize the variable.

e.g

if you do not initialize variable, then

1
2
3
4
5
6
int sum;

for(int i=1;i<4;i++)
{
 sum+=i;
}

Expected result=6;
but since variable is not initialized it can take any initial value and result may be a random value.

Oh okay, I see. So, if I didn't set the variable to 0 in my example, it wouldn't know what sum += sq; would be. It would bring up an error. So, basically setting it to 0, shows to start from 0. But, why wouldn't that be a given? Why wouldn't int sum; already equal 0. Since, it wasn't given a value. I'm sorry for not really grasping it. Thank you.
When you declare a variable, a certain number of bytes in memory get allocated to represent that variable.

If, after having declared the variable, you don't give it an initial value, the value of the variable will be that of whatever was in that memory location at the time is was created - hence, garbage data.

Since this garbage data could be anything, there's no way of knowing what will happen if you attempt to use variables with garbage data - hence, undefined behaviour.

In your code, you attempt to add i to sum, which is uninitialized.
Which boils down to this:

sum = garbage + i


Wikipedia says it best:
A common assumption made by novice programmers is that all variables are set to a known value, such as zero, when they are declared. While this is true for many languages, it is not true for all of them, and so the potential for error is there. Languages such as C use stack space for variables, and the collection of variables allocated for a subroutine is known as a stack frame. While the computer will set aside the appropriate amount of space for the stack frame, it usually does so simply by adjusting the value of the stack pointer, and does not set the memory itself to any new state (typically out of efficiency concerns). Therefore, whatever contents of that memory at the time will appear as initial values of the variables which occupy those addresses.
Last edited on
Efficiency.

1
2
int x;
std::cin >> x;

Line 2 always* overwrites the value of x and therefore setting x to 0 on line 1 would be a waste of time.


*Input could fail though, but then x should be treated as undefined anyway.
Awesome! You guys are all awesome. Thank you very much for clarifying everything for me.
Topic archived. No new replies allowed.