C++ Question


  1. Posts : 30
    Window 7 Ultimate x64
       #1

    C++ Question


    I am working on my homework and need help figuring out a tough question.

    2) When you call a function, as in the area and perimeter of a past assignment, you include the values to be sent to the function (the arguments) in parentheses
    and the function must have been defined with parameters of an equivalent type, that will be used to receive those values

    If the function is called using variables, as in cout << area(length, width) for instance, what is passed to the parameters is a copy of the values in those variables.

    This is called call-by-value. In that case, the receiving parameters in the function are initialized to the contents of the two argument variables (length and width, in our example).

    Based on this, given the following function:

    void f (int i )

    {

    i = 57;

    }

    assuming it is called like this:

    f(value)

    where value is a variable of type int, defined as int value = 9; what would be the result of printing variable value after the call? Explain
      My Computer


  2. Posts : 1,814
    XP / Win7 x64 Pro
       #2

    I don't believe this is the right section for this type of question, nor am I certain you should be getting help on forums for your homework, but I feel comfortable enough asking the following questions in helping you figure this out:

    - What is actually being passed to the f() function?
    - Is it the variable itself that is being passed or just the value it contains?
    - If it's just the value it contains, how does the f() function impact the actual value of the variable value?

    I would hope there is some sort of help system set up at your school or in your class for when you need help like this. I would suggest you look into that instead.
      My Computer


  3. Posts : 30
    Window 7 Ultimate x64
    Thread Starter
       #3

    I dont think there is a section on here for programming or other random computer related topics. I am taking an online class with a professor who doesn't reply very fast to my emails, and there is a small discussion board for the class, but its pretty much tumble weeds, no help there.

    #include <iostream>
    using namespace std;
    void f (int);
    int main()
    {
    int value = 9;
    cout << "Value in Main: " << value << endl;
    f(value);
    cout << "Value in Main: " << value << endl;
    return 0;
    }
    void f (int i)
    {
    i = 57;
    cout << "Value in Function: " << i << endl;
    }
    Returns
    Value in Main: 9
    Value in Function: 57
    Value in Main: 9

    Answer 2) There are basically two ways to pass a value to a function pass by value and pass by reference.
    When you pass by value, the compiler creates a temporary variable to hold the passed value within the scope of the function.
    Any changes made to the value by the function are temporary to the scope of the function;
    the changes are not seen by the scope of the caller when the function returns.
    When you pass by reference, the compiler passes the memory location of the variable rather than a temporary copy.
    This means that any changes made to the variable by the function are also seen in the scope of the caller when the function returns.
    In this case, changes made to the parameter have no effect on the argument.
    I think if you printed the value of value after the call (and not in the function itself) it would still be 9,
    because the value only changes in the scope of the function. The scope of variables declared within a function
    or any other inner block is only their own function or their own block and cannot be used outside of them.
      My Computer


  4. Posts : 1,814
    XP / Win7 x64 Pro
       #4

    Right. You're only passing the value of the variable value, not the reference to it. Doing anything to that value within the function doesn't change the value variable at all.
      My Computer


 

  Related Discussions
Our Sites
Site Links
About Us
Windows 7 Forums is an independent web site and has not been authorized, sponsored, or otherwise approved by Microsoft Corporation. "Windows 7" and related materials are trademarks of Microsoft Corp.

© Designer Media Ltd
All times are GMT -5. The time now is 01:12.
Find Us