搜尋此網誌

2025年9月9日星期二

Returning values from a function

Returning values from a function in C++ is a fundamental concept that allows a function to send data back to the part of the program that called it.

Here’s a simple function that returns an int:

int add(int a, int b) {

    return a + b;  // returns the sum of a and b

}


int main() {

    int result = add(5, 3);

    cout << "Result: " << result << endl;  // Output: 8

}


int add(int a, int b) → declares a function that returns an int.

return a + b; → sends the result back to the caller.


Microsoft Copilot

沒有留言:

發佈留言