-2

In the function

int foo(int i, int j)
{
    return i + j;
}

both i and j are uninitialized, so this function could be called with and only with two arguments:

foo(5, 6); // 11

Okay, but C++ featured default parameters, and made possible definitions like:

int foo(int i = 1, int j = 2)
{
    return i + j;
}

Here both i and j has default arguments, then calls:

foo(5, 6); // 11
foo(5); // 7
foo(); // 3

are possible. So then, why this definition:

int foo(int i += 1, int j += 2)
{
    return i + j;
}

And these calls:

foo(5, 6); // 14
foo(5); // 8
foo(); // 3

are impossible? Is it difficult to represent this definition or the calls causes some problems? I want to know.

Keith Thompson
  • 230,326
  • 38
  • 368
  • 578
Netherwire
  • 2,391
  • 1
  • 22
  • 41

2 Answers2

6

Because it makes no sense. It's one thing to assign a value, it's another to increment a pre-existing value. What would you expect the value of i to be in this (invalid) code snippet?

// huh?
int i += 12;

i never had a value, so it makes no sense to add to it.

Ed S.
  • 115,705
  • 20
  • 165
  • 244
3

Declarations (with or without initializers) and assignments are different things, even if there are some similarities in the syntax.

This:

int i;

declares a variable i of type int. This:

int i = 42;

does the same and specifies its initial value 42.

This:

i = 42;

looks a lot like the declaration with the initializer, but it assigns the value 42 to an existing variable which has to have been declared previously. This:

i += 42;

is a compound assignment, essentially equivalent to i = i + 42;.

Initialization is similar to a simple assignment, not to a compound assignment. An initializer specifies the initial value of a variable by providing an expression to be evaluated and stored in the variable. You can't just have arbitrary expressions and statements in that context.

If you want to have a parameter whose value is, say, 2 greater than the value passed as an argument, you can do that within the function:

int foo(int i, int j)
{
    i ++;
    j += 2;
    return i + j;
}

though that cases is more clearly and simply written as:

int foo(int i, int j)
{
    return i + j + 3;
}
Keith Thompson
  • 230,326
  • 38
  • 368
  • 578
  • Thanks, your answer differs from "no because of no". – Netherwire Oct 21 '13 at 22:16
  • Actually, `int i;` ___[defines](http://stackoverflow.com/a/1410632/140719)___ a variable. – sbi Oct 21 '13 at 22:20
  • 3
    @sbi: Yes, it defines a variable. It also declares it. – Keith Thompson Oct 21 '13 at 22:26
  • @Keith: Yes, you are human. You are also an ape. (If someone came along who couldn't tell a human from a monkey, would consider it accurate to be introduced as an ape?) – sbi Oct 22 '13 at 05:59
  • 1
    @sbi: I didn't think the distinction was very important in this context, and guessed that "declares" might be clearer than "defines" to the OP. If I were going for language-lawyer levels of precision (which I often do), I would have written "object" rather than "variable". – Keith Thompson Oct 22 '13 at 06:11