← Back to C++ Course | Chapter 5: Functions | Lesson 6 of 13

C++ के Return Values

एक return value वह answer है जो एक function खत्म होने पर वापस देता है, एक vending machine के आपको एक snack देने जैसा। एक void function बस अपना काम करता है और कुछ return नहीं करता।
Syntax
cpp
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

cpp
// 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;
}

Void Return

एक void function बिल्कुल कुछ return न करने के लिए declared है, जो एक value compute करके वापस देने की बजाय एक action perform करने वाले (जैसे printing) functions के लिए appropriate है।

आप अभी भी void function के अंदर बिना किसी value के एक bare return; लिखकर इसे जल्दी exit कर सकते हैं, जो एक condition के आधार पर execution बीच में रोकने के लिए useful है।

उदाहरण: Void Return

cpp
// 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;
}

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

cpp
// 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;
}

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

cpp
// 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;
}

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

cpp
// 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;
}
Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the view already swaps in the hi_ translation fields (or blanks these out if untranslated), so this renders correctly for both languages without a lang_code check here. #}
आम गलतियां
  1. एक non-void function में एक return statement भूल जाना, जो undefined behavior है।
  2. exit पर destroy होने वाले एक local variable का pointer या reference return करना।
  3. return a, b; से एक से ज़्यादा value return करने की कोशिश करना, जो सिर्फ b return करता है।

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.