a BUG!!

The code explains it all, I have a variable in my main function, I want to increment it via a function named Increment, when I log the variable in the console, It remains same!!...help please! here is my code.....

1
2
3
4
5
6
7
8
9
10
11
12
13
  #include <iostream>
#define LOG(x) std::cout << x << std::endl
void Increment(int value)
{
	value++;
}

int main()
{
	auto a = 5.33f;
	Increment(a);
	LOG(a);
}
You have to pass the value by reference, otherwise you're just passing a copy. The copy is changed and then lost when the function returns.
1
2
3
void Increment(int& value) {
    ++value;
}
Last edited on
Not "a bug" but "multiple bugs".

If you want its value to change when passing through a function, then you will need to pass arguments by reference, not value.

You are multiply inconsistent with the type of argument - trying to call it with a float, and receive and increment it as an int.
It gives me error on line 11!!!...and where will I learn reference?
I GOT IT ..............BUT!......where will I learn references? pleas give me some link if you can..
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#include <iostream>

//the '&' means it is by reference
void Increment(int &value)
{
	value++;
}


int main()
{
  int i = 5;
  
  Increment(i);
  
  std::cout << i;
  
}


Look at this.

1
2
3
4
5
6
7
8
9
10
11
12
#include <iostream>


int main()
{
  int i = 5;
  
  
  //notice what the '&' symbol does here.
  std::cout << &i;
  
}


Pass by reference means you pass the address of the variable to the function and not just the value.
You're muddling things up. In your second code snippet, you're displaying the address of i.

But in your first code snippet, the & has nothing to do with addresses. It means that value is a reference.
Yes Mikey, but this is how I finally understood that the '&' means something. It never made sense to me until someone once posted this code, cout << &i; I was able to bridge the connection. I know that an address and a reference are two different things, but looking at it this way worked for me when I was starting out.

If you simply try to look up definitions of reference you end up with this crap...
In the C++ programming language, a reference is a simple reference datatype that is less powerful but safer than the pointer type inherited from C. The name C++ reference may cause confusion, as in computer science a reference is a general concept datatype, with pointers and C++ references being specific reference datatype implementations. The definition of a reference in C++ is such that it does not need to exist. It can be implemented as a new name for an existing object (similar to rename keyword in Ada).


That kind of stuff never really helped me.
Last edited on
In C++ by default all arguments are passed by value. This means that the variable used in the function gets a copy of the data used in the function call.

1
2
3
4
5
6
7
8
9
10
void Increment(int v)
{
	v++;
}

int main()
{
	int a = 5;
	Increment(a);
}


Here the value of a (5) is passed as the argument to function Increment. In Increment the variable v receives the value of a - is copied. There is no 'connection' between a and v. Any changes made to v in Increment() do not alter the value of a in main(). a can be const or non-const or r-value etc.

If you want Increment() to affect the value of the passed argument in the calling function then you pass by reference.

1
2
3
4
5
6
7
8
9
10
void Increment(int& v)
{
	v++;
}

int main()
{
	int a = 5;
	Increment(a);
}


Now when the valve of v is changed in Increment(), the value of a in main() is also changed. This is pass by reference. a cannot be const or r-value.

However this means that a in main() cannot be const (or r-value) as it can be when passed as value (as a copy is done). If the size of the data that is being passed is large, then passing by value means doing a copy which is expensive in terms of performance. Pass by reference has much better performance. In order to accommodate both (pass by reference by performance and const data), then you use

1
2
3
4
5
6
7
8
9
10
void Increment(const int& v)
{
	//v++; This now generates an error as v is const
        std::cout v << std:endl;
}

int main()
{
	Increment(5);   // This is OK now as passed by const ref
}


Behind the scenes, pass by reference is usually implemented using pointers (pass by value of a pointer). So only the number of bytes for a pointer is copied - not the size of the passed type. This can drastically improve performance for types like string. vector etc and why you often seen const std::string& as the type of a function argument.
Last edited on
Topic archived. No new replies allowed.