answersLogoWhite

0

How do you swap two variables using function?

Updated: 8/19/2019
User Avatar

Wiki User

13y ago

Best Answer

using pointers, example:

void Swapd (double *d1, double *d2)

{

. double tmp= *d1;

. *d1= *d2;

. *d2= *tmp;

}

User Avatar

Wiki User

13y ago
This answer is:
User Avatar

Add your answer:

Earn +20 pts
Q: How do you swap two variables using function?
Write your answer...
Submit
Still have questions?
magnify glass
imp
Related questions

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


How do you swap two variables using airthematic operators?

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


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)


Write a program to swap two numbers using function?

swap (int *a, int *b) { *a ^= *b; *b ^= *a; *a ^= *b; }


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;


How do you compute the probability distribution of a function of two Poisson random variables?

we compute it by using their differences


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& p, int& 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; }


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


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 the value of two variables using call by value by c plus plus?

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


What is the difination of linear function?

A linear function is any function that graphs to a straight line. What this means mathematically is that the function has either one or two variables with no exponents or powers. If the function has more variables, the variables must be constants or known variables for the function to remain a linear function.