C++の基礎:関数のvoidと戻り値の型について学ぶ
概要
別の記事では、簡単な関数の宣言と定義方法を説明しました。さらに別の記事では、main関数にはコードが正常に完了したかどうかを表す整数の戻り値があることを説明しました。C++プログラミング言語では、多くの関数を簡単に定義することができます。返り値は、整数(int)、浮動小数点(float)、文字列(string)などの単一の変数のほか、複数の変数、ベクター、構造体、クラスオブジェクト、ポインタなどを返すことができます。
すべての戻り値の型は、関数の名前の前に定義されています。戻り値がない場合は、voidキーワードを使用します。つまり、この関数には戻り値がなく、それはvoid関数であることを意味します。
void myfunction() // this is the function declaration, return type is void
{
// the definition of the function (statements and other functions)
// no return statement here because it is a void function
}
こちらの記事で紹介したinfo()のサンプルでは戻り値がなかったので、ここではvoidを使ってこの関数を定義します。コンパイラによっては、関数内に戻り値がない場合、void型を使用しなければならない場合があり、そうでない場合はエラーや警告が出ることがあります。
void info()
{
std::cout << "This is my application.\n";
std::cout << "Here is my information.\n";
std::cout << "Developed in 2021 by My Company\n";
std::cout << '\n';
}
整数型の関数の例
戻り型を整数値として定義し、その値を関数内で返すことができます。 ここで、戻り値(結果)は、定義された戻り値の型と同じであることが期待されますが、以下の例をご覧ください。
int calculate() // return type is int
{
int result = 500+3*100;
return result; // return value is int
}
以下の完全な例のように、この整数型の関数をmain関数の中で使用できます。
#include <iostream>
int calculate()
{
int result = 500+3*100;
return result;
}
int main()
{
int height = calculate();
std::cout << "vertical distance at 0.5 seconds for an object drop is " << height << '\n';
}
戻り値の制限に従って、この関数の戻り型および戻り値として、bool、char、int、short int、unsigned short int、long int、unsigned long int、long long int、unsigned long longintを使用できます。 詳しくはデータ型の詳細をご覧ください。
浮動小数点型の関数の例
float calculate() // return type float
{
float result = 0.5*9.81*0.5*0.5; // (1/2)(gt^2)
return result; // return value float
}
以下の例のように、main関数の中でこのfloat 関数を使うことができます。
#include <iostream>
float calculate()
{
float result = 0.5*9.81*0.5*0.5; // (1/2)(gt^2)
return result;
}
int main()
{
float height = calculate();
std::cout << "vertical distance at 0.5 seconds for an object drop is " << height << '\n';
}
floatの戻り型と戻り値の代わりに、doubleを使用することもできます。
文字列型関数の例
using namespace std;を宣言してstringを使用、あるいはstd::stringを使用して文字列関数を定義できます。以下のinfo()関数の例では、アプリケーションの情報を文字列形式で返しています。
std::string info() // return type is string
{
std::string str = "This is My Application and developed by My Company !";
return str; // return value is string
}
以下のコード例のように、main関数の中でこの文字列関数を使うことができます。
#include <iostream>
#include <string>
std::string info()
{
std::string str = "This is My Application and developed by My Company !";
return str;
}
int main()
{
std::string appinfo;
appinfo = info(); // calling a string function
std::cout << appinfo << '\n'; // we may directly call info() here too
}
ここでは、最後の行で、appinfo変数を宣言して設定する代わりに、以下のように関数を文字列のように直接使用することができます。
std::cout << info() << '\n';
構造体関数の例(複数の変数型を返す)
場合によっては、複数の変数型で形成された構造体を使用して、複数の変数を返すことができます。以下の例では、複数の変数を持つロボット情報の構造体と関数セットがあり、関数は複数の変数を返します。
#include <iostream>
#include <string>
struct mystruct
{
int id;
float x,y;
std::string name;
};
struct mystruct robotinfo()
{
struct mystruct result;
result.id = 1;
result.x = 15.5;
result.y = 20.2;
result.name = "Robot Model One";
return result;
}
int main()
{
struct mystruct robot = robotinfo();
std::cout << "Robot Params:" << robot.name << ',' << robot.id << ',' << robot.x << ',' << robot.y << '\n';
getchar();
}
関数は、内部で得られたデータを転送するのに非常に便利です。関数からは,任意の型(ベクター,クラスなど)を返すことができますし,ポインタを返すこともできます.ポインタパラメータは、関数の内部または使用する関数の前、外部、または別の関数によって割り当てる必要があることに注意してください。