Return Values in C++ Functions

A function can return a value to the caller, which can be used for further calculations or operations.

Example: Function with Return Value

#include <iostream>

int square(int number) {
    return number * number;
}

int main() {
    int result = square(5);
    std::cout << "Square of 5: " << result << std::endl;
    return 0;
}

Explanation: The square function returns the square of the provided number parameter, which is used in the main function.

Key Takeaways

  • Return values allow functions to produce output for further use.
  • Functions with a return type must include a return statement.
  • Ensure the function return type matches the data type of the returned value.