関数を使って 2 つの数値を入れ替える

C++Beginner
オンラインで実践に進む

はじめに

この実験では、C++ の関数を使って 2 つの数値を入れ替える方法について説明します。関数を使ってこの問題を解く方法は 2 通りあります。

新しい C++ ファイルを作成する

始めるには、好きなコードエディタで新しい C++ ファイルを作成します。実験の最初にコードファイルのパスと名前を指定する必要があります。たとえば:~/project/main.cpp

touch ~/project/main.cpp

値渡しを使って 2 つの数値を入れ替える

値渡しでは、関数を呼び出す際に実引数が渡され、形式引数に対する操作の影響が実引数に反映されません。以下はその実装方法です。

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

コードを実行するには、ファイルを保存し、ターミナルを開いて次のコマンドを入力します。

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

参照渡しを使って 2 つの数値を入れ替える

参照渡しでは、形式引数に実引数のアドレスを渡すので、形式引数の変更が実引数に反映されます。以下はその実装方法です。

#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*/
}

コードを実行するには、ファイルを保存し、ターミナルを開いて次のコマンドを入力します。

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

main.cpp ファイルの完全なコード

以下は main.cpp ファイルの完全なコードです。

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

    // 方法 1: 値渡し
    cout<<"\nSwapping of Two Numbers Using Call By Value\n";
    swap(a,b);

    // 方法 2: 参照渡し
    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*/
}

コードを実行する

ファイルを保存し、ターミナルを開いて、コードを実行するために次のコマンドを入力します。

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

まとめ

この実験では、C++ プログラミング言語において、値渡しと参照渡しの方法を使って 2 つの数値を入れ替えるための関数の使い方を学びました。