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

C के Function Prototypes

एक function prototype लिखे जाने से पहले एक function के नाम और inputs की announcement है, एक table of contents entry की तरह। यह C को function के exist करने के बारे में बताता है।
Syntax
c
return_type function_name(type param1, type param2);  // prototype

return_type function_name(type param1, type param2) {  // definition
  // body
}

Function Prototype क्या है?

एक function prototype compiler को एक function का return type, नाम, और parameter types declare करता है इससे पहले कि function की पूरी body file में बाद में दिखे, ताकि compiler को पता हो कि इसकी पहले की किसी भी calls को सही तरीके से कैसे interpret करना है।

उदाहरण: What is a Function Prototype?

c
// Include standard input/output (printf, scanf)
#include <stdio.h>
int add(int a, int 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;
}
int add(int a, int b) {
	// Send a + b back to the caller
	return a + b;
}

Prototypes क्यों इस्तेमाल करें?

क्योंकि C compilers एक file को top से bottom पढ़ते हैं, इसकी definition दिखने से पहले एक function call करना अन्यथा एक error cause करेगा -- prototypes compiler को function के बारे में पहले ही काफी information देकर इसे solve करते हैं।

उदाहरण: Why Use Prototypes?

c
// Include standard input/output (printf, scanf)
#include <stdio.h>
int square(int 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;
}
int square(int n) {
	// Send n * n back to the caller
	return n * n;
}

Prototype Syntax

एक prototype function के header को exactly वैसे ही दोहराता है जैसे यह definition में दिखेगा, लेकिन एक body की बजाय एक semicolon से खत्म होता है, और इसके अंदर parameter names optional हैं क्योंकि compiler को actually सिर्फ उनके types से मतलब है।

उदाहरण: Prototype Syntax

c
// Include standard input/output (printf, scanf)
#include <stdio.h>
int add(int, int);
// Program execution starts in main()
int main() {
	// Print formatted text to the screen
	printf("%d", add(3, 4));
	// Return 0 to signal that the program finished successfully
	return 0;
}
int add(int a, int b) {
	// Send a + b back to the caller
	return a + b;
}

Compiler Warnings रोकना

Modern compilers हर function call को उसके prototype के declared parameter types और count के against check करते हैं, mismatched arguments को एक runtime bug की तरह slip करने देने की बजाय एक compile error की तरह पकड़ते हुए।

उदाहरण: Preventing Compiler Warnings

c
// Include standard input/output (printf, scanf)
#include <stdio.h>
int add(int a, int 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;
}
int add(int a, int b) {
	// Send a + b back to the caller
	return a + b;
}

कई Prototypes

एक program के सारे function prototypes को file के top के पास साथ list करना standard C practice है, file पढ़ने वाले किसी को भी implementations में जाने से पहले इसमें defined हर function का एक quick overview देते हुए।

उदाहरण: Multiple Prototypes

c
// Include standard input/output (printf, scanf)
#include <stdio.h>
int add(int a, int b);
int square(int n);
// Program execution starts in main()
int main() {
	// Print formatted text to the screen
	printf("%d %d", add(2, 3), square(4));
	// Return 0 to signal that the program finished successfully
	return 0;
}
int add(int a, int b) {
	// Send a + b back to the caller
	return a + b;
}
int square(int n) {
	// Send n * n back to the caller
	return n * n;
}
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. main के बाद defined एक function को बिना prototype के call करना, जो एक implicit declaration warning या error देता है।
  2. एक prototype लिखना जिसके parameter types definition से अलग हों, ताकि compiler conflicting types report करे।
  3. एक prototype line के अंत में semicolon भूल जाना, जो एक syntax error cause करता है।
🔒

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.