C++ return Keyword
π C++ return Keyword
The return keyword in C++ is used to send a value back from a function and terminate the function execution.
πΉ 1. Basic Syntax
or (for void functions):
πΉ 2. Simple Return Example
πΉ 3. Return Multiple Times (Conditional Return)
β‘ Function exits as soon as return is executed.
πΉ 4. Return in void Function
πΉ 5. Returning Expressions
πΉ 6. Returning a Reference
β οΈ Must return a reference to an existing variable (not local).
πΉ 7. Returning a Pointer
πΉ 8. Return an Array (Using Pointer)
β οΈ Caller must delete the memory.
πΉ 9. Return Struct or Class Object
πΉ 10. Return vs cout
β return sends data back
β cout only prints
β Common Mistakes
π return and main()
-
0β successful execution -
Non-zero β error code
π Summary
-
returnsends a value back to the caller -
Immediately ends function execution
-
Required for non-
voidfunctions -
Can return values, references, pointers, objects
-
return;can be used invoidfunctions
