Question:

What is the answer to this c++ problem?

by  |  earlier

0 LIKES UnLike

int x =2 ;

void myDouble ( int x) {

x= x + 1;

}

what is the value of x inside the function and when it completes?

 Tags:

   Report

6 ANSWERS


  1. If x is declared in main() and myDouble at top level then when myDouble terminated x would be 2. By default C and C++ copy parameters by value rather than by address, unless the parameter is an array in which case it copies the address.  This means unless int and myDouble are declared in a class (in which case myDouble is a method, not a function) it operates on a copy of x, and when the function ends the copy of x is destroyed with all the changes that the function has made to it.


  2. it could be anything.  that is, any integer x.  nevermind, i hate c++.  Look on some c++ forums for help.  yahoo answers won't have anyone who knows this stuff.  Or you get a book for c++ at borders or something.


  3. First of all, you never call the function. So x would be 2 after the "int x=2" function is called.

    If you actually called your function (let me fix this for you)...

    void myDouble ( int x) {

    x= x + 1;

    }

    int x =2 ;

    myDouble(x);

    The variable x would of course be 2 upon entering the function. After the "x=x+1" call, x would be 3, and upon leaving that function and going back to the main program, x would go back to being 2. This is because the variable was passed as a value from the program, not as a reference. The 'x' variable within the function only has scope within that function- it is a different x than the one in the outer program.

  4. The problem is you never called this function. Just because you declare x=2 above this function, doesn't mean you call it with x=2. For example, you could have something like below:

    int x=2;

    void myDouble( int x) {x = x+1;}

    myDouble(3);

    At the very beginning of the function, x will be 3 (because you called MyDouble and passed it a variable of 3). At the end it will be 4. The only line in the function "x = x+1" will just increment x by 1 before it is over, regardless of what value came in.

    In the example you give, it is somewhat confusing because you never call the function to pass in a value. I assume you mean to do something like the following:

    int x=2;

    void myDouble( int x) { x = x+1;}

    myDouble(2);

    which would make x=2 when it first enters the function myDouble and then have x=3 when it leaves. Moral of the story, you have to call the function before it has any value -- and you never did.  

  5. GARBAGE VALUE

    x inside function is limited within function only and int x=2 wont apply for function.

    so when function gets called function x takes some random garbage value and executes the res of function statements for that value only resulting in again some garbage value...

    hope that helped......

  6. inside the function x = 3

    after the function x = 2

Question Stats

Latest activity: earlier.
This question has 6 answers.

BECOME A GUIDE

Share your knowledge and help people by answering questions.