double test()
{
double a,b,c;
for(int i=0; i<n; i++){
a = i*10;
b = a-i;
c += a*b;
}
return c;
double test()
{
double c;
for(int i=0; i<n; i++){
double a = i*10;
double b = a-i;
c += a*b;
}
return c;
Answer is : the later
consider if n == 0. if n==0 for your second example the varibles would
never have to be allocated.
If they will always be allocated (n > 0) then the difference is assignment
.vs. initialization. For simple types (double, float, etc...) it probably
doesn't make much difference. For classes, however, it can if they have
constructors that take time.
of the main reasons is so you know what the variable is declared. If a
function is large and you come across
a = 10;
what is a? Is it int? double? char? a class? You'd have to scroll up to
the top of the function to find out. However
double a = 10;
makes it easier to read/maintain the code IMO.