Functions in C Language सी लैंग्वेज में फंक्शन
In C language, a function is a group of programming statements written together to perform a specific task. When a function is called from a program, its statements are executed. This process is known as function calling.
सी लैंग्वेज में, फंक्शन स्टेटमेंट्स का एक समूह होता है, जिन्हें किसी निश्चित कार्य को पूरा करने के लिए एक साथ लिखा जाता है। जब किसी प्रोग्राम में फंक्शन को कॉल किया जाता है, तब उसके स्टेटमेंट्स एक्सीक्यूट होते हैं। इस प्रक्रिया को फंक्शन कॉलिंग कहा जाता है।
Types of Function फंक्शन के प्रकार
There are two types of functions in C language:
सी लैंग्वेज में फंक्शन दो प्रकार के होते हैं:
1. Library Function / Built-in Function
लाइब्रेरी फंक्शन / बिल्ट-इन फंक्शन
Library functions are predefined functions provided by the C standard library or other libraries. These functions perform commonly required tasks, such as input/output operations, mathematical calculations, string handling, and memory management.
लाइब्रेरी फंक्शन पहले से परिभाषित फंक्शन होते हैं, जो C की स्टैंडर्ड लाइब्रेरी या अन्य लाइब्रेरी द्वारा उपलब्ध कराए जाते हैं। ये फंक्शन सामान्यतः आवश्यक कार्यों जैसे इनपुट/आउटपुट, गणितीय गणना, स्ट्रिंग हैंडलिंग तथा मेमोरी मैनेजमेंट के लिए प्रयोग किए जाते हैं।
The declarations of many standard library functions are provided in header files. A programmer can access these functions by including the appropriate header file in the program using the #include preprocessor directive.
कई स्टैंडर्ड लाइब्रेरी फंक्शन्स के डिक्लेरेशन उनकी संबंधित हैडर फाइल्स में उपलब्ध होते हैं। प्रोग्रामर #include प्रीप्रोसेसर डायरेक्टिव की सहायता से आवश्यक हैडर फाइल को प्रोग्राम में शामिल करके इन फंक्शन्स का प्रयोग कर सकता है।
The function implementation is provided by the library, and the linker connects the required library code with the program. The programmer normally uses library functions without writing their internal implementation.
फंक्शन की वास्तविक इम्प्लीमेंटेशन लाइब्रेरी में उपलब्ध होती है तथा लिंकर आवश्यक लाइब्रेरी कोड को प्रोग्राम से जोड़ता है। प्रोग्रामर सामान्यतः लाइब्रेरी फंक्शन्स की आंतरिक परिभाषा लिखने के बजाय उनका सीधे प्रयोग करता है।
Library functions are very useful because they allow programmers to focus on solving the main problem instead of writing the same common operations repeatedly.
लाइब्रेरी फंक्शन्स अत्यंत उपयोगी होते हैं क्योंकि इनके प्रयोग से प्रोग्रामर सामान्य कार्यों के लिए बार-बार कोड लिखने के बजाय मुख्य समस्या के समाधान पर ध्यान केंद्रित कर सकता है।
Examples:
printf(), scanf(), sqrt(), pow(), strlen(), fopen() etc.
Note: getch() and clrscr() are commonly used in older Turbo C environments, but they are not standard C library functions.
2. User-Defined Function
यूजर डिफाइन्ड फंक्शन
A user-defined function is a function created by the programmer according to the requirements of a program. It is used to perform a specific task and helps divide a large program into smaller and manageable parts.
यूजर डिफाइन्ड फंक्शन वह फंक्शन होता है जिसे प्रोग्रामर प्रोग्राम की आवश्यकतानुसार स्वयं तैयार करता है। इसका प्रयोग किसी निश्चित कार्य को पूरा करने के लिए किया जाता है तथा यह बड़े प्रोग्राम को छोटे एवं व्यवस्थित भागों में विभाजित करने में सहायता करता है।
C language allows programmers to create different functions for different tasks. A program may contain several user-defined functions, but its execution in a hosted C environment starts from the main() function.
सी लैंग्वेज में प्रोग्रामर अलग-अलग कार्यों के लिए कई फंक्शन्स बना सकता है। एक प्रोग्राम में अनेक यूजर डिफाइन्ड फंक्शन्स हो सकते हैं, लेकिन सामान्य C प्रोग्राम का एक्सीक्यूशन main() फंक्शन से प्रारंभ होता है।
Declaration and Definition of a Function
फंक्शन का डिक्लेरेशन एवं डेफिनिशन
The declaration and definition of a function include the following major components:
फंक्शन के डिक्लेरेशन एवं डेफिनिशन में निम्न मुख्य अवयव होते हैं:
Return type: Specifies the type of value returned by the function.
फंक्शन द्वारा रिटर्न की जाने वाली वैल्यू का टाइप।Function name: The name used to identify and call the function.
फंक्शन को पहचानने एवं कॉल करने के लिए उसका नाम।Parameters: The input values received by the function.
फंक्शन द्वारा प्राप्त किए जाने वाले इनपुट मान।Function body: The statements that perform the required task. It is included in the function definition.
फंक्शन की बॉडी में वे स्टेटमेंट्स होते हैं जो आवश्यक कार्य को पूरा करते हैं। यह फंक्शन की डेफिनिशन में शामिल होती है।
Function Declaration / Prototype
फंक्शन डिक्लेरेशन / प्रोटोटाइप
A function prototype informs the compiler about the function's name, return type, and parameter types before the function is called.
फंक्शन प्रोटोटाइप, फंक्शन को कॉल करने से पहले कम्पाइलर को उसके नाम, रिटर्न टाइप तथा पैरामीटर्स के टाइप के बारे में जानकारी देता है।
Syntax:
return_type function_name(parameter_types);Function Definition
फंक्शन डेफिनिशन
The function definition contains the actual statements that perform the required task.
फंक्शन डेफिनिशन में वे वास्तविक स्टेटमेंट्स होते हैं जो आवश्यक कार्य को पूरा करते हैं।
Syntax:
return_type function_name(parameters)
{
statements;
return value;
}If the function's return type is void, it does not return a value.
यदि फंक्शन का रिटर्न टाइप void है, तो वह कोई वैल्यू रिटर्न नहीं करता है।
Position of Function Definition
फंक्शन डेफिनिशन का स्थान
If a function is defined before main(), its definition is already available before it is called. Therefore, a separate function prototype is generally not required.
यदि किसी फंक्शन को main() से पहले डिफाइन किया जाता है, तो कॉल किए जाने से पहले उसकी डेफिनिशन उपलब्ध होती है। इसलिए सामान्यतः अलग से फंक्शन प्रोटोटाइप लिखने की आवश्यकता नहीं होती है।
If a function is defined after main(), its prototype should be declared before it is called from main().
यदि किसी फंक्शन को main() के बाद डिफाइन किया जाता है, तो main() से उसे कॉल करने से पहले उसका प्रोटोटाइप डिक्लेयर करना चाहिए।
Example
C Program to Calculate Factorial of a Number Using Function
फंक्शन की सहायता से किसी संख्या का फैक्टोरियल ज्ञात करने का प्रोग्राम
#include <stdio.h>
/* Function prototype */
int factorial(int n);
int main(void)
{
int num, result;
printf("Enter a number: ");
scanf("%d", &num);
result = factorial(num);
printf("Factorial = %d", result);
return 0;
}
/* Function definition */
int factorial(int n)
{
int i, fact = 1;
for (i = 1; i <= n; i++)
{
fact = fact * i;
}
return fact;
}Output:
Enter a number: 5
Factorial = 120Explanation
व्याख्या
In this program, factorial() is a user-defined function that calculates the factorial of a given number.
इस प्रोग्राम में factorial() एक यूजर डिफाइन्ड फंक्शन है, जो दी गई संख्या का फैक्टोरियल ज्ञात करता है।
The function prototype int factorial(int n); declares the function before it is called from main().
फंक्शन प्रोटोटाइप int factorial(int n); को main() से फंक्शन कॉल करने से पहले डिक्लेयर किया गया है।
The statement result = factorial(num); calls the function and passes the value of num as an argument.
स्टेटमेंट result = factorial(num); फंक्शन को कॉल करता है तथा num की वैल्यू को आर्गुमेंट के रूप में पास करता है।
The function receives this value through parameter n. The for loop calculates the factorial, and the return fact; statement returns the calculated result to main().
फंक्शन इस वैल्यू को पैरामीटर n के माध्यम से प्राप्त करता है। for लूप फैक्टोरियल की गणना करता है तथा return fact; स्टेटमेंट प्राप्त परिणाम को main() में वापस भेज देता है।
For example:
5! = 5 × 4 × 3 × 2 × 1 = 120
Note: This program assumes that the user enters a non-negative integer small enough for the int data type. The factorial of 0 is 1.
Comments
Post a Comment