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

C++ Functions का परिचय

एक function steps का एक named set है जिसे आप बार-बार इस्तेमाल कर सकते हैं, एक recipe की तरह। आप इसे एक बार लिखते हैं और जब चाहें इसे call करते हैं।
Syntax
cpp
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

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

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

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

बिना 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

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

कई Functions

एक program को कई छोटे functions में तोड़ना, हर एक एक specific task handle करते हुए, और कुछ functions को दूसरों को call करने देना, logic के individual pieces को focused और isolation में test करना आसान रखता है।

यह एक लंबे, unbroken block of instructions की बजाय maintainable code लिखने की foundation है।

उदाहरण: Multiple Functions

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

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

cpp
// 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;
}
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. एक function को इसके declared होने से पहले call करना, जो एक prototype add किए बिना fail होता है।
  2. एक non-void function में return भूल जाना, जो undefined behavior है।
  3. call करते समय void foo(); लिखना और parentheses भूल जाना, जैसे foo;, जो कुछ नहीं करता।

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.