answersLogoWhite

0


Best Answer

Lets start simple by swapping two int variables in a function call. You must use a & to pass the variables by reference so that when you edit their values the original variables get changed as well.

void swapVariables(int &var1, int &var2)

{

int temp = var1;

var1 = var2;

var2 = temp;

}

You could change this function to use any variable type that you want or you could use a template function instead so that the same function could be used with any variable type.

template <class T>

void swapVariables(T &var1, T &var2)

{

T temp = var1;

var1 = var2;

var2 = temp;

}

Another interesting thing you can do when swapping two variables is use an xor function to swap the two variables without using a temp variable.

void swapVariables(int &var1, int &var2)

{

var1 = var1^var2;

var2 = var1^var2;

var1 = var1^var2;

}

Which can also be simplified to:

void swapVariables(int &var1, int &var2)

{

var1 ^= var2 ^= var1^= var2;

}

User Avatar

Wiki User

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

Wiki User

14y ago

swap(int *p1, int *p2) { int temp; temp = *p1; *p1 = *p2; *p2 = temp; }

This answer is:
User Avatar

Add your answer:

Earn +20 pts
Q: Swap the value of two variables using call by value by c plus plus?
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; }


Swap the value of two variables using call by reference?

swap (int *a, int *b) { *a ^= *b; *b ^= *a; *a ^= *b; } int c = 13; int d = 27; swap (&amp;c, &amp;d); /*c is now 27 and d is now 13 */ Note: there is no call-by-reference in C. In C++: void swap (int &amp;a, int &amp;b) { . int tmp; . tmp = a; . a = b; . b = tmp; }


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.


How would you write a program to swap the values of character variable using call by address?

void swap (int* a, int* b) { if (!a !b) return; // can't swap a pointer to null *a^=*b^=*a^=*b; }


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

Related questions

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


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


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


Swap the value of two variables using call by reference?

swap (int *a, int *b) { *a ^= *b; *b ^= *a; *a ^= *b; } int c = 13; int d = 27; swap (&amp;c, &amp;d); /*c is now 27 and d is now 13 */ Note: there is no call-by-reference in C. In C++: void swap (int &amp;a, int &amp;b) { . int tmp; . tmp = a; . a = b; . b = tmp; }


C program to swapping two numbers using call by value method?

You have to pass the address of the variables.void swap (int *pa, int *pb){...}


What do you call finding the value of the variables that make equations true?

Solving the equation.


What do you call it when you trade something you have for something else of equal value?

It could be BARTER or simply SWAP.


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.


How would you write a program to swap the values of character variable using call by address?

void swap (int* a, int* b) { if (!a !b) return; // can't swap a pointer to null *a^=*b^=*a^=*b; }


Can you swap two numbers by call by reference outside the function?

The using of term 'call-by-reference' implies function-call, so please rethink your question...


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


Wap to interchange values of two variabkes by implementing call by value and call by reference method?

You cannot swap values via pass by value since the values would be copies of the original values, therefore only the copies would be swapped. You have to use pass by value, either by reference or by pointer. References are easier to implement: void swap(int&amp; x,int&amp; y){x^=y^=x^=y;} Using pointers: void swap(int* const x,int* const y){ assert(x &amp;&amp; y); // null pointers not permitted! (*x)^=(*y)^=(*x)^=(*y);}