11/15/2015

Swapping of Two Numbers in C++ Using Functions | Call by Reference & Call by Value

Write a Program for Swapping of Two Numbers in C++ Using Call By Value and Call by Reference. There are two methods to solve this problem with the help of functions, and there are two methods to do this the first one is Call By Value and Call by Reference. Now let discuss the Call by Value in details. In the function we can pass the value by 2 ways the first one is Call By Value and the second one is Call by Reference and there are 2 things we have to discuss that Actual Parameter and Formal Parameter to fully understand the passing values in the function in C++ Programming language. In this article I am going to tell you the Call By Value, Call by Reference, Actual Parameter and Formal Parameter to fully understand the programming problem.

Write a Program to Swap of Two Numbers in C++


1. Call by Value


In Call by Value Actual parameter are passed while calling the function, The operations effect on the formal parameters doesn't reflect on the Actual Parameters.

Example: Int A = 5 is an actual parameter and Int X = 5(Here we have Copied the Int A = 5 value to the X = 5), so whatever we do with the X, it does not reflect the Actual Value Int A = 5. It will always remain the same.  If we increase the value of the X by 1 then the value of the X will be 6 and the value of the A remains the same 5 as previous.

2. Call by Reference


In Call by Reference we passed the address of the actual parameter in a formal parameter, So the changes on the Formal Parameters reflect on the Actual parameters. If we take the above example for this then if we increase the value of the X will reflect on the A thus the value of the X and A will be same(X = A = 6)

Before going to understand the Call by value and Call by Reference let's first understood the Actual and Formal Parameters terminology to fully understand the code.


Actual parameters: The Actual parameters that appear in function calls.
Formal parameters: The Formal parameters that appear in function declarations.

1. Swapping of Two Numbers in C++ Using Call by Reference | Functions


#include<iostream>
using namespace std;

void swap(int *x ,int *y );
/*Swapping of Two Numbers in C++ Using Functions Call by Reference*/
int main()
{
   //Program by- Ghanendra Yadav
    int a,b;
    cout<<"Enter Two Numbers To Swap: ";
    cin>>a>>b;
    
    swap(&a,&b);
    
    cout<<"\nAfter Swapping Two Numbers: ";
    cout<<a<<" "<<b<<" \n";
    
    return 0;
}
void swap(int *x,int *y)
{
 int z;
 z=*x;
/*Copying the first variable Address to the tempriory variable*/
 *x=*y;
/*Copying the second variable Address to the first variable*/
 *y=z;
/*Copying the tempriory variable Address to the second variable*/ 
}

2. Program to Swapping of Two Numbers in C++ Using Call by Value | Functions


#include<iostream>
using namespace std;

void swap(int ,int );
/*Swapping of Two Numbers in C++ Using Functions Call by Value*/
int main()
{
   //Program by- Ghanendra Yadav
    int a,b;
    cout<<"Enter the Two Numbers to Swap in C++: ";
    cin>>a>>b;
    cout<<"\nAfter Swapping of Two Numbers:";
    swap(a,b);
    
    return 0;
}
void swap(int x,int y)
{
 int z;
/*Extra veriable for storing the value of first or second variable*/ 
 
 z=x;
/*Copying the first variable value to the tempriory variable*/
 x=y;
/*Copying the second variable value to the first variable*/
 y=z;
/*Copying the tempriory variable value to the second variable*/ 
 cout<<" "<<x<<"   "<<y;
 
}

Program Output of Swapping of Two Numbers Using Call by Reference

Program Output of Swapping of Two Numbers Using Call by Reference

Program Output of  Swapping of Two Numbers Using Call by Value


Program Output of  Swapping of Two Numbers Using Call by Value

Previous Post
Next Post

post written by:

Hi, I’m Ghanendra Yadav, SEO Expert, Professional Blogger, Programmer, and UI Developer. Get a Solution of More Than 500+ Programming Problems, and Practice All Programs in C, C++, and Java Languages. Get a Competitive Website Solution also Ie. Hackerrank Solutions and Geeksforgeeks Solutions. If You Are Interested to Learn a C Programming Language and You Don't Have Experience in Any Programming, You Should Start with a C Programming Language, Read: List of Format Specifiers in C.
Follow Me

7 comments:

  1. Anonymous11/16/2015

    nice job

    ReplyDelete
  2. Can u tell what will be the changes if we use void main()

    ReplyDelete
  3. 2- Write a program to read two integer numbers then swap the values of them using
    a void function named swap.
    3- Write a program to read the items of two dimensional integer array then use a
    function to find the summation of the array items.

    ReplyDelete
  4. tell me how can i make money using my c++ skills????????????????????????????????????????????????????????????????????????????????



    ReplyDelete