Swap Two Numbers Using Functions

C++C++Beginner
Practice Now

Introduction

In this lab, we will be discussing how to swap two numbers using functions in C++. There are two methods to solve this problem with the help of functions:


Skills Graph

%%%%{init: {'theme':'neutral'}}%%%% flowchart RL cpp(("`C++`")) -.-> cpp/SyntaxandStyleGroup(["`Syntax and Style`"]) cpp(("`C++`")) -.-> cpp/BasicsGroup(["`Basics`"]) cpp(("`C++`")) -.-> cpp/AdvancedConceptsGroup(["`Advanced Concepts`"]) cpp(("`C++`")) -.-> cpp/FunctionsGroup(["`Functions`"]) cpp/SyntaxandStyleGroup -.-> cpp/comments("`Comments`") cpp/BasicsGroup -.-> cpp/variables("`Variables`") cpp/BasicsGroup -.-> cpp/data_types("`Data Types`") cpp/BasicsGroup -.-> cpp/operators("`Operators`") cpp/AdvancedConceptsGroup -.-> cpp/pointers("`Pointers`") cpp/FunctionsGroup -.-> cpp/function_parameters("`Function Parameters`") subgraph Lab Skills cpp/comments -.-> lab-96216{{"`Swap Two Numbers Using Functions`"}} cpp/variables -.-> lab-96216{{"`Swap Two Numbers Using Functions`"}} cpp/data_types -.-> lab-96216{{"`Swap Two Numbers Using Functions`"}} cpp/operators -.-> lab-96216{{"`Swap Two Numbers Using Functions`"}} cpp/pointers -.-> lab-96216{{"`Swap Two Numbers Using Functions`"}} cpp/function_parameters -.-> lab-96216{{"`Swap Two Numbers Using Functions`"}} end

Create a new C++ file

To get started, create a new C++ file in your preferred code editor. You must provide the path and name of the code file at the beginning of the lab. For example: ~/project/main.cpp.

touch ~/project/main.cpp

Swap Two Numbers Using Call by Value

In Call by Value, actual parameters are passed while calling the function, and the operations effect on the formal parameters doesn't reflect on the actual parameters. Here's how to implement it:

#include<iostream>
using namespace std;

void swap(int x,int y);
/*Swapping of Two Numbers in C++ Using Functions Call by Value*/

int main()
{
    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;
}

To run the code, save the file and open the terminal and type the following command:

g++ main.cpp -o main && ./main

Swap Two Numbers Using Call by Reference

In Call by Reference, we pass the address of the actual parameter in the formal parameter, so the changes on the formal parameters reflect on the actual parameters. Here's how to implement it:

#include<iostream>
using namespace std;

void swap(int *x,int *y);
/*Swapping of Two Numbers in C++ Using Functions Call by Reference*/

int main()
{
    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 temporary variable*/
    *x = *y;
    /*Copying the second variable Address to the first variable*/
    *y = z;
    /*Copying the temporary variable Address to the second variable*/
}

To run the code, save the file and open the terminal and type the following command:

g++ main.cpp -o main && ./main

Full Code for the main.cpp file

Here is the full code for the main.cpp file:

#include<iostream>
using namespace std;

void swap(int x,int y);
/*Swapping of Two Numbers in C++ Using Functions Call by Value*/

void swap(int *x,int *y);
/*Swapping of Two Numbers in C++ Using Functions Call by Reference*/

int main()
{
    int a,b;
    cout<<"Enter the first number: ";
    cin>>a;

    cout<<"Enter the second number: ";
    cin>>b;

    // Method 1: Call By Value
    cout<<"\nSwapping of Two Numbers Using Call By Value\n";
    swap(a,b);

    // Method 2: Call By Reference
    cout<<"\nSwapping of Two Numbers Using Call By Reference\n";
    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;
}

void swap(int *x,int *y)
{
    int z;
    z = *x;
    /*Copying the first variable Address to the temporary variable*/
    *x = *y;
    /*Copying the second variable Address to the first variable*/
    *y = z;
    /*Copying the temporary variable Address to the second variable*/
}

Run the Code

Save the file, open the terminal and type the following command to run the code:

g++ main.cpp -o main && ./main

Summary

In this lab, we have learned how to use functions to swap two numbers using the Call by Value and Call by Reference methods in C++ programming language.

Other C++ Tutorials you may like