C++ के Return Values
In this page:
return_type function_name(parameters) {
return value;
}
void function_name() {
return; // optional, returns nothing
}
Values Return करना
एक function return statement से अपने caller को एक result वापस भेजता है, और function का declared return type — जैसे int या double — actually return हो रही value के type से match होना चाहिए।
एक बार return execute हो जाए, function तुरंत खत्म हो जाता है, भले ही इसके बाद और code लिखा हो।
उदाहरण: Returning Values
// Include std::cout and std::cin
#include <iostream>
int square(int n) {
// Send n * n back to the caller
return n * n;
}
// Program execution starts in main()
int main() {
// Print to the console with cout
std::cout << square(4) << std::endl;
// Return 0 to signal that the program finished successfully
return 0;
}
Login to try C/C++/Java/PHP code in the editor
Void Return
एक void function बिल्कुल कुछ return न करने के लिए declared है, जो एक value compute करके वापस देने की बजाय एक action perform करने वाले (जैसे printing) functions के लिए appropriate है।
आप अभी भी void function के अंदर बिना किसी value के एक bare return; लिखकर इसे जल्दी exit कर सकते हैं, जो एक condition के आधार पर execution बीच में रोकने के लिए useful है।
उदाहरण: Void Return
// Include std::cout and std::cin
#include <iostream>
void printMessage() {
// Print to the console with cout
std::cout << "Just performs an action" << std::endl;
return;
}
// Program execution starts in main()
int main() {
printMessage();
// Return 0 to signal that the program finished successfully
return 0;
}
Login to try C/C++/Java/PHP code in the editor
Boolean Values Return करना
bool return करने वाले functions yes/no checks के लिए ideal हैं, जैसे bool isValid(int age), क्योंकि caller बिना किसी extra comparison के result सीधे एक if condition में इस्तेमाल कर सकता है।
यह pattern calling code को लगभग plain English जैसे पढ़ाता है: if (isValid(age))।
उदाहरण: Returning Boolean Values
// Include std::cout and std::cin
#include <iostream>
bool isValid(int age) {
// Send age >= 0 && age <= 120 back to the caller
return age >= 0 && age <= 120;
}
// Program execution starts in main()
int main() {
// Check whether isValid(30)
if (isValid(30)) {
// Print to the console with cout
std::cout << "Valid" << std::endl;
}
// Return 0 to signal that the program finished successfully
return 0;
}
Login to try C/C++/Java/PHP code in the editor
References Return करना
एक reference return करना एक function को इसकी value की एक copy की बजाय एक variable तक direct access वापस देने देता है, लेकिन यह dangerous है अगर वह variable function के लिए local हो — local variables function के return होते ही destroy हो जाते हैं, इसलिए इनमें से एक का reference एक ऐसी memory का dangling reference बन जाता है जो अब exist नहीं करती।
सिर्फ उस data के references return करें जो function call से ज़्यादा जिएगा, जैसे एक reference parameter या एक global variable।
उदाहरण: Returning References
// Include std::cout and std::cin
#include <iostream>
// Declare globalScore and set it to 0
int globalScore = 0;
int &getScore() {
// Send globalScore back to the caller
return globalScore;
}
// Program execution starts in main()
int main() {
getScore() = 42;
// Print to the console with cout
std::cout << globalScore << std::endl;
// Return 0 to signal that the program finished successfully
return 0;
}
Login to try C/C++/Java/PHP code in the editor
Structures Return करना
जब एक function को एक साथ related data का एक से ज़्यादा piece वापस देना हो, उन्हें एक struct में pack करना और वह struct एक single unit की तरह return करना कई output parameters या global variables इस्तेमाल करने से cleaner है।
उदाहरण के लिए, दोनों एक minimum और maximum compute करने वाला एक function एक single struct MinMax { int min; int max; }; return कर सकता है।
उदाहरण: Returning Structures
// Include std::cout and std::cin
#include <iostream>
// Define a structure type named Point
struct Point { int x; int y; };
Point makePoint(int x, int y) {
Point p;
p.x = x;
p.y = y;
// Send p back to the caller
return p;
}
// Program execution starts in main()
int main() {
Point p = makePoint(3, 4);
// Print to the console with cout
std::cout << p.x << "," << p.y << std::endl;
// Return 0 to signal that the program finished successfully
return 0;
}
Login to try C/C++/Java/PHP code in the editor
- एक non-void function में एक
returnstatement भूल जाना, जो undefined behavior है। - exit पर destroy होने वाले एक local variable का pointer या reference return करना।
return a, b;से एक से ज़्यादा value return करने की कोशिश करना, जो सिर्फbreturn करता है।
Chapter Quiz — Complete all 13 topics to unlock
0/13 topics done
Complete these topics first:
- C++ Functions Introduction
- C++ Function Parameters
- C++ Multiple Function Parameters
- C++ Passing by Reference
- C++ Passing Structures to Functions
- C++ Return Values
- C++ Function Overloading
- C++ Default Arguments
- C++ Recursion
- C++ Inline Functions
- C++ Lambda Functions
- C++ Scope & Lifetime
- C++ Math Functions