views:

112

answers:

3

Given:

  x = MyFunc(2);

My understanding:

The variable x is assigned to the function MyFunc(2).

First, MyFunc( ) is called. When it returns, its return value if any, is assigned to x.?

+7  A: 

No. x is assigned to the evaluated result of MyFunc(2).

The value returned depends on what MyFunc does. It could be anything and does not need to be 2.

Frank
+6  A: 

This cannot be answered completely without:

  • x's declaration
  • MyFunc's declaration
  • MyFunc's definition

But your sentence "When MyFunc(2) is called it returns the value 2 to x" is wrong. MyFunc is invoked and 2 is passed as the actual parameter value. MyFunc may return anything, which is then assigned to x.

Daniel Daranas
MyFunc may return anything, which is then assigned to x., Okay.
Newb
Thanks for the clarification Daniel DaranasMyFunc is invoked and 2 is passed as the actual parameter value.
Newb
+3  A: 

No, quite the contrary, you call the function MyFunc over the value 2 and the result is assigned to x

for example

int MyFunc( int number ) {
  return number + 1;
}
int x = MyFunc(2);
int y = MyFunc(1);

x will be 3 (x+1) and y will be 2

With a different function, the returned value will be different, of course

int MyFunc2( int number ) {
  return number - 1;
}
int x = MyFunc2(2);
int y = MyFunc2(1);

x will be 1 and y will be 0

The main point is, you've got to declare the function and decide what it returns according to its params.

Khelben
Even after you declare the function, the return value if any, will be what get's assigned to the variable as illustrated by you lovely examples, where int x will be 1 and y will be 0 for that last half of your examples, correct?
Newb
Interestingly, I had to resort to the algebraic recall of the number line for those nice examples.
Newb
Thanks guys for the clarification, they should give points here for fastest correct response, i have been on forums where you response might be a week or never-Stack Overflow rocks.
Newb