-1

I have confusion about why function increment is not returning value 11 is this because a is local to function increment so that we cannot access it in the main function.

#include <iostream>

using namespace std;

int increment(int a) {
  a=a+1;
  return a;
}

int main() {
  int a=10;
  increment(a);
  cout<<a;
  return 0;
}
chux - Reinstate Monica
  • 143,097
  • 13
  • 135
  • 256
vikash
  • 11
  • 2
  • You're passing `a` by value, so you're incrementing a copy of the value. Said copy is then discarded at the end of the function, and the original is left untouched. – Etienne de Martel Oct 31 '20 at 16:23
  • 1
    Try `a=increment(a);` – JHBonarius Oct 31 '20 at 16:24
  • 1
    @EtienneDeMartel discarded at the end of the function? It is returned. The value is discarded when the function is called. – JHBonarius Oct 31 '20 at 16:26
  • can you explain what do you mean a by value – vikash Oct 31 '20 at 16:27
  • @vikash variables as you know them are _values_. They just have some type of value to them. Those values/variables reside at an _address_ in memory (of some kind). A _pointer_ (e.g. `int*`) then is a variable/value, which is equal to the address of some other variable/value, of type `int`. When you pass something to a function (pointer or otherwise), it makes a copy of that value. So a passed pointer makes a copy of a pointer's _address_ (not its contents!), and a passed value makes a copy of the value. C++ also has pass-by-reference semantics (`int&`) – Rogue Oct 31 '20 at 16:29
  • @vikash -- What [C++ books](https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list) are you reading that states that `a` will change? There is not one C++ book out there that will say that `a` changes value when passed by value. So I'm wondering what or who gave you this wrong information. – PaulMcKenzie Oct 31 '20 at 16:30
  • Either `increment(a);` --> `a = increment(a);` or `int increment(int a)` --> `int increment(int & a)`. – chux - Reinstate Monica Oct 31 '20 at 18:50
  • @Rogue. No, a variable is a reserved space in memory that holds a value. @vikash The function call holds a copy of the value in its own LOCAL and PRIVATE, parameter variable a, increments it and returns another copy the value to the caller in a unnamed variable on the stack. It's up to you to then store the value in a variable., with an assignment statement as in `a = increment(a);` To increment the actual variable passed as a parameter, you must pass a reference (or a pointer) to the actual variable. – Michaël Roy Oct 31 '20 at 19:55
  • @MichaëlRoy Did I not say just that? `Those values/variables reside at an address in memory` – Rogue Oct 31 '20 at 23:06
  • @Rogue. You said, I qquote: "variables as you know them are values". and "They just have some type of value to them" I don't want to start a fight, I just wanted to make sure that Vikash, who is a novice, understands clearly that these are two very different concepts. A variable dees not have a type of value in int. A variable _holds_ a value, and that value is of a _specific_ type. I just felt your defnition lacked the precision needed for a beginner to grasp the concepts correctly. Precise engineering needs precise definitions. Especiallly in c++. That's all I meant and nothing more. – Michaël Roy Nov 01 '20 at 00:47
  • @MichaëlRoy thanks I got it now. btw you are good at explaining and thanks for not using gibberish. – vikash Nov 02 '20 at 16:15
  • @vikash You're welcome. Have fun learning c++! – Michaël Roy Nov 02 '20 at 17:46

3 Answers3

0

The function is returning the incremented value, but you ignore the returned value. You will see 11 with this

int main() {
  int a=10;
  int b = increment(a);
  cout<<b;
}
463035818_is_not_an_ai
  • 109,796
  • 11
  • 89
  • 185
0

Your function well return 11 but you expect variable 'a' to be changed and it is not the case because it was passed by value and not by reference.

You called the function increment(a) but you don't use the returned value.

If you cout increment(a) or initialize a variable with increment(a) you will notice that the return is 11 but the variable 'a' value is still 10. Try

int var = increment(a); std::cout << var << std::endl; // print 11

or

std::cout << increment(a) << std::cout //print 11
Sambalika
  • 100
  • 7
-1

You need to send the value by reference.

int increment(int &a){

Ayan Bhunia
  • 479
  • 3
  • 14