-
Junior Member
Function Swap
How do you write a function swap for C++?
-
Junior Member
Re: Function Swap
Hello, Either if it is in C or C++ you can use the best logic as given below:
if a and b are the values (integer/float/....), then inside function if you use,
a=a+b;
b=a-b;
a=a-b;
The values of a and b gets swaped.
The protype for a simple function fname is given below:
void fname(int*,int*);
While calling the function you have to pass the address of the values to be swaped as arguments.
The definition is given below:
fname(int *p,int *q)
{
*p=*p+*q;
*q=*p-*q;
*p=*p-*q;
}
-
Junior Member
Re: Function Swap
void Swap(int &a, int&b)
{
a=a-(b=(a=a+b)-b);
}
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules