Program Discussion :: Basics
71 / 279
Write a program, swapping function by passing value using call by reference?
Answer:
#include
using namespace std;
/* function declaration */
void swap(int &x, int &y);
int main () {
/* local variable declaration: */
int a = 100;
int b = 200;
cout
Asked In ::
Language:
Pankaj
24 Jun, 2017 5:27 PM
<!DOCTYPE html>
<html>
<body>
<?php
$x = 1998;
if ((($x % 4 == 0) && ($x % 100 != 0)) || ($x % 400 == 0)) {
echo ("this is a leap year");
}
else {
echo ("this is not a leap year");
}
?>
</body>
</html>
Language:
Ganesh
7 Jul, 2017 9:30 AM
#include<stdio.h>
#include<conio.h>
void swap(int *num1, int *num2);
void main() {
int x, y;
printf("\nEnter First number : ");
scanf("%d", &x);
printf("\nEnter Second number : ");
scanf("%d", &y);
printf("\nBefore Swaping x = %d and y = %d", x, y);
swap(&x, &y); // Function Call - Pass By Reference
printf("\nAfter Swaping x = %d and y = %d", x, y);
getch();
}
void swap(int *num1, int *num2) {
int temp;
temp = *num1;
*num1 = *num2;
*num2 = temp;
}
Language:
Varun
7 Jul, 2017 9:30 AM
#include <iostream>
using namespace std;
/* function declaration */
void swap(int &x, int &y);
int main () {
/* local variable declaration: */
int a = 100;
int b = 200;
cout << "Before swap, value of a :" << a << endl;
cout << "Before swap, value of b :" << b << endl;
/* calling a function to swap the values using variable reference.*/
swap(a, b);
cout << "After swap, value of a :" << a << endl;
cout << "After swap, value of b :" << b << endl;
return 0;
}
Language:
Siddhartha Paul
19 Jun, 2019 2:16 PM
#include<stdio.h>
void swap(int *a,int *b)
{
int temp;
temp=*a;
*a=*b;
*b=temp;
}
void main()
{
int x,y;
printf("\nEnter 2 numbers: ");
scanf("%d",&x);
scanf("%d",&y);
printf("\nBefore swapping, value of x=%d and y=%d",x,y);
swap(&x,&y);
printf("\nAfter swapping, value of x=%d and y=%d",x,y);
}