473,805 Members | 2,034 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Function that swaps arguements - is this right?

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 );
}
Oct 8 '05 #1
9 1755
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.
Oct 8 '05 #2
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::?
Oct 8 '05 #3
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.
Oct 8 '05 #4
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
Oct 9 '05 #5
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.

Oct 9 '05 #6
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?
Oct 9 '05 #7
"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
Oct 9 '05 #8
Ian
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
Oct 9 '05 #9
>>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'.

Oct 9 '05 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
3470
by: Andrew Koenig | last post by:
I am looking for the most effective way to exchange v and v, and expected to find something like v.swap(i, j) but it doesn't seem to exist. Is there anything better than v, v = v, v (which I don't like particularly because it evaluates each of v and v
3
1755
by: junk | last post by:
Hi, Given the following function:- void foo (char *fmt, ...) { } I know how to use va_start, va_end etc to process the parameters but is there an easy way of passing the parameter list onto another function that also has variable length arguements.
8
1872
by: Dawn Minnis | last post by:
Hey guys If I have a program (see codeSnippet1) that I compile to be called test.o Then run it as test.o n n 2 3 4 I want the code to be able to strip out the two characters at the start (always going to be 2) and store them as characters. But I can't seem to get it to work because it is a pointer to a vector of characters. However, if I only run with integer arguements and use codeSnippet2 it works fine and they convert nicely to...
11
11539
by: yangsuli | last post by:
i want to creat a link when somebody click the link the php script calls a function,then display itself :) i have tried <a href=<? funtion(); echo=$_server ?>text</a> but it will call the function whether i click the link then i tried this (using forms)
5
6355
by: rf | last post by:
As I understand it, it is legal to have a template member function of a non-template class, as in the following example: class XYZ { public: template <class Tvoid fn(T* ptr) { T val = *ptr; cout << val << endl;
20
1817
by: sam_cit | last post by:
Hi Everyone, I have the following code, int main() { int p = {10,20,30,40,50}; int *i = &p; printf("before : %p\n",(void*)i); printf("1 %d %d\n",*++i,*i++);
2
1824
by: Nike | last post by:
I have a small question w.r.t usage of default arguements in template.. I shall try to elaborate this with an example.. let's say I have some template function , where EntryType is the input for the template fn 1.. and another type where EntryType and lass P1 are both inputs.. case1 (1)template<class EntryType_> i.e void A<EntryType_>::createObject(EntryType::inputIdType inpId_ )..
28
2861
by: Jess | last post by:
Hello, It is said that if I implement a "swap" member function, then it should never throw any exception. However, if I implement "swap" non- member function, then the restriction doesn't apply. Can somebody tell me why? Thanks, Jess
8
2356
by: cheetahclaws | last post by:
I am trying to create a function that reverses the halves of an integer array. For example. if the array contains 1,2,3,4 --- after it has gone to the function I constructed, the array will look like 3,4,1,2. In the case the array contains an odd amount of elements e.g. 1,2,3,4,5, it will look like 4,5,3,1,2. I have created such an array as followed: ================================== for(i=0;i<=((size/2)-1);i++) {
0
9716
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9596
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10356
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
9179
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7644
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5536
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5676
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3839
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
3006
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.