Thread: int&, passing of arguments by reference, etc

  1. #1
    Registered User
    Join Date
    Mar 2011
    Posts
    254

    int&, passing of arguments by reference, etc

    Hi

    Today the instructor was trying to teach us how the following code works. To be truthful I couldn't understand anything.

    First thing he changed in the today's program was that he made declared the user defined function within the int main though in the past he told us to declare it before entering int main. Please have a look on CODE 2 which you would understand what I'm saying. I don't understand the reason for this.

    1: I copied the following CODE 1 onto my flash drive. You can see that the declaration for the user defined function has been made inside the int main. What's the reason for this change?

    2: What kind of data type is this "int&"?

    3: Could you please tell me in simple words what that passing of arguments by reference means? What's the advantage of this practice.

    Please keep your replies as simple as possible. Thank you very much.

    CODE 1:
    Code:
    // orders two arguments passed by reference
    #include <iostream>
    using namespace std;
    
    int main()
       {
       void order(int&, int&);          //prototype
    
       int n1=99, n2=11;                //this pair not ordered
       int n3=22, n4=88;                //this pair ordered
    
       order(n1, n2);                   //order each pair of numbers
       order(n3, n4);
    
       cout << "n1=" << n1 << endl;     //print out all numbers
       cout << "n2=" << n2 << endl;
       cout << "n3=" << n3 << endl;
       cout << "n4=" << n4 << endl;
       return 0;
       }
    //--------------------------------------------------------------
    void order(int& numb1, int& numb2)  //orders two numbers
       {
       if(numb1 > numb2)                //if 1st larger than 2nd,
          {
          int temp = numb1;             //swap them
          numb1 = numb2;
          numb2 = temp;
          }
       }
    CODE 2:
    Code:
    // calculating area of a circle using user-defined function
    
    #include <iostream>
    #include <cstdlib>
    
    using namespace std;
    
    float area(float dummy);
    
    int main()
    
    {
        float r; float a;
    
    	cout << "enter radius: "; cin >> r;
    
    	a = area(r);
    
    	cout << a;
    
    	system("pause");
    
    	return 0;
    }
    
    //------------------------------------
    // area(int), function definition
    
            float area(float dummy)
    
    		{
    			float Area;
    
    			Area = 3.1416*(dummy*dummy);
    
    			return Area;
    
    		}
    //--------------------------------------
    I'm an outright beginner. Using Win XP Pro and Code::Blocks. Be nice to me, please.

  2. #2
    [](){}(); manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    *nullptr
    Posts
    2,657
    What's the reason for this change?
    Maybe, to confuse you..! AFAIK ..no other reason applies here..

    What kind of data type is this "int&"?
    It just says that you are passing an integer by reference..

    passing of arguments by reference
    If you understand pointers.....passing by reference is a less messy way of passing by address..
    If you don't...say.....you pass an integer by reference and the function changes the value of the integer..but does not return it...When the called function exits; the value of the original variable you passed..reflects the changes to the variable you worked with...inside the function.

  3. #3
    Registered User
    Join Date
    Mar 2011
    Posts
    254
    Quote Originally Posted by manasij7479 View Post
    If you don't...say.....you pass an integer by reference and the function changes the value of the integer..but does not return it...When the called function exits; the value of the original variable you passed..reflects the changes to the variable you worked with...inside the function.
    Thank you.

    I don't know anything about the pointers. Okay. So, the int main passes the value of a variable by reference (perhaps, by sending the information where it is stored in the memory) to a called function. The called function acts on those variables passed to it through reference (i.e. by sending their memory addresses) by the int main. The called function can affect the values (i.e. numerical values etc) without really changing the memory addresses so that the variables are still accessed by the int main but perhaps it (int main) will see new values for the variables.

    Is what I'm saying correct? Please tell me. Thanks
    I'm an outright beginner. Using Win XP Pro and Code::Blocks. Be nice to me, please.

  4. #4
    [](){}(); manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    *nullptr
    Posts
    2,657
    Yes.

  5. #5
    Registered User
    Join Date
    Mar 2011
    Posts
    254
    Thanks a lot, Manasij. So, it's just like swapping the contents of the drawers without really affecting the configuration (or, arrangement) of the drawers.
    I'm an outright beginner. Using Win XP Pro and Code::Blocks. Be nice to me, please.

  6. #6
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    One thing about your instructor's code is bad. You can find info here: http://cpwiki.sourceforge.net/do_not...arameter_names
    (Feel free to point it out to your lecturer!)

    Now, references... consider this:

    Code:
    void foo(int & b) { b = 10; }
    void bar(int b) { b = 10; }
    
    int main()
    {
        int a = 20;
        std::cout << a << std::endl;
        bar(a);
        std::cout << a << std::endl;
        foo(a);
        std::cout << a << std::endl;
    }
    In bar, the value of a will be copied, and so bar will work with its local copy of the contents of a (since the contents was copied over to bar's b). So no changes are reflected in main.
    foo is a different store. The "int&" says it's a reference. What that means is that "b" in foo is another name for a in main. Thus, foo's b is main's a. Just with a different name. Hence, when changing it in foo, main's a will also change.
    That is what a reference is. An alias.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  7. #7
    Registered User
    Join Date
    Mar 2011
    Posts
    254
    Thank you, Elysia, for pointing this out.

    Best regards
    Jackson
    I'm an outright beginner. Using Win XP Pro and Code::Blocks. Be nice to me, please.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Passing arguments by value
    By nedim in forum C++ Programming
    Replies: 5
    Last Post: 10-29-2009, 01:24 AM
  2. Managed C++, passing arguments by reference
    By jimzy in forum C++ Programming
    Replies: 8
    Last Post: 11-02-2007, 01:03 PM
  3. passing arguments using "Command Line Arguments"
    By Madshan in forum C++ Programming
    Replies: 1
    Last Post: 04-19-2006, 03:46 PM
  4. const reference formal arguments
    By Chaplin27 in forum C++ Programming
    Replies: 8
    Last Post: 10-12-2004, 10:12 PM
  5. passing by address vs passing by reference
    By lambs4 in forum C++ Programming
    Replies: 16
    Last Post: 01-09-2003, 01:25 AM