Function that swaps arguements - is this right?

Discussion in 'C++' started by Ook, Oct 8, 2005.

  1. Ook

    Ook Guest

    I need a function that swaps arguements. This is my function. It works,
    after calling swapArgs aa now has 321, and bb has 123. My question - did I
    do it right? Just because it works doesn't mean I didn't make some
    fundamental mistake somewhere.

    void swapArgs( int &parm1, int &parm2 );

    void swapArgs( int &parm1, int &parm2 )
    {
    int zoot;
    zoot = parm1;
    parm1 = parm2;
    parm2 = zoot;
    }

    // Call it from main()
    main()
    {
    int aa = 123;
    int bb = 321;
    swapArgs( aa, bb );
    }
    Ook, Oct 8, 2005
    #1
    1. Advertising

  2. Ook wrote:

    > I need a function that swaps arguements. This is my function. It works,
    > after calling swapArgs aa now has 321, and bb has 123. My question - did I
    > do it right? Just because it works doesn't mean I didn't make some
    > fundamental mistake somewhere.


    That's fine, but there's an even better way: use the std::swap function
    from the <algorithm> header.

    #include <algorithm>

    int main(){
    int aa = 123;
    int bb = 321;
    std::swap(aa, bb);
    }

    Jacques.
    Jacques Labuschagne, Oct 8, 2005
    #2
    1. Advertising

  3. Ook

    Ook Guest

    >
    > That's fine, but there's an even better way: use the std::swap function
    > from the <algorithm> header.
    >
    > #include <algorithm>
    >
    > int main(){
    > int aa = 123;
    > int bb = 321;
    > std::swap(aa, bb);
    > }
    >
    > Jacques.


    Didn't know you could do that LOL. Why do you precede swap(.. with std::?
    Ook, Oct 9, 2005
    #3
  4. Ook wrote:

    > Didn't know you could do that LOL. Why do you precede swap(.. with std::?
    >
    >


    The functions and objects in the standard library are declared in the
    "std" namespace. To use them you must either do something like

    using std::swap;
    ...
    swap(aa, bb);

    Or fully qualify the name when you use it, like

    std::swap(aa, bb);

    The same thing goes for things like strings and vectors, cin and cout.

    Jacques.
    Jacques Labuschagne, Oct 9, 2005
    #4
  5. Ook

    ben Guest


    > Didn't know you could do that LOL. Why do you precede swap(.. with std::?
    >
    >


    Because if you happen to name your original function as "swap", you can
    spare yourself for a name conflict. Generally speaking, you should also
    put your stuff in your namespace. It's a good practice so stick to it.

    Ben
    ben, Oct 9, 2005
    #5
  6. Ook

    Sandeep Guest

    And since it is working, there must be something right there too !!

    I do not understand why do you think there might be something wrong
    with the code above. As far as I can see it you did it right. You
    passed references from the main to the function swap and swapped the
    data, which is perfectly legal and correct way to swap.
    Sandeep, Oct 9, 2005
    #6
  7. Ook

    Ook Guest

    > And since it is working, there must be something right there too !!
    >
    > I do not understand why do you think there might be something wrong
    > with the code above. As far as I can see it you did it right. You
    > passed references from the main to the function swap and swapped the
    > data, which is perfectly legal and correct way to swap.


    It's because I was not 100% certain what I was doing. As I understand I
    passed the references, so what I really did was change the two variables to
    point at each others memory, rather then just swapping their values. If I
    wanted to actually swap values, how would I do that? Once I pass the
    reference to the varible to my function, how do I get to the variable's
    value?
    Ook, Oct 9, 2005
    #7
  8. Ook

    Kai-Uwe Bux Guest

    "Ook" <Don't send me any freakin' spam> wrote:

    >> And since it is working, there must be something right there too !!
    >>
    >> I do not understand why do you think there might be something wrong
    >> with the code above. As far as I can see it you did it right. You
    >> passed references from the main to the function swap and swapped the
    >> data, which is perfectly legal and correct way to swap.

    >
    > It's because I was not 100% certain what I was doing. As I understand I
    > passed the references, so what I really did was change the two variables
    > to point at each others memory, rather then just swapping their values.


    No, you actually swapped the values. There is no way to make a variable
    point to another object unless that variable *is* a pointer in the first
    place.


    > If I wanted to actually swap values, how would I do that? Once I pass the
    > reference to the varible to my function, how do I get to the variable's
    > value?


    By means of the reference. Think of a reference as an alias, as just another
    name for the object passed, a name by which that object is called within
    the function. Do not think of a reference as a pointer.

    If you something by value, actually a copy of that value is created and the
    function operates on the copy. When the function returns the local copy is
    destroyed and any changes you made are lost. [I am slightly lying here to
    simplifying things a little bit.]


    Best

    Kai-Uwe Bux
    Kai-Uwe Bux, Oct 9, 2005
    #8
  9. Ook

    Ian Guest

    Sandeep wrote:
    > And since it is working, there must be something right there too !!
    >
    > I do not understand why do you think there might be something wrong
    > with the code above. As far as I can see it you did it right. You
    > passed references from the main to the function swap and swapped the
    > data, which is perfectly legal and correct way to swap.
    >

    Which code above? Please quote, it's very frustrating having to look
    back through messages.

    Ian
    Ian, Oct 9, 2005
    #9
  10. Ook

    Sandeep Guest

    >>It's because I was not 100% certain what I was doing. As I understand I
    >>passed the references, so what I really did was change the two variables to
    >>point at each others memory, rather then just swapping their values. If I
    >>wanted to actually swap values, how would I do that? Once I pass the
    >>reference to the varible to my function, how do I get to the variable's >>value?


    A reference means an 'Alias' to a variable. A compiler tyically parses
    for all Identifiers and associates an address to it in the symbol
    table.
    Eg:
    int i = 100;
    int j = 200;

    Symbol Table
    i:0X0001
    j:0X0004

    If you have a reference 'k' to a variable 'i' , the symbol table will
    become
    i:0X0001
    j:0X0004
    k:0X0001

    This what i mean when i say ( and others) in this post , 'Alias'.
    Sandeep, Oct 9, 2005
    #10
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Bender MX

    Help with Image Swaps

    Bender MX, Nov 14, 2003, in forum: HTML
    Replies:
    4
    Views:
    399
    Bender MX
    Nov 15, 2003
  2. tschulz

    emacs swaps windows

    tschulz, Nov 9, 2004, in forum: Python
    Replies:
    1
    Views:
    293
    Harry George
    Nov 10, 2004
  3. Aaron Graham
    Replies:
    20
    Views:
    1,726
    Aaron Graham
    Feb 2, 2006
  4. Replies:
    8
    Views:
    357
  5. BootNic

    Re: CS4 image swaps problem

    BootNic, Nov 9, 2013, in forum: HTML
    Replies:
    5
    Views:
    194
    dorayme
    Nov 11, 2013
Loading...

Share This Page