C++ Return Statement

C++ Return Statement

The return statement in C++ is used to terminate the execution of a function and return a value to the caller. When a return statement is encountered, the control of the program returns to the calling function along with the value specified in the return statement.

  1. Syntax: The syntax of the return statement is as follows: cpp return expression;

  2. Example: In this example, the function addNumbers takes two integers as input, adds them together, and returns the result. cpp int addNumbers(int a, int b) { int sum = a + b; return sum; }

  3. Execution: When the return statement is executed, the value of sum is returned to the caller of the addNumbers function.

  4. Function Termination: After the return statement is encountered, the function terminates, and the control returns to the caller.

  5. Returning Values: The return statement can be used to return different types of values, depending on the function's return type.

The return statement is a fundamental aspect of C++ functions, allowing them to produce results and communicate information back to the calling code.