← Back to C Course | Chapter 5: Functions | Lesson 3 of 8

C के Return Values

एक return value वह answer है जो एक function खत्म होने पर वापस देता है, एक vending machine के आपको एक snack देने जैसा। फिर आप उस answer को अपने program के बाकी हिस्से में इस्तेमाल कर सकते हैं।
Syntax
c
return_type function_name(parameters) {
  return value;
}

void function_name() {
  return;  // optional, returns nothing
}

return Statement

return statement तुरंत एक function का execution खत्म कर देता है और, अगर function का return type non-void है, इसे call करने वाले code को एक value वापस भेजता है -- उस function में return के बाद के statements कभी नहीं चलते।

उदाहरण: The return Statement

c
// Include standard input/output (printf, scanf)
#include <stdio.h>
int getFive() {
	// Send 5 back to the caller
	return 5;
	// Print formatted text to the screen
	printf("never runs");
}
// Program execution starts in main()
int main() {
	// Print formatted text to the screen
	printf("%d", getFive());
	// Return 0 to signal that the program finished successfully
	return 0;
}

Integers Return करना

एक function के return type को int declare करने का मतलब है यह अपने caller को एक whole number वापस देता है, जो counts, indexes, या integer arithmetic के results compute करने वाले functions के लिए standard choice है।

उदाहरण: Returning Integers

c
// Include standard input/output (printf, scanf)
#include <stdio.h>
int add(int a, int b) {
	// Send a + b back to the caller
	return a + b;
}
// Program execution starts in main()
int main() {
	// Print formatted text to the screen
	printf("%d", add(2, 3));
	// Return 0 to signal that the program finished successfully
	return 0;
}

Floating-Point Numbers Return करना

जब एक calculation को fractional precision चाहिए, function का return type float या double declare करना इसे एक whole number तक round होने पर मजबूर होने की बजाय एक decimal result वापस देने देता है।

उदाहरण: Returning Floating-Point Numbers

c
// Include standard input/output (printf, scanf)
#include <stdio.h>
double divide(int a, int b) {
	// Send (double)a / b back to the caller
	return (double)a / b;
}
// Program execution starts in main()
int main() {
	// Print formatted text to the screen
	printf("%.2f", divide(7, 2));
	// Return 0 to signal that the program finished successfully
	return 0;
}

Characters Return करना

एक function एक single character result वापस देने के लिए char return कर सकता है -- ऐसे functions के लिए useful जो input classify करते हैं या एक numeric score के आधार पर एक grade letter जैसा कुछ compute करते हैं।

उदाहरण: Returning Characters

c
// Include standard input/output (printf, scanf)
#include <stdio.h>
char getGrade(int score) {
	// Send (score >= 90) ? 'A' : 'B' back to the caller
	return (score >= 90) ? 'A' : 'B';
}
// Program execution starts in main()
int main() {
	// Print formatted text to the screen
	printf("%c", getGrade(95));
	// Return 0 to signal that the program finished successfully
	return 0;
}

Expressions में Returned Values इस्तेमाल करना

एक function की returned value सीधे एक बड़े expression के अंदर इस्तेमाल की जा सकती है -- जैसे total = add(3, 4) * 2; -- पहले इसे एक intermediate variable में store किए बिना, क्योंकि call खुद उस returned value में evaluate होता है।

उदाहरण: Using Returned Values in Expressions

c
// Include standard input/output (printf, scanf)
#include <stdio.h>
int add(int a, int b) {
	// Send a + b back to the caller
	return a + b;
}
// Program execution starts in main()
int main() {
	// Declare total and set it to add(3, 4) * 2
	int total = add(3, 4) * 2;
	// Print formatted text to the screen
	printf("%d", total);
	// 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 भूल जाना।
  2. एक local variable का pointer return करना, जो function खत्म होने के बाद exist ही नहीं करता।
  3. return a, b; से दो values return करने की कोशिश करना, जो सिर्फ b return करता है।
🔒

Chapter Quiz — Complete all 8 topics to unlock

0/8 topics done

Complete these topics first:

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.