GeezerVisonThe World Wide Web, restored to 800 pixels of glory. |
| Home|About|Gaming cabinet|Archives|How it works|Starter sites |
GeezerVison is showing www.tutorialspoint.com — Return Statement in C++
Selected Reading
Return Statement in C++The return statement in C++ is used to exit a function and to send a value back to the function's caller which is optional depending on requirement. It plays a very important role in controlling the flow of a program and making sure that functions will provide results to other parts of the code. SyntaxBelow is the syntax of using return statement in C++ − return [expression]; Where, "expression" is optional, specifically used for function. If provided, it specifies the value to be returned to the caller. Example of return StatementThe following is an example of return statement − #include <iostream>
using namespace std;
int sum(int a, int b){
// Here, returning sum of a and b
return a + b;
}
int main(){
// Calling the function
int ans = sum(5, 2);
cout << "The sum of two integers 5 and 2 is: " << ans << endl;
// Returning from the main (),
// 0 represents execution done without any error
return 0;
}
OutputThe sum of two integers 5 and 2 is: 7 Key Aspects of return Statement1. Function TerminationWhen a return statement is executed, the function exits immediately, and optionally sends value back to the caller. 2. Returning TypesReturn by ValueIn this the specified value in return statement is sent back to the caller. This is essential for functions which perform calculations or need to provide results. int Add(int a, int b) {
return a + b; // Returns the sum of a and b
}
No Return Value (void)The functions which are declared with void, the return statement can be used without an expression to exit the function early. void GetMessage() {
cout << "Hello, TutorialsPoint Learner!";
return; // Exits the function
}
3. Multiple return StatementsA function may consist of multiple return statements, which we generally get to see within conditional statements. int max(int a, int b) {
if (a > b)
return a;
else
return b;
}
4. Returning ObjectsFunctions can return objects, which can be useful for returning multiple values encapsulated in a class or struct. struct point {
int x, y;
};
point getOrigin() {
return {0, 0};
}
5. Early ExitThe return statement can be used to exit a function early, which is useful for error handling or special conditions. int divideInteger(int a, int b) {
if (b == 0) {
cer << "Error: Division by zero!" << endl;
return -1; // Shows an error
}
return a / b;
}
Return Types and Value Handling in C++In C++, the return type of a function determines what kind of value (if any) a function will return to the caller. Proper handling of return types and values is important for making sure that functions behave as expected and integrate smoothly with other parts of the program. 1. Primitive Data TypesPrimitive data types are the basic built-in types provided by C++. Common examples are like int, float, double, char etc. Example#include <iostream>
using namespace std;
// created a function which returns an integer
int getSquare(int num) {
return num * num;
}
int main() {
int value = 5;
int result = getSquare(value); // Calling the function and storing its result
cout << "The square of " << value << " is " << result << endl;
return 0;
}
OutputThe square of 5 is 25 2. User-Defined TypesUser-defined types include structs and classes. These types allow you to define complex data structures and customize them as per your specific requirements. Example with Struct#include <iostream>
using namespace std;
struct Point {
int x;
int y;
};
Point createPoint(int x, int y) {
Point p;
p.x = x;
p.y = y;
return p; // will returns a Point object
}
int main() {
Point p = createPoint(10, 20);
cout << "Point coordinates: (" << p.x << ", " << p.y << ")" << endl;
return 0;
}
OutputPoint coordinates: (10, 20) Example with Classes#include <iostream>
using namespace std;
class rectangle {
public:
rectangle(int w, int h) : width(w), height(h) {}
int getArea() const {
return width * height;
}
private:
int width;
int height;
};
rectangle createRectangle(int width, int height) {
return rectangle(width, height); // Returns a Rectangle object
}
int main() {
rectangle rect = createRectangle(10, 5);
cout << "Area of given Rectangle is: " << rect.getArea() << endl;
return 0;
}
OutputArea of given Rectangle is: 50 3. References and PointersReferences and pointers are used to refer to variables or objects without making any copies. Which can be useful for efficiency and and easy to modify the original data when needed. Returning by Reference#include <iostream>
using namespace std;
int globalValue = 100;
int& getGlobalValue() {
return globalValue; // Returns a reference to the global variable
}
int main() {
int& ref = getGlobalValue();
ref = 200; // Modifies the global variable
cout << "Global Value: " << globalValue << endl;
return 0;
}
OutputGlobal Value: 200 Returning by Pointer#include <iostream>
using namespace std;
int* createArray(int size) {
int* array = new int[size]; // Allocating memory dynamically
for (int i = 0; i < size; ++i) {
array[i] = i * 10;
}
return array; // Returning a pointer to the allocated array
}
int main() {
int* myArray = createArray(5);
for (int i = 0; i < 5; ++i) {
cout << myArray[i] << " ";
}
delete[] myArray; // Free dynamically allocated memory
return 0;
}
Output0 10 20 30 40 Advertisements
|
| You are visitor 01402503 — GeezerVison does not store the pages you look up. Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required. |