Introduction
जब हम C language में functions का उपयोग करते हैं, तो compiler को यह पहले से पता होना चाहिए कि कौन सा function बाद में use होने वाला है।
अगर हम function को main() के बाद define करते हैं और बिना बताए उसे पहले call कर देते हैं, तो compiler error दे सकता है।
इस समस्या को solve करने के लिए C language में Function Prototype का उपयोग किया जाता है।
Function prototype compiler को पहले ही यह जानकारी दे देता है कि:
- function का नाम क्या है
- वह कौन सा data return करेगा
- उसमें कितने और किस प्रकार के arguments होंगे
इससे compiler program को सही तरीके से समझ पाता है और execution में error नहीं आता।
Definition
Function Prototype एक declaration होता है जो function की information (name, return type, parameters) को function के actual definition से पहले define करता है।
Syntax
return_type function_name(parameter_types);
Example 1: Without Prototype (Problem)
#include <stdio.h>
int main()
{
int result = add(2, 3);
printf("%d", result);
return 0;
}
int add(int a, int b)
{
return a + b;
}
Explanation:
- यहाँ function main() के बाद define है
- compiler को पहले जानकारी नहीं थी
- कुछ compilers में error या warning आ सकती है
Example 2: Using Function Prototype
#include <stdio.h>
int add(int, int); // function prototype
int main()
{
int result = add(2, 3);
printf("%d", result);
return 0;
}
int add(int a, int b)
{
return a + b;
}
Output:
5
Explanation:
- पहले prototype दिया गया
- compiler को पहले ही function की जानकारी मिल गई
- इसलिए function call correctly execute हुआ
Example 3: Prototype with Different Function
#include <stdio.h>
void display(); // prototype
int main()
{
display();
return 0;
}
void display()
{
printf("Hello LearnBCA Student");
}
Output:
Hello LearnBCA Student
Explanation:
- prototype पहले define हुआ
- function बाद में define हुआ
- program successfully execute हुआ
Important Points
- Prototype में parameter names optional होते हैं
- केवल data types लिखना जरूरी है
- Prototype हमेशा main() से पहले लिखा जाता है
- यह compiler को function की advance information देता है
Technical Understanding
Function prototype का use इन situations में होता है:
- जब function main() के बाद define हो
- large programs में modular design के लिए
- multiple files वाले programs में
Exam Points
- Function prototype = function declaration
- Compiler को पहले से information देता है
- Parameter names optional होते हैं
- main() से पहले लिखना चाहिए
- Definition और prototype में difference पूछा जाता है