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

C Functions परिचय

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

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

एक Function Define करना

एक function define करने का मतलब है इसका return type, एक unique name, और curly braces में एक body लिखना जिसमें वे statements हों जो यह चलाता है -- compiler exactly यह जानने के लिए इस definition का इस्तेमाल करता है कि function क्या करता है और क्या उम्मीद करता है।

उदाहरण: Defining a Function

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

एक Function Call करना

एक function call करना -- इसका नाम parentheses के साथ लिखना -- control को उस function की body में transfer करता है; एक बार यह खत्म हो जाए, execution वहीं resume होता है जहाँ call किया गया था, optionally एक value वापस मिलने के साथ।

उदाहरण: Calling a Function

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

Void Functions

एक void function एक action perform करता है (जैसे कुछ print करना या एक variable update करना) इसे call करने वाले को कोई value वापस दिए बिना -- return type के तौर पर void keyword explicitly signal करता है "कुछ वापस नहीं आता।"

उदाहरण: Void Functions

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

कई Helper Functions

एक program को कई छोटे helper functions में बांटना, हर एक एक clear task के लिए responsible, हर piece को independently test और debug करना आसान बनाता है, code के एक विशाल block के अंदर एक bug ढूंढने की बजाय।

उदाहरण: Multiple Helper Functions

c
// 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;
}
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 को इसके declare होने से पहले call करना, जो एक implicit declaration warning या error cause करता है।
  2. एक non-void function में return भूल जाना, जो result इस्तेमाल होने पर undefined behavior है।
  3. foo(); की बजाय foo; लिखना, जो function call नहीं करता।
🔒

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.