answersLogoWhite

0


Best Answer

swap (int *a, int *b) {

*a ^= *b;

*b ^= *a;

*a ^= *b;

}

int c = 13;

int d = 27;

swap (&c, &d); /*c is now 27 and d is now 13 */

Note: there is no call-by-reference in C. In C++:

void swap (int &a, int &b)

{

. int tmp;

. tmp = a;

. a = b;

. b = tmp;

}

User Avatar

Wiki User

13y ago
This answer is:
User Avatar
More answers
User Avatar

Wiki User

11y ago

#include <iostream>

using namespace std;

void swap(int& x, int& y)

{

x ^= y ^= x ^= y; // Swap values without using temporary variable.

}

int main()

{

int a=1, b=2;

cout<<"Before:\t";

cout<<"a="<<a<<"\tb="<<b<<endl;

swap(a,b);

cout<<"After:\t";

cout<<"a="<<a<<"\tb="<<b<<endl;

return( 0 );

}

This answer is:
User Avatar

Add your answer:

Earn +20 pts
Q: Swap the value of two variables using call by reference?
Write your answer...
Submit
Still have questions?
magnify glass
imp
Continue Learning about Engineering

Swap two number using pointer?

The only way to swap two values using call by value semantics is to pass pointer variables by value. A pointer is a variable that stores an address. Passing a pointer by value copies the address, the value of the pointer, not the pointer itself. By passing the addresses of the two values to be swapped, you are effectively passing those values by reference. Both C and C++ use pass by value semantics by default, however C++ also has a reference data type to support native pass by reference semantics. By contrast, Java uses pass by reference semantics by default. In C, to swap two variables using pass by value: void swap (int* p, int* q) { int t = *p; *p = *q; *q = t; } In C++, to swap two variables using pass by reference: void swap (int&amp; p, int&amp; q) { std::swap (p, q); } Note that C++ is more efficient because std::swap uses move semantics; there is no temporary variable required to move variables. With copy semantics, a temporary is required. However, with primitive data types, there is a way to swap values without using a temporary, using a chain of exclusive-or assignments: void swap (int* p, int* q) { *p^=*q^=*p^=*q; }


Write a function using reference variables as arguments to swap the values of a pair of integers?

#include&lt;iostream&gt; using namespace std; void swap(int &amp;i, int &amp;j) { int temp = i; i = j; j = temp; } int main() { int i,j; cin&gt;&gt;i&gt;&gt;j; cout &lt;&lt; i &lt;&lt; j &lt;&lt; endl; swap(i,j); cout &lt;&lt; i &lt;&lt; j; return 0; }


How do you write a program in C to swap two variables using a function?

#include&lt;iostream&gt; void swap(int* x, int* x){ (*x)^=(*y)^=(*x)^=(*y); } int main() { int a, b; a=10; b=20; std::cout&lt;&lt;"Before swap: a="&lt;&lt;a&lt;&lt;", b="&lt;&lt;b&lt;&lt;std::endl; swap(&amp;a,&amp;b); std::cout&lt;&lt;"After swap: a="&lt;&lt;a&lt;&lt;", b="&lt;&lt;b&lt;&lt;std::endl; return(0); }


How do you write a program in Perl to swap two variables without using the third one?

Use list assignment i.e. for two variables $a, $b: ($a,$b) = ($b,$a)


What is calling by reference How it is different from call by value?

Call By Value, the standard way of doing things in C and C++, is where a copy of an object is placed in the parameter stack. The called function can access and manipulate that copy at will, but it cannot change the original copy because it has no way of knowing where that original copy is located. Call By Reference, on the other hand, is where the address of an object is placed in the parameter stack. Using extra syntax, the * or the -&gt;, the called function can access and manipulate the original copy at will.

Related questions

Swap two number using pointer?

The only way to swap two values using call by value semantics is to pass pointer variables by value. A pointer is a variable that stores an address. Passing a pointer by value copies the address, the value of the pointer, not the pointer itself. By passing the addresses of the two values to be swapped, you are effectively passing those values by reference. Both C and C++ use pass by value semantics by default, however C++ also has a reference data type to support native pass by reference semantics. By contrast, Java uses pass by reference semantics by default. In C, to swap two variables using pass by value: void swap (int* p, int* q) { int t = *p; *p = *q; *q = t; } In C++, to swap two variables using pass by reference: void swap (int&amp; p, int&amp; q) { std::swap (p, q); } Note that C++ is more efficient because std::swap uses move semantics; there is no temporary variable required to move variables. With copy semantics, a temporary is required. However, with primitive data types, there is a way to swap values without using a temporary, using a chain of exclusive-or assignments: void swap (int* p, int* q) { *p^=*q^=*p^=*q; }


Swap two numbers using call by value and call by reference?

You cannot swap two numbers using call by value, because the called function does not have access to the original copy of the numbers.Swap with call by reference... This routine uses exclusive or swap without temporary variable.void swap (int *a, int *b) {*a ^= *b;*b ^= *a;*a ^= *b;return;}


How do you write a program in C plus plus plus plus How do you write a program in C to swap two variables without using the third oneo swap two variables without using the third one?

To swap two variables without using a third variable, use exclusive or manipulation... a ^= b; b ^= a; a ^= b;


Simple c program for the call by reference?

In call by reference, you are calling the program by passing the variables address(reference) to it. This is done through the use of functions. Any changes made in the variables to the function will be reflected even the calling function.Here is a code snippet for swapping two numbers.#includevoid swap( int *a, int *b){int temp = *a;*a = *b;*b = temp;}int main(){int a=8, b=9;printf("The value of a and b before swap = %d %d\n", a, b);swap(a,b);printf("The value of a and b after swap = %d %d\n", a, b);return 0;}


How do you swap two variables using airthematic operators?

a += b; b -= a; a -= b;


How do you write a program in c to swap two variables without using the add and multiplication?

int varA, varB; int tmp; // common way to swap 2 variables in the same type; tmp = varA; // store the original value of varA varA = varB; // assign A with B varB = tmp; // B now has the original value of varA


Write a function using reference variables as arguments to swap the values of a pair of integers?

#include&lt;iostream&gt; using namespace std; void swap(int &amp;i, int &amp;j) { int temp = i; i = j; j = temp; } int main() { int i,j; cin&gt;&gt;i&gt;&gt;j; cout &lt;&lt; i &lt;&lt; j &lt;&lt; endl; swap(i,j); cout &lt;&lt; i &lt;&lt; j; return 0; }


How do you swap variables in PHP?

By using a third temporary variable. $tmp = $a; $a = $b; $b = $tmp;


How do you write a program in C to swap two variables using a function?

#include&lt;iostream&gt; void swap(int* x, int* x){ (*x)^=(*y)^=(*x)^=(*y); } int main() { int a, b; a=10; b=20; std::cout&lt;&lt;"Before swap: a="&lt;&lt;a&lt;&lt;", b="&lt;&lt;b&lt;&lt;std::endl; swap(&amp;a,&amp;b); std::cout&lt;&lt;"After swap: a="&lt;&lt;a&lt;&lt;", b="&lt;&lt;b&lt;&lt;std::endl; return(0); }


How do yow swap two variables in java using a function?

You can swap two variables, by storing one of them temporarily in a third variable, like this: temp = a; a = b; b = temp; Inside a function, this won't work, because the function parameters are COPIES of the original variables, not the variables themselves. Any change won't affect the original variables. If you work with OBJECTS, and swap the CONTENTS of the objects (not the object pointers), it can work, though.


How do you write a program in C to swap two variables without using the third one using XOR?

a=a^b; b=a^b; a=a^b;


Swapping of 2 numbers using java language?

Use a temporary variable. Example (swap variables "a" and "b"): int a = 5; int b = 10; // Swap the variables int temp; temp = a; a = b; b = temp; System.out.println("a = " + a); System.out.println("b = " + b);Use a temporary variable. Example (swap variables "a" and "b"): int a = 5; int b = 10; // Swap the variables int temp; temp = a; a = b; b = temp; System.out.println("a = " + a); System.out.println("b = " + b);Use a temporary variable. Example (swap variables "a" and "b"): int a = 5; int b = 10; // Swap the variables int temp; temp = a; a = b; b = temp; System.out.println("a = " + a); System.out.println("b = " + b);Use a temporary variable. Example (swap variables "a" and "b"): int a = 5; int b = 10; // Swap the variables int temp; temp = a; a = b; b = temp; System.out.println("a = " + a); System.out.println("b = " + b);