answersLogoWhite

0


Best Answer

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;

}

User Avatar

Wiki User

14y ago
This answer is:
User Avatar

Add your answer:

Earn +20 pts
Q: Swap two numbers using call by value and call by reference?
Write your answer...
Submit
Still have questions?
magnify glass
imp
Related questions

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


How to swap two numbers by call by reference in c plus plus?

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


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){...}


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


Write a program to swap two numbers using function?

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


How do you swap two numbers without using third one using pointers?

How do you do. I am doing well thank you. Swap two number by using reference operators A tough assignment, it will make you think. I think you are confusing reference operators with pointers. Were I you I would study the section on pointers in your text book or course material.


How do you write a javascript to swap two numbers without using third one?

By using the algorithm of bitwise EORing (Exclusive ORing) the numbers together:If the two numbers are X and Y, then to swap them:X = X EOR YY = Y EOR XX =X EOR Ywill swap them.With knowledge of that algorithm, one then uses the syntax of Javascript to implement it.


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


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& x,int& y){x^=y^=x^=y;} Using pointers: void swap(int* const x,int* const y){ assert(x && y); // null pointers not permitted! (*x)^=(*y)^=(*x)^=(*y);}


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

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


How to write Program to swap two variables using function call by value?

//This program swaps the values in the variable using function containing reference arguments #include<iostream.h> void swap(int &iNum1, int &iNum2); void main() { int iVar1, iVar2; cout<<"Enter two numbers "<<endl; cin>>iVar1; cin>>iVar2; swap(iVar1, iVar2); cout<<"In main "<<iVar1<<" "<<iVar2<<endl; } void swap(int &iNum1, int &iNum2) { int iTemp; iTemp = iNum1; iNum1 = iNum2; iNum2 = iTemp; cout<<"In swap "<<iNum1<<" "<<iNum2<<endl; } Reference arguments are indicated by an ampersand (&) preceding the argument: int &iNUm1; the ampersand (&) indicates that iNum1 is an alias for iVar1 which is passed as an argument. The function declaration must have an ampersand following the data type of the argument: void swap(int &iNum1, int &iNum2) The ampersand sign is not used during the function call: swap(iVar1, iVar2); The sample output is Enter two numbers 12 24 In swap 24 12 In main 24 12 ------------------------------------------------------------------ By Satish from here / * Program to Swap Two Numbers by Using Call By Reference Method * / #include main() { int i, j; clrscr(); printf("Please Enter the First Number in A : "); scanf("%d",&i); printf("\nPlease Enter the Second Number in B : "); scanf("%d",&j); swapr(&i,&j); /* call by reference*/ printf("A is now in B : %d",i); printf("B is now in A : %d",j); } /* call by reference function*/ swapr(int *x, int *y) { int t; t=*x; *x=*y; *y=t; }