C++ Functions का परिचय
In this page:
return_type function_name(); // declaration
return_type function_name() { // definition
// body
}
function_name(); // call
Functions Define और Call करना
एक function code के एक block को एक नाम के तहत bundle करता है ताकि यह सिर्फ explicitly call होने पर चले, आपकी file के बाकी हिस्से की तरह automatically top to bottom execute होने की बजाय।
void greet() { cout << "Hi"; } जैसा एक function define करना और फिर main() के अंदर greet(); call करना वह code trigger करने का तरीका है।
उदाहरण: Defining and Calling Functions
// Include std::cout and std::cin
#include <iostream>
void greet() {
// Print to the console with cout
std::cout << "Hello from a function!" << std::endl;
}
// Program execution starts in main()
int main() {
greet();
// Return 0 to signal that the program finished successfully
return 0;
}
Login to try C/C++/Java/PHP code in the editor
Declaration बनाम Definition
क्योंकि C++ एक file को top से bottom पढ़ता है, इसकी पूरी definition आगे appear होने से पहले एक function call करना normally fail होगा — top पर एक function prototype (declaration) compiler को बताता है कि function exist करता है और यह कैसा दिखता है, आपको actual body file में बाद में define करने देते हुए।
उदाहरण: Declaration vs Definition
// Include std::cout and std::cin
#include <iostream>
void greet();
// Program execution starts in main()
int main() {
greet();
// Return 0 to signal that the program finished successfully
return 0;
}
void greet() {
// Print to the console with cout
std::cout << "Called before its full definition" << std::endl;
}
Login to try C/C++/Java/PHP code in the editor
बिना Arguments वाले Functions
Functions को बिल्कुल कोई parameters की ज़रूरत नहीं अगर उन्हें अपना काम करने के लिए बाहरी data न चाहिए हो — void showMenu() जैसा एक function जो बस options का एक fixed set print करता है एक common example है।
ये parameterless functions logging या static text display करने जैसे repeated, self-contained actions के लिए खासकर handy हैं।
उदाहरण: Functions with No Arguments
// Include std::cout and std::cin
#include <iostream>
void showMenu() {
// Print to the console with cout
std::cout << "1. Start" << std::endl;
// Print to the console with cout
std::cout << "2. Exit" << std::endl;
}
// Program execution starts in main()
int main() {
showMenu();
// Return 0 to signal that the program finished successfully
return 0;
}
Login to try C/C++/Java/PHP code in the editor
कई Functions
एक program को कई छोटे functions में तोड़ना, हर एक एक specific task handle करते हुए, और कुछ functions को दूसरों को call करने देना, logic के individual pieces को focused और isolation में test करना आसान रखता है।
यह एक लंबे, unbroken block of instructions की बजाय maintainable code लिखने की foundation है।
उदाहरण: Multiple Functions
// Include std::cout and std::cin
#include <iostream>
void printHeader() {
// Print to the console with cout
std::cout << "=== Report ===" << std::endl;
}
void printBody() {
// Print to the console with cout
std::cout << "Report contents" << std::endl;
}
// Program execution starts in main()
int main() {
printHeader();
printBody();
// Return 0 to signal that the program finished successfully
return 0;
}
Login to try C/C++/Java/PHP code in the editor
Local बनाम Global Variables
एक function के अंदर declared एक variable local है और सिर्फ उस function के चलते हुए exist करता है, किसी दूसरे function को invisible — जबकि हर function के बाहर declared एक variable global है और file में कहीं से भी पढ़ा (और modify) जा सकता है।
Global variables पर heavily depend करना bugs को track करना मुश्किल बनाता है, क्योंकि कोई भी function shared value बदलने वाला हो सकता है।
उदाहरण: Local vs Global Variables
// Include std::cout and std::cin
#include <iostream>
// Declare globalCount and set it to 100
int globalCount = 100;
void showLocal() {
// Declare localCount and set it to 5
int localCount = 5;
// Print to the console with cout
std::cout << "Local: " << localCount << std::endl;
}
// Program execution starts in main()
int main() {
showLocal();
// Print to the console with cout
std::cout << "Global: " << globalCount << std::endl;
// Return 0 to signal that the program finished successfully
return 0;
}
Login to try C/C++/Java/PHP code in the editor
- एक function को इसके declared होने से पहले call करना, जो एक prototype add किए बिना fail होता है।
- एक non-void function में
returnभूल जाना, जो undefined behavior है। - call करते समय
void foo();लिखना और parentheses भूल जाना, जैसेfoo;, जो कुछ नहीं करता।
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