answersLogoWhite

0


Best Answer

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

}

User Avatar

Wiki User

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

Wiki User

13y ago

#include <stdlib.h>

#include <stdio.h>

swap (int *a, int *b) {

*a ^= *b;

*b ^= *a;

*a ^= *b;

}

int main (int /*argc*/, char **/*argv*/) {

int a = 17;

int b = 375;

printf ("a: %d b: %d\n", a, b);

swap (&a, &b);

printf ("a: %d b: %d\n", a, b);

return 0;

}

This answer is:
User Avatar

User Avatar

Wiki User

13y ago

#include

void main()

{

int a,b;

a=5;

b=6;

/* now adding b to a we have */;

a=b+a;

b=a-b; /* now here b will get value of a */;

a=a-b /*this will give a value of b */;

}

This answer is:
User Avatar

User Avatar

Wiki User

9y ago

The only way to achieve this is to pass pointer variables. A pointer is a variable that holds a memory address and allows indirect access to that memory. When you pass a pointer to a function, the pointer is passed by value. But since the value of a pointer is a memory address, it is the same as pass by reference:

template<typename T>

void swap (T* a, T* b) {

T* temp = a;

a = b;

b = temp;

}

This answer is:
User Avatar

User Avatar

Wiki User

11y ago

Swap by value achieves nothing since the values are simply copies of the values you actually want to swap. Swapping the copies does not swap the actual values. The only way to achieve this is by passing pointers. Pointers are always passed by value, but dereferencing the memory they point to makes them act like references.

void SwapByReference(int & a, int & b )

{

int c = a;

a = b;

b = c;

}

void SwapByValue(int * a, int * b )

{

int c = *a;

*a = *b;

*b = c;

}

This answer is:
User Avatar

User Avatar

Wiki User

16y ago

for .NET C++ define the following template template void NET_Swap(T % A, T % B) { const T C = A; A = B; B = C; }

This answer is:
User Avatar

User Avatar

Wiki User

13y ago

Only 'call-by-value' is possible in C, so you have to use pointers:

void swap (type *pa, type *pb) {

type tmp;

tmp = *pa;

*pa = *pb;

*pb = tmp;

}

This answer is:
User Avatar

Add your answer:

Earn +20 pts
Q: How to write Program to swap two variables using function call by value?
Write your answer...
Submit
Still have questions?
magnify glass
imp
Continue Learning about Engineering

Program to exchange the value x and y using 3 variables?

temp = x; x = y; y = temp;


Which variable is not destroyed on exit from the function instead its value is presented and becomes available again when the function is next called These variables are declared as?

Local function variables defined static remain in memory at all times. Such variables are only in scope (accessible) when the function itself is in scope.


What module returns a value back to the part of the program that called it?

A function. You can have a function that returns but doesn't return a value with it.


Write a program that defines a template function named add This function takes two arguments add two variables and then return the sum?

Write a program that defines a template function named add(). This function takes two arguments, add two variables and then return the sum. In main function, define two variables of type int, two variables of type float and two objects of type 'String'. Now call the add() function three times for these different data types. Note: String is a user-defined data type and for this you have to define a class named 'String'. When template function will be called to add two objects of type String then it must concatenate two strings. Your output should look like this: Sample Output: Enter two integer values to be added Enter First value: 12 Enter Second value: 25 Enter two float values to be added Enter First value: 13.5 Enter Second value: 14.2 Enter two Strings to be added Enter First value: Virtual Enter Second value: University Addition of two variables of different data types Sum of values of type int = 37 Sum of values of type float = 27.7 Sum of values of type String = VirtualUniversity


Why are public instance variable dangerous?

Public instance variables are not dangerous, but can put your program at risk of being hacked. For instance, say the variable bacon is an integer that represents your health in a game, and its value is 50. Someone could change its value to 1,000,000 by using a program called a decompiler, then change the code, and recompile it.

Related questions

What are independent variables and dependent variables in math?

Independent variables are the input value of a function (usually x) and dependent variables are the output value of the function (usually y).


Program to exchange the value x and y using 3 variables?

temp = x; x = y; y = temp;


When can we say that a function is a relation?

When it doesn't fulfill the requirements of a function. A function must have EXACTLY ONE value of one of the variables (the "dependent variable") for every value of the other variable or variables (the "independent variable").


When can we say that a relation is a function?

When it doesn't fulfill the requirements of a function. A function must have EXACTLY ONE value of one of the variables (the "dependent variable") for every value of the other variable or variables (the "independent variable").


Why are function notation important?

A function is a relationship between quantities (variables) that occurs when the value of one of the quantities can be given uniquely by specified values of the other quantities. The variables involved can be either independent or dependent. The values of certain variables are fixed while others are allowed to change. The fixed variables are called the independent variables, and the dependent variables are those that change in response to the given value of the independent variable. A function therefore relates dependent variables to independent variables, the only restriction being that each value of the dependent variable is given uniquely by one, and only one, value for each of the independent variables.


To evaluate a function for a specific value substitute the value in for what?

You substitute the variable for its value. Or you substitute the variables for each of the values.


Which variable is not destroyed on exit from the function instead its value is presented and becomes available again when the function is next called These variables are declared as?

Local function variables defined static remain in memory at all times. Such variables are only in scope (accessible) when the function itself is in scope.


What mean by reference function in c?

Reference function has no meaning. Variables are passed to functions by reference or by value, depending on the function signature.


What is the difference between high and maxinum in Excel?

Maximum is the highest value in a range of values and can be got using the MAX function. There is no function called High in Excel.Maximum is the highest value in a range of values and can be got using the MAX function. There is no function called High in Excel.Maximum is the highest value in a range of values and can be got using the MAX function. There is no function called High in Excel.Maximum is the highest value in a range of values and can be got using the MAX function. There is no function called High in Excel.Maximum is the highest value in a range of values and can be got using the MAX function. There is no function called High in Excel.Maximum is the highest value in a range of values and can be got using the MAX function. There is no function called High in Excel.Maximum is the highest value in a range of values and can be got using the MAX function. There is no function called High in Excel.Maximum is the highest value in a range of values and can be got using the MAX function. There is no function called High in Excel.Maximum is the highest value in a range of values and can be got using the MAX function. There is no function called High in Excel.Maximum is the highest value in a range of values and can be got using the MAX function. There is no function called High in Excel.Maximum is the highest value in a range of values and can be got using the MAX function. There is no function called High in Excel.


What module returns a value back to the part of the program that called it?

A function. You can have a function that returns but doesn't return a value with it.


What are the advantages of using pointers in a program?

pointers points to the memory address of another variable.....in functions we have two kind of variables the actual and dummy variable. when we operate on variables..the value of dummy variables are effected, but if we want to make changes in the actual variable then we have to refer to their address..and we can reach to address of the variables by only using pointers.


How do you evaluate in mathematics?

Evaluate means find the value of.To evaluate an expression, if there are any variables replace them by their values. Then, using BIDMAS/PEMDAS, calculate the value of the expression.Evaluate means find the value of.To evaluate an expression, if there are any variables replace them by their values. Then, using BIDMAS/PEMDAS, calculate the value of the expression.Evaluate means find the value of.To evaluate an expression, if there are any variables replace them by their values. Then, using BIDMAS/PEMDAS, calculate the value of the expression.Evaluate means find the value of.To evaluate an expression, if there are any variables replace them by their values. Then, using BIDMAS/PEMDAS, calculate the value of the expression.