just a copy ?

This is a discussion on just a copy ? within the C++ Programming forums, part of the General Programming Boards category; Hi, guys~ I found the code below when reading some documents. and it says: the Number referred to in AddFive ...

  1. #1
    flashing vampire black's Avatar
    Join Date
    May 2002
    Posts
    563

    Question just a copy ?

    Hi, guys~

    I found the code below when reading some documents.
    and it says: the Number referred to in AddFive is a copy of the variable nMyNumber passed to the function, not the variable itself.
    I am at a loss this moment, any ideas to me plz ?
    #include <stdio.h>
    void AddFive(int Number)
    {
    Number = Number + 5;
    }

    void main()
    {
    int nMyNumber = 18;

    printf("My original number is %d\n", nMyNumber);
    AddFive(nMyNumber);
    printf("My new number is %d\n", nMyNumber);
    }
    Never end on learning~

  2. #2
    Registered User Azuth's Avatar
    Join Date
    Feb 2002
    Posts
    236
    When you call your function 'AddFive' it expects a number, in the case below the number you're giving it is 18 because you're sending it a copy of 'nMyNumber' which has been assigned a value of 18.

    The function would normally then do something with the value you send it, then return a value to where it was called from. It will not change the original variable in the code you have.

    In the code you have the function AddFive doesn't actually return anything (a. because it is declared as void, and b. because it has no return statement), so your Original number and new number will still show as 18. In order to run the function and see the logical result you would want something like...

    Code:
    #include <stdio.h>
    int AddFive(int Number)
    {
    Number = Number + 5;
    return (Number);
    }
    
    int main()
    {
    int nMyNumber = 18;
    int nNewNumber;
    
    printf("My original number is %d\n", nMyNumber);
    nNewNumber = AddFive(nMyNumber);
    printf("My new number is %d\n", nNewNumber);
    return 0;
    }
    I know that was clear as mud, I really hope someone comes along and clears up the confusion I've just created for you.

  3. #3
    flashing vampire black's Avatar
    Join Date
    May 2002
    Posts
    563

    Arrow

    thanx ur explaination~

    my confusion is why the value not be changed ? and what about that copied one ?

    any idea will be helpful~
    Never end on learning~

  4. #4
    Nerd Xmevs's Avatar
    Join Date
    Apr 2002
    Posts
    146
    When you call AddFive(nMyNumber), nMyNumber is only read from. But in the AddFive function, the statement Number is written to, thus making Number = nMyNumber.

  5. #5
    Registered User Azuth's Avatar
    Join Date
    Feb 2002
    Posts
    236
    If you want the number to be changed by your function, you have to assign to it what ever value the function returns.

    You could declare your variable globally, then change it directly with your function if you wanted to, along the following lines...
    Code:
    #include <stdio.h>
    
    int nMyNumber = 18; //Declared outside of all functions
    
    void AddFive(void)
    {
    nMyNumber = nMyNumber + 5;
    }
    
    int main()
    {
    //int nNewNumber;
    
    printf("My original number is %d\n", nMyNumber);
    AddFive();
    printf("My new number is %d\n", nMyNumber);
    
    return 0;
    }
    In the above example we are working directly with the variable, rather than using a copy of it and returning a value, which on this level almost defeats the purpose of the function entirely.


    A more streamlined version of the code I posted previously (1 variable removed) could look like this

    Code:
    #include <stdio.h>
    int AddFive(int Number)
    {
    Number = Number + 5;
    return (Number);
    }
    
    int main()
    {
    int nMyNumber = 18;
    //int nNewNumber;
    
    printf("My original number is %d\n", nMyNumber);
    nMyNumber = AddFive(nMyNumber);
    printf("My new number is %d\n", nMyNumber);
    
    return 0;
    }
    This is still only working with a copy of nMyNumber, the line
    "nMyNumber = AddFive(nMyNumber);"
    works happily because of the order of operations. In terms of workflow at that point the following happens
    [list=1][*] Addfive(nMyNumber) // Send the value of nMyNumber to the AddFive function[*] That value is now stored in the variable Number inside the Addfive function[*] Add 5 to Number[*] Return our new Number to the calling function, giving us nMyNumber=23[/list=1]

    I really have tried to be clear, but I am only a beginner. Though I'd suggest if this is still really unclear you go back to reading the book/website, and better yet, look for one that doesn't use "void main" in their examples.
    Last edited by Azuth; 05-14-2002 at 07:37 AM.

  6. #6
    Registered User
    Join Date
    Apr 2002
    Posts
    139
    Tis very simple it doesn't change nMyNumber because when it is passed to the function AddFive() it is passing by value (which allows you to use the value but any changes are discarded after the function ends) If you want the number to be able to be changed in the function you must pass by reference (using the & placed on the variable) look below for the modified code.

    Code:
    #include <stdio.h> 
    void AddFive(int& Number)   //THIS is the only modification
    { 
      Number = Number + 5; 
    } 
    
    void main() 
    { 
      int nMyNumber = 18; 
    
      printf("My original number is %d\n", nMyNumber); 
      AddFive(nMyNumber);  
      printf("My new number is %d\n", nMyNumber); 
    }
    nMyNumber would finish with being 23

    cheers

  7. #7
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,788
    Not to beat a dead horse but you can also pass the value by pointer and have the same thing happen as in the above post:
    Code:
    #include <stdio.h> 
    void AddFive(int* Number)
    { 
        *Number = *Number + 5; 
    } 
    
    void main() 
    { 
      int nMyNumber = 18; 
    
      printf("My original number is %d\n", nMyNumber); 
      AddFive(&nMyNumber);  
      printf("My new number is %d\n", nMyNumber); 
    }
    At the end of this the nMyNumber variable will have the value 23.
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. copy = concatenate ?
    By Arruba in forum C Programming
    Replies: 3
    Last Post: 11-03-2006, 03:54 PM
  2. calling copy constructor from template
    By Ancient Dragon in forum C++ Programming
    Replies: 3
    Last Post: 09-28-2005, 01:54 PM
  3. dynamic memory alloccation & returning objects
    By haditya in forum C++ Programming
    Replies: 8
    Last Post: 04-21-2005, 11:55 PM
  4. Copy constructors and operator=()
    By filler_bunny in forum C++ Programming
    Replies: 13
    Last Post: 08-25-2003, 07:43 AM
  5. Copy Constructor Help
    By Jubba in forum C++ Programming
    Replies: 2
    Last Post: 11-07-2001, 10:15 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21