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.
Syntax: The syntax of the return statement is as follows:
cpp return expression;
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; }
Execution: When the
return
statement is executed, the value ofsum
is returned to the caller of theaddNumbers
function.Function Termination: After the return statement is encountered, the function terminates, and the control returns to the caller.
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.