C++ 中的返回語句



C++ 中的 **return** 語句用於退出函式並將值(可選)傳送回函數的呼叫者,這取決於需求。它在控制程式流程和確保函式向程式碼的其他部分提供結果方面起著非常重要的作用。

語法

以下是 C++ 中使用 return 語句的語法:

return [expression];

其中,“**表示式**”是可選的,專門用於函式。如果提供,它指定要返回給呼叫者的值。

return 語句示例

以下是 return 語句的示例:

#include <iostream>
using namespace std;

int sum(int a, int b){
   // Here, returning sum of a and b
   return a + b;
}

int main(){
   // Calling the function
   int ans = sum(5, 2);
   cout << "The sum of two integers 5 and 2 is:  " << ans << endl;

   // Returning from the main (),
   // 0 represents execution done without any error
   return 0;
}

輸出

The sum of two integers 5 and 2 is:  7

return 語句的關鍵方面

1. 函式終止

執行 return 語句時,函式會立即退出,並可選地將值返回給呼叫者。

2. 返回型別

按值返回

在此,return 語句中指定的值將傳送回撥用者。這對於執行計算或需要提供結果的函式至關重要。

int Add(int a, int b) {
   return a + b;  // Returns the sum of a and b
}

無返回值 (void)

對於宣告為 void 的函式,可以使用不帶表示式的 return 語句來提前退出函式。

void GetMessage() {
   cout << "Hello, TutorialsPoint Learner!";
   return;  // Exits the function
}

3. 多個 return 語句

一個函式可能包含多個 return 語句,我們通常在條件語句中看到這些語句。

int max(int a, int b) {
   if (a > b)
      return a;
   else
      return b;
}

4. 返回物件

函式可以返回物件,這對於返回封裝在類或結構體中的多個值很有用。

struct point {
   int x, y;
};

point getOrigin() {
   return {0, 0};
}

5. 提前退出

return 語句可以用來提前退出函式,這對於錯誤處理或特殊條件很有用。

int divideInteger(int a, int b) {
   if (b == 0) {
      cer << "Error: Division by zero!" << endl;
      return -1; // Shows an error
   }
   return a / b;
}

C++ 中的返回型別和值處理

在 C++ 中,函式的返回型別決定了函式將返回給呼叫者的值型別(如果有)。正確處理返回型別和值對於確保函式按預期執行並與程式的其他部分平滑整合非常重要。

1. 原生資料型別

原生資料型別是 C++ 提供的基本內建型別。常見的示例包括 int、float、double、char 等。

示例

#include <iostream>
using namespace std;
// created a function which returns an integer
int getSquare(int num) {
   return num * num;
}
int main() {
   int value = 5;
   int result = getSquare(value); // Calling the function and storing its result
   cout << "The square of " << value << " is " << result << endl;
   return 0;
}
輸出
The square of 5 is 25

2. 使用者定義型別

使用者定義型別包括結構體。這些型別允許您定義複雜的資料結構並根據您的特定需求對其進行自定義。

結構體示例

#include <iostream>
using namespace std;
struct Point {
   int x;
   int y;
};
Point createPoint(int x, int y) {
   Point p;
   p.x = x;
   p.y = y;
   return p; // will returns a Point object
}
int main() {
   Point p = createPoint(10, 20);
   cout << "Point coordinates: (" << p.x << ", " << p.y << ")" << endl;
   return 0;
}
輸出
Point coordinates: (10, 20)

類示例

#include <iostream>
using namespace std;

class rectangle {
   public:
      rectangle(int w, int h) : width(w), height(h) {}
      int getArea() const {
         return width * height;
      }
   private:
      int width;
      int height;
};

rectangle createRectangle(int width, int height) {
   return rectangle(width, height); // Returns a Rectangle object
}

int main() {
   rectangle rect = createRectangle(10, 5);
   cout << "Area of given Rectangle is: " << rect.getArea() << endl;
   return 0;
}
輸出
Area of given Rectangle is: 50

3. 引用和指標

引用指標用於引用變數或物件而無需進行任何複製。這對於效率和在需要時輕鬆修改原始資料很有用。

按引用返回

#include <iostream>
using namespace std;
int globalValue = 100;

int& getGlobalValue() {
   return globalValue; // Returns a reference to the global variable
}

int main() {
   int& ref = getGlobalValue();
   ref = 200; // Modifies the global variable
   cout << "Global Value: " << globalValue << endl; 
   return 0;
}
輸出
Global Value: 200

按指標返回

#include <iostream>
using namespace std;
int* createArray(int size) {
   int* array = new int[size]; // Allocating memory dynamically
   for (int i = 0; i < size; ++i) {
      array[i] = i * 10;
   }
   return array; // Returning a pointer to the allocated array
}

int main() {
   int* myArray = createArray(5);
   for (int i = 0; i < 5; ++i) {
      cout << myArray[i] << " ";
   }
   delete[] myArray; // Free dynamically allocated memory
   return 0;
}
輸出
0 10 20 30 40 
廣告