C Functions परिचय
In this page:
return_type function_name(parameters) {
// body
}
function_name(arguments); // call
Function क्या है?
एक function एक specific task को reusable, named code के एक block में bundle करता है, ताकि आप इसे दोबारा type करने की बजाय program में कई जगहों से same logic call कर सकें, और कई जगह की बजाय एक जगह एक bug fix कर सकें।
उदाहरण: What is a Function?
// Include standard input/output (printf, scanf)
#include <stdio.h>
void greet() {
// Print formatted text to the screen
printf("Hello");
}
// Program execution starts in main()
int main() {
greet();
greet();
// Return 0 to signal that the program finished successfully
return 0;
}
Login to try C/C++/Java/PHP code in the editor
एक Function Define करना
एक function define करने का मतलब है इसका return type, एक unique name, और curly braces में एक body लिखना जिसमें वे statements हों जो यह चलाता है -- compiler exactly यह जानने के लिए इस definition का इस्तेमाल करता है कि function क्या करता है और क्या उम्मीद करता है।
उदाहरण: Defining a Function
// Include standard input/output (printf, scanf)
#include <stdio.h>
int square(int n) {
// Send n * n back to the caller
return n * n;
}
// Program execution starts in main()
int main() {
// Print formatted text to the screen
printf("%d", square(4));
// Return 0 to signal that the program finished successfully
return 0;
}
Login to try C/C++/Java/PHP code in the editor
एक Function Call करना
एक function call करना -- इसका नाम parentheses के साथ लिखना -- control को उस function की body में transfer करता है; एक बार यह खत्म हो जाए, execution वहीं resume होता है जहाँ call किया गया था, optionally एक value वापस मिलने के साथ।
उदाहरण: Calling a Function
// Include standard input/output (printf, scanf)
#include <stdio.h>
int square(int n) {
// Send n * n back to the caller
return n * n;
}
// Program execution starts in main()
int main() {
// Declare result and set it to square(5)
int result = square(5);
// Print formatted text to the screen
printf("%d", result);
// Return 0 to signal that the program finished successfully
return 0;
}
Login to try C/C++/Java/PHP code in the editor
Void Functions
एक void function एक action perform करता है (जैसे कुछ print करना या एक variable update करना) इसे call करने वाले को कोई value वापस दिए बिना -- return type के तौर पर void keyword explicitly signal करता है "कुछ वापस नहीं आता।"
उदाहरण: Void Functions
// Include standard input/output (printf, scanf)
#include <stdio.h>
void printMessage() {
// Print formatted text to the screen
printf("This function returns nothing");
}
// 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
कई Helper Functions
एक program को कई छोटे helper functions में बांटना, हर एक एक clear task के लिए responsible, हर piece को independently test और debug करना आसान बनाता है, code के एक विशाल block के अंदर एक bug ढूंढने की बजाय।
उदाहरण: Multiple Helper Functions
// Include standard input/output (printf, scanf)
#include <stdio.h>
int square(int n) {
// Send n * n back to the caller
return n * n;
}
int cube(int n) {
// Send n * n * n back to the caller
return n * n * n;
}
// Program execution starts in main()
int main() {
// Print formatted text to the screen
printf("%d %d", square(3), cube(3));
// Return 0 to signal that the program finished successfully
return 0;
}
Login to try C/C++/Java/PHP code in the editor
- एक function को इसके declare होने से पहले call करना, जो एक implicit declaration warning या error cause करता है।
- एक non-void function में
returnभूल जाना, जो result इस्तेमाल होने पर undefined behavior है। foo();की बजायfoo;लिखना, जो function call नहीं करता।
Chapter Quiz — Complete all 8 topics to unlock
0/8 topics done
Complete these topics first: