Function that swaps arguements - is this right?

O

Ook

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 );
}
 
J

Jacques Labuschagne

Ook said:
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.
 
O

Ook

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::?
 
J

Jacques Labuschagne

Ook said:
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.
 
B

ben

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
 
S

Sandeep

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.
 
O

Ook

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?
 
K

Kai-Uwe Bux

Ook said:
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
 
I

Ian

Sandeep said:
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
 
S

Sandeep

It's because I was not 100% certain what I was doing. As I understand I
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'.
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,767
Messages
2,569,572
Members
45,045
Latest member
DRCM

Latest Threads

Top