0

I have a main function that declares a double. It then passes it to void Display, which displays the double. The main then passes the double (by value) to a function void Pointers which calls the display function and then changes the value of the double. I then run the display function again from within the main, again passing it the double. But the double has changed to be the new value (that we changed it to in Pointers()) and i don't understand why.

As i understand it, when we pass by value, we are handing the function a copy to work with, and it can do whatever it wants to that copy, but it won't change the original version. If we pass by reference, then we are simply handing the address to the original data.

Since we passed by value, shouldn't it have changed it's own copy and left the original alone?

Code below for reference.

#include <iostream>
#include <cctype>
#include <string>
#include <vector>
#include <iomanip>
#include <windows.h> // for a bigger window
using namespace std;

void Heading()
{
    system("cls");
    cout << "David Fritz, CO127, 04/9/2013 Assignment 14 (Week 12)";
    cout << endl;
    cout << endl;
}
void Display(string source, double &value, double* &pointer)
{
    cout << "Source: " << source << endl;
    cout << "Double Address: " << &value << endl;
    cout << "Double Value: " << value << endl;
    cout << "Pointer Address: " << &pointer << endl;
    cout << "Pointer Value: " << pointer << endl;
    cout << "Dereferenced Pointer Value: " << *pointer << endl << endl;
}

void Pointer(double value, double* &pointer)
{
    string source = "Method";
    Display(source, value, pointer);
    value = 7;
    *pointer = value;
}

int main()
//2.    In the main
//       •    Call the heading function
//       •    Create a double and assigns it a value
//       •    Create a pointer that points to the double
{
    MoveWindow(GetConsoleWindow(), 100, 0, 700, 800, TRUE); //moves and resizes window
    Heading();
    string source = "Main";
    double aValue = 4;
    double* pointer = &aValue;
    Display(source, aValue, pointer);
    //system("pause");
    Pointer(aValue, pointer);
    //system("pause");
    Display(source, aValue, pointer);
    cout << endl;
    cout << "Q: If you pass the double by value to the function, why does the value of     the" << endl 
         << "double change in the main after you return from the function?" << endl     << endl;
    cout << "A: I dont know. We passed it its own copy, but it didn't care. I'll figure     it out"
}

This is the assignment (which i post because step 5 specifically tells me HOW to pass the double and pointer)

Write a program that creates
1. Create a heading function that displays your name, class number, and date.
2. In the main
• Call the heading function
• Create a double and assigns it a value
• Create a pointer that points to the double
3. Write a Display function that takes a string, a pointer, and a double and uses cout statements to display text describing what you are displaying and then display each in turn:
The location it is being displayed from (called from main or method?)
The double address
The double value
The pointer address
The pointer value (pointers hold addresses, this should be the address of the double)
The dereferenced pointer value (what the pointer is pointing to, the double value)
4. Call the display function from main to display the attributes of the pointer and double.
5. Create a function with a void return type that takes a pointer and a double (pass double by value) from the main. Then display the values of the pointer and double as you did in the main.
• Change the value of the double and then assign the point the value of the double (*pointer = double)
• If ptr is the point name and x is the name for the double then *ptr = x;
6. Back in the main re-display the attributes of the pointer and the double
7. Add a pause.
8. Answer this one question:
• Call the heading function
• Display the following question and its answer.
“If you pass the double by value to the function, why does the value of the double change in the main after you return from the function?”

David
  • 43
  • 7

2 Answers2

0

Pointer() does get its own copy (double value), and changing this doesn't change the value in the main function.

However, you are also passing Pointer() an address (double *pointer), and setting the thing at that address to the value (7) as well, with "*pointer = value;". Because you gave Pointer() the address of aValue (&aValue), it changes in main().

EDIT: So you are right about passing by value not changing the original version, but in this case you have passed both by value and by 'reference' (well, a pointer, which is sort of like a reference), so it changed.

Also some other things:

a) Your indentation is a bit confused (although that could have just been from copying and pasting it in) b) Passing the pointer by reference (double * &pointer), is unnecessary unless you want to change the value of 'pointer', rather than the value at the address it points to. Probably 'double *pointer' is better.

Cheers

chrisd
  • 21
  • 3
0

Check this version. It's complete and a lot better.

            #include <iostream>
            #include <string>
            #include <iomanip>
            using namespace std;

            void Display(string source, double &value, double* pointer)
            {
                cout << source << endl;
                cout << left << setw(40) << "Original Double Address: "             << &value   << endl;
                cout << left << setw(40) << "Original Double Value: "               << value    << endl;
                cout << left << setw(40) << "Original Pointer Address: "            << &pointer << endl;
                cout << left << setw(40) << "Original Pointer Value: "              << pointer  << endl;
                cout << left << setw(40) << "Original Dereferenced Pointer Value: " << *pointer << endl << endl;
            }

            void Pointer(double value, double *pointer)
            {
                string source = "Pointer Function";
                value = 1842;
                *pointer = value;
                Display(source, value, pointer);
            }

            void main()
            {
                cout << "Student Name, CO127, Assignment 14.1" << endl << endl;

                string source = "Main";
                double aValue = 921;
                double* pointer = &aValue;

                // Initial Values Displayed
                Display(source, aValue, pointer);

                // Values after passing to function
                Pointer(aValue, pointer);

                // Values from main after being modified in function.
                Display(source, aValue, pointer);

                cout << endl;
                cout << "Answer " << endl;
            }