Introduction to User Defined Function in Hindi
/ BCA / Programming with C and CPP
Introduction and Comparison of User Defined and Library Functions in C in Hindi
Introduction to User Defined Function in Hindi
What is User Defined Function?
जब हम किसी ऐसे function को खुद बनाते हैं जो किसी विशेष task को करने के लिए बार-बार उपयोग में आए, तो उसे User Defined Function कहा जाता है। C language में पहले से कुछ functions होते हैं जिन्हें हम library functions कहते हैं, लेकिन कई बार हमें अपनी जरूरतों के अनुसार functions बनाने होते हैं जिन्हें हम खुद define करते हैं।
Why Use User Defined Function?
- Code को modular और आसान बनाना
- Repeated code से बचना
- Logic को अलग-अलग parts में divide करना
- Debugging और maintenance को आसान बनाना
Structure of User Defined Function
एक User Defined Function को तीन भागों में बांटा जाता है:
- Function Declaration
- Function Definition
- Function Call
Syntax:
return_type function_name(parameter_list); // Declaration
return_type function_name(parameter_list) // Definition
{
// function body
}
function_name(arguments); // Call
Example:
#include<stdio.h>
int add(int a, int b); // Function Declaration
int main() {
int result = add(5, 3); // Function Call
printf("Sum = %d", result);
return 0;
}
int add(int a, int b) { // Function Definition
return a + b;
}
How Library Function Works in C in Hindi
What is Library Function?
Library Functions वे predefined functions होते हैं जो C की standard libraries में पहले से मौजूद होते हैं। इन functions को हम सीधे use कर सकते हैं और इसके लिए हमें केवल संबंधित header file को include करना होता है।
Library Functions कहां से आते हैं?
C language में कई standard header files होती हैं जैसे stdio.h, math.h, string.h आदि, जिनमें बहुत सारे useful functions predefined होते हैं।
Example of Library Function:
printf()- output देने के लिएscanf()- input लेने के लिएstrlen()- string की length के लिएsqrt()- square root के लिए
Working of Library Function:
जब हम किसी library function को call करते हैं, तो उसका definition internally उस header file में होता है जिसे हम include करते हैं। Compiler उस function को पहचानता है और उसकी functionality को perform करता है।
Example:
#include<stdio.h>
#include<math.h>
int main() {
double result = sqrt(25.0); // Library function
printf("Square Root = %lf", result);
return 0;
}
Difference between User Defined and Library Function in Hindi
| Aspect | User Defined Function | Library Function |
|---|---|---|
| Definition | Programmer द्वारा बनाया गया function | Predefined function जो C libraries में होता है |
| Header File | कोई विशेष header file जरूरी नहीं | Specific header file जरूरी होती है |
| Control | Programmer खुद logic लिखता है | Logic पहले से fixed होता है |
| Flexibility | Highly flexible, logic modify किया जा सकता है | Logic को modify नहीं किया जा सकता |
| Use | Specific user requirement के अनुसार | General purpose के लिए |
Examples of Library Function and User Defined Function in Hindi
Examples of Library Functions:
printf()- स्क्रीन पर data दिखाने के लिएscanf()- user से input लेने के लिएstrlen()- string की लंबाई बताने के लिएstrcpy()- एक string को दूसरी में copy करने के लिएsqrt()- square root निकालने के लिए
Examples of User Defined Functions:
#include<stdio.h>
void greet(); // Declaration
int main() {
greet(); // Call
return 0;
}
void greet() { // Definition
printf("Welcome to User Defined Function Example!");
}
ऊपर दिए गए program में हमने एक simple function बनाया है जो message print करता है। यह function user द्वारा define किया गया है, इसलिए इसे user defined function कहा जाएगा।
#include<stdio.h>
int square(int n); // Declaration
int main() {
int num = 5;
printf("Square = %d", square(num));
return 0;
}
int square(int n) { // Definition
return n * n;
}
यह function किसी भी number का square निकालने का काम करता है। इसे भी हम अपने हिसाब से बना सकते हैं और इसे बार-बार use कर सकते हैं।
Final Notes:
- Library Functions हमें convenience देते हैं और code को छोटा बनाते हैं
- User Defined Functions flexibility और reusability बढ़ाते हैं
- दोनों का सही उपयोग करना अच्छे programmer की पहचान है