C के Function Prototypes
In this page:
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?
// 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;
}
Login to try C/C++/Java/PHP code in the editor
Prototypes क्यों इस्तेमाल करें?
क्योंकि C compilers एक file को top से bottom पढ़ते हैं, इसकी definition दिखने से पहले एक function call करना अन्यथा एक error cause करेगा -- prototypes compiler को function के बारे में पहले ही काफी information देकर इसे solve करते हैं।
उदाहरण: Why Use Prototypes?
// 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;
}
Login to try C/C++/Java/PHP code in the editor
Prototype Syntax
एक prototype function के header को exactly वैसे ही दोहराता है जैसे यह definition में दिखेगा, लेकिन एक body की बजाय एक semicolon से खत्म होता है, और इसके अंदर parameter names optional हैं क्योंकि compiler को actually सिर्फ उनके types से मतलब है।
उदाहरण: Prototype Syntax
// 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;
}
Login to try C/C++/Java/PHP code in the editor
Compiler Warnings रोकना
Modern compilers हर function call को उसके prototype के declared parameter types और count के against check करते हैं, mismatched arguments को एक runtime bug की तरह slip करने देने की बजाय एक compile error की तरह पकड़ते हुए।
उदाहरण: Preventing Compiler Warnings
// 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;
}
Login to try C/C++/Java/PHP code in the editor
कई Prototypes
एक program के सारे function prototypes को file के top के पास साथ list करना standard C practice है, file पढ़ने वाले किसी को भी implementations में जाने से पहले इसमें defined हर function का एक quick overview देते हुए।
उदाहरण: Multiple Prototypes
// 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;
}
Login to try C/C++/Java/PHP code in the editor
mainके बाद defined एक function को बिना prototype के call करना, जो एक implicit declaration warning या error देता है।- एक prototype लिखना जिसके parameter types definition से अलग हों, ताकि compiler conflicting types report करे।
- एक prototype line के अंत में semicolon भूल जाना, जो एक syntax error cause करता है।
Chapter Quiz — Complete all 8 topics to unlock
0/8 topics done
Complete these topics first: