C के Return Values
In this page:
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
// 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;
}
Login to try C/C++/Java/PHP code in the editor
Integers Return करना
एक function के return type को int declare करने का मतलब है यह अपने caller को एक whole number वापस देता है, जो counts, indexes, या integer arithmetic के results compute करने वाले functions के लिए standard choice है।
उदाहरण: Returning Integers
// 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;
}
Login to try C/C++/Java/PHP code in the editor
Floating-Point Numbers Return करना
जब एक calculation को fractional precision चाहिए, function का return type float या double declare करना इसे एक whole number तक round होने पर मजबूर होने की बजाय एक decimal result वापस देने देता है।
उदाहरण: Returning Floating-Point Numbers
// 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;
}
Login to try C/C++/Java/PHP code in the editor
Characters Return करना
एक function एक single character result वापस देने के लिए char return कर सकता है -- ऐसे functions के लिए useful जो input classify करते हैं या एक numeric score के आधार पर एक grade letter जैसा कुछ compute करते हैं।
उदाहरण: Returning Characters
// 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;
}
Login to try C/C++/Java/PHP code in the editor
Expressions में Returned Values इस्तेमाल करना
एक function की returned value सीधे एक बड़े expression के अंदर इस्तेमाल की जा सकती है -- जैसे total = add(3, 4) * 2; -- पहले इसे एक intermediate variable में store किए बिना, क्योंकि call खुद उस returned value में evaluate होता है।
उदाहरण: Using Returned Values in Expressions
// 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;
}
Login to try C/C++/Java/PHP code in the editor
- एक non-void function में एक
returnstatement भूल जाना। - एक local variable का pointer return करना, जो function खत्म होने के बाद exist ही नहीं करता।
return a, b;से दो values return करने की कोशिश करना, जो सिर्फbreturn करता है।
Chapter Quiz — Complete all 8 topics to unlock
0/8 topics done
Complete these topics first: