A function question

This is a discussion on A function question within the C++ Programming forums, part of the General Programming Boards category; Hello guys, I am utterly confused about functions. Neither, my professor or the text book did a good job of ...

  1. #1
    Registered User axon's Avatar
    Join Date
    Feb 2003
    Posts
    2,572

    A function question

    Hello guys, I am utterly confused about functions. Neither, my professor or the text book did a good job of explaining the exact structure and happenings of function paramaters. On a recent test I had a pece of code that looked like the one I posted below this. I got the question wrong and when I compiled it at home the value of the final variables was a=7, b=1, c=8.

    Could someone please explain to me how this works, and maybe a method of tracing the variables?

    thanks a bunch,

    axon

    Code:
    #include <iostream>
    using namespace std;
    
    int a;
    int b;
    int c;
    
    
    void c1(int b, int a)
    {
    	a+=1;
    	b+=1;
    	c+=1;
    }
    
    void c2(int b, int& a)
    {
    	a+=3;
    	b+=3;
    	c+=3;
    }
    
    void c3(int c, int& b)
    {
    	a+=3;
    	b+=3;
    	c+=3;
    }
    
    int main()
    {
    	a=b=c=1;
    
    	c1(a, b);
    	c2(b, c);
    	c3(c, a);
    
    	cout << "the value of the variables is: ";
    	cout << a << " and " << b << " and " << c <<endl;
    
    	return 0;
    
    }

  2. #2
    Confused Magos's Avatar
    Join Date
    Sep 2001
    Location
    Sweden
    Posts
    3,145
    void Func(int Var)

    Here, a copy of the parameter is created, thus not changing the original variable that was passed to it.

    void Func(int& Var)

    Here, a reference to the variable is passed, so any changes to this variable will change the one that was passed.
    MagosX.com

    Give a man a fish and you feed him for a day.
    Teach a man to fish and you feed him for a lifetime.

  3. #3
    Registered User kiss_psycho's Avatar
    Join Date
    Feb 2003
    Posts
    49
    First of all, u are using global variables and the value of a global variable altered in any function is reflected back in all other functions. However, the local variables a,b, and c are also referred.

    Try tracing the program with F7 if you are using a TC++ or Borland C++ compiler and add the watches for a,b,c,::a,::b,::c.

    You will notice the problem.
    Definition of Programmer : A red-eyed mumbling mammal capable of conversing with inanimate objects.

    Happy Hunting...
    The Root

  4. #4
    In your face... ha ha ha Liger86's Avatar
    Join Date
    Dec 2002
    Location
    Motorcity Capital
    Posts
    321
    Random Question:

    What does "namespacestd;" do?
    From Ukraine with love!

    Internationally known widely respected

    - Digitally yourz -

  5. #5
    Registered User axon's Avatar
    Join Date
    Feb 2003
    Posts
    2,572
    Oh, I think I'm starting to understand now. But the confusing part is that lets say the calling funtion says c1(a, c), and the function parameters are c1(int& b, int& a)...does the value of c stay the same, or does it change?

    As for the namespace std question: namespace std is a collection of name definitions.

    axon

  6. #6
    Registered User kiss_psycho's Avatar
    Join Date
    Feb 2003
    Posts
    49
    The namespace keyword allows you to partition the global namespace consisting of huge numbers of variable names, class names and function names into declarative regions, thus allowing a name used in one library to be used by another library. This reduces collisions and enhances scope definition. in the std namespace, the C++ library is defined.

    if the using namespace statement was not used , the members of a namespace would have to be referred everytime by the scope resolution op. however specifying the namespace name to be accessed by the using statement

    Code:
    e.g using namespace std
    brings all or specific members within the specified namesopace into the current namespace so that they can be used without qualification.


    For more details, consult a book...
    Definition of Programmer : A red-eyed mumbling mammal capable of conversing with inanimate objects.

    Happy Hunting...
    The Root

  7. #7
    Registered User kiss_psycho's Avatar
    Join Date
    Feb 2003
    Posts
    49
    The variable, c in the calling module is referenced as variable,a in the called module, and if a changes in the called module, c will change.
    Definition of Programmer : A red-eyed mumbling mammal capable of conversing with inanimate objects.

    Happy Hunting...
    The Root

  8. #8
    Hardware Engineer
    Join Date
    Sep 2001
    Posts
    1,398

    REFERENCES

    does the value of c stay the same, or does it change?
    It looks like you're supposed to be learning references (i.e. passing by reference) When you use the & symbol, you are using a "reference" .

    The use of references and pointers are the ways to make a function affect more than one variable. YES, you CAN change the original variable. The "normal" way of passing by value creates a new local variable (sometimes with the same name) and the function may "return" only ONE value.

    A common exercise demonstrating the use of references (or pointers) is to create a function that "swaps" the values of A & B.

    Note that the variable names inside the function may be different than the variable names outside the function. But the reference still "refers" to the original variable.

    This stuff can seem really confusing at first, but it will become almost second-nature soon!

    [EDIT]
    If your book does not explain this stuff clearly, and if you have an "extra" $30.00 or so, I recommend "Teach Yourself C++ In 21 Days" by Jesse Liberty. He explains the basics very clearly. (I don't remember if he addresses "using namespase std")
    Last edited by DougDbug; 02-21-2003 at 06:32 PM.

  9. #9
    Registered User axon's Avatar
    Join Date
    Feb 2003
    Posts
    2,572

    Re: REFERENCES

    This stuff can seem really confusing at first, but it will become almost second-nature soon! [/B]
    I hope so!!

    axon

  10. #10
    CS Author and Instructor
    Join Date
    Sep 2002
    Posts
    511

    Wink

    Learning how to pass functions by value and reference and tracing are important. Catch on to it quickly- you will find college/univ instructors will expect you to know it. A lot of good test question come from this concept as well.
    Mr. C: Author and Instructor

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. CreateThread() function question
    By chiefmonkey in forum C++ Programming
    Replies: 5
    Last Post: 05-15-2009, 08:52 AM
  2. Undefined Reference Compiling Error
    By AlakaAlaki in forum C++ Programming
    Replies: 1
    Last Post: 06-27-2008, 12:45 PM
  3. Bisection Method function value at root incorrect
    By mr_glass in forum C Programming
    Replies: 3
    Last Post: 11-10-2005, 09:10 AM
  4. Change this program so it uses function??
    By stormfront in forum C Programming
    Replies: 8
    Last Post: 11-01-2005, 08:55 AM
  5. Replies: 3
    Last Post: 03-04-2005, 02:46 PM

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