आज आप पढ़ने वाले हैं Function in C Programming in Hindi में आसान Explanation और Example Programs के साथ। जब भी कोई बड़ा program लिखना होता है, तो उसे छोटे-छोटे हिस्सों में बाँटना पड़ता है — और यही काम करने के लिये Functions का use करते हैं। C programming सीखने वाले students के लिए functions को समझना एक game-changer moment होता है, क्योंकि इसके बाद code लिखना न सिर्फ आसान हो जाता है, बल्कि पूरी तरह organized और professional भी लगने लगता है।
इस blog post में हम function को बिल्कुल शुरुआत से सीखेंगे। आपको किसी भी advanced knowledge की जरूरत नहीं है। हम सरल भाषा में, real life उदाहरणों के साथ और code examples की मदद से functions को step by step समझेंगे।
इस post को पढ़ने के बाद आप जानेंगे कि function क्या होता है, इसे कैसे declare और define करते हैं, इसके कितने प्रकार होते हैं, function में arguments और return values कैसे काम करती हैं, और C programming में functions का उपयोग कहाँ-कहाँ होता है। तो चलिए शुरू करते हैं!
Table of Contents
Function in C Programming क्या होता है?
Function in C Programming एक ऐसा code block होता है जो एक specific काम करने के लिए लिखा जाता है। इसे एक बार लिखो और जितनी बार चाहो उपयोग करो — यही function की सबसे बड़ी खूबी है।
Function के फायदे क्यों हैं?
- Code Reusability — एक ही code को बार-बार लिखने की जरूरत नहीं
- Readability — code साफ और समझने में आसान लगता है
- Easy Debugging — गलती ढूंढना आसान हो जाता है
- Modular Programming — बड़े program को छोटे हिस्सों में बाँटा जा सकता है
- Time की बचत — एक बार लिखो, कई जगह उपयोग करो

Function का Basic Structure
C programming में किसी भी function के तीन हिस्से होते हैं:
1. C Programming Function Declaration (Prototype)
Function declaration से compiler को पहले से मालूम पड़ जाता है कि आगे यह function आने वाला है।
return_type function_name(parameter_list);
return_type: Function का return type लिखा जाता है जैसे,
- int add(); : यह function integer value return करेगा।
- void print(); : यह function कुछ return नहीं करेगा।
function_name: यह function का नाम होता है, जिससे उसे program में कहीं से भी call किया जा सकता है। नाम meaningful होना चाहिए ताकि code पढ़ने में आसान हो, जैसे add, printMessage, findMax आदि।
parameter_list: यह उन values के data types और (optionally) नाम बताते हैं जो function को call करते समय दी जाएंगी। इन्हें “arguments” या “parameters” कहते हैं। Declaration में सिर्फ data type लिखना भी काफी होता है, नाम लिखना ज़रूरी नहीं।
Example:
int add(int a, int b); // दो integers लेगा और एक integer return करेगा
void greet(char name[]); // एक string लेगा और कुछ return नहीं करेगा
float average(int arr[], int size);
ध्यान दें कि declaration के अंत में semicolon (;) लगाया जाता है, क्योंकि यह सिर्फ एक statement है, function का actual code नहीं।
2. Function Definition In C
यह वह जगह है जहाँ function का actual काम (logic) लिखा जाता है। इसमें वही statements होते हैं जो execute होंगे जब function को call किया जाएगा।
Function Declaration Syntax In C
return_type function_name(parameter_list)
{
// function body
// statements
return value; // अगर return type void नहीं है
}
Example:
int add(int a, int b)
{
int sum;
sum = a + b;
return sum;
}यहाँ ध्यान देने वाली बातें:
- Definition में curly braces { } होते हैं, जिनके अंदर function का body (code) लिखा जाता है।
- अगर return type int, float, char आदि है, तो function के अंदर return statement से value वापस भेजी जाती है।
- अगर return type void है, तो return; statement optional है (या बिना value के लिख सकते हैं), क्योंकि function कुछ return नहीं करता।
- Parameter list में दिए गए variables (जैसे a, b) सिर्फ उसी function के अंदर इस्तेमाल हो सकते हैं — इन्हें “local variables” या “formal parameters” कहा जाता है।
3. Function Call
जब हमें function का काम execute करवाना होता है, तो उसे उसके नाम से call करते हैं और ज़रूरत के मुताबिक values (arguments) पास करते हैं।
function_name(actual_arguments);
Example:
int result;
result = add(5, 10); // function call, 5 और 10 actual arguments हैं
printf(“%d”, result);
यहाँ ध्यान देने वाली बातें:
- जो values हम call करते समय भेजते हैं उन्हें actual arguments कहते हैं, और जो definition में होते हैं उन्हें formal parameters कहते हैं।
- Function call में दी गई values, formal parameters में क्रम (order) के अनुसार copy हो जाती हैं (call by value में)।
- अगर function कोई value return करता है, तो उसे किसी variable में store किया जा सकता है, या सीधे print/use भी किया जा सकता है।
- अगर return type void है, तो उसे सीधे ही statement के रूप में call किया जाता है, बिना किसी variable में assign किए।
Complete Example Program (Declaration + Definition + Call)
#include <stdio.h>
// Function declaration (prototype)
int add(int a, int b);
int main() {
int result;
// Call the function and store the returned value
result = add(5, 10);
printf("Sum = %d\n", result);
return 0;
}
// Function definition
int add(int a, int b) {
return a + b; // Return the sum of two numbers
}
इस तरह, declaration compiler को function के बारे में जानकारी देता है, definition में actual logic लिखा जाता है, और call उस logic को execute करने के लिए इस्तेमाल होता है। यह तीनों मिलकर function को modular, reusable और readable बनाते हैं।
Function Declaration Without Definition In C
यह ज़रूरी नहीं है कि हमेशा अलग से function declaration (prototype) लिखा ही जाए। अगर हम किसी function की definition को main() से पहले लिख दें, तो compiler को उस function के बारे में पहले से ही पूरी जानकारी (return type, name, parameters) मिल जाती है। ऐसी स्थिति में अलग से declaration लिखने की ज़रूरत नहीं पड़ती, क्योंकि definition खुद ही declaration का काम भी कर देती है।
Program Code:
#include <stdio.h>
// Function definition
int add(int a, int b) {
return a + b; // Return the sum of two numbers
}
int main() {
int result;
// Call the function and store the returned value
result = add(5, 10);
printf("Sum = %d\n", result);
return 0;
}
यहाँ ध्यान देने वाली बातें:
- चूँकि add() function की पूरी definition main() से पहले लिखी गई है, compiler को main() के अंदर इस function को call करते समय कोई दिक्कत नहीं होती, क्योंकि वह पहले ही उसका return type, नाम और parameters देख चुका होता है।
- अगर हम function की definition को main() के बाद लिखें (जैसा पहले उदाहरण में किया गया था), तो उस स्थिति में अलग से declaration (prototype) देना ज़रूरी हो जाता है, ताकि compiler को पहले से पता हो कि आगे यह function defined किया जाएगा। अगर declaration नहीं देंगे, तो compiler error या warning दे सकता है (function add को main() में call करने से पहले उसके बारे में compiler को कुछ पता नहीं होगा)।
- इसलिए, छोटे programs में अक्सर declaration को skip करके सीधे main() से पहले ही function की definition लिख दी जाती है — इससे code छोटा और simple हो जाता है।
- बड़े programs में आमतौर पर declarations को header file (.h) में या file की शुरुआत में लिखा जाता है, और definitions को बाद में या अलग file में, ताकि code organized रहे।
इस तरह, declaration, definition और call मिलकर function को modular, reusable और readable बनाते हैं — और छोटे programs में definition को पहले लिखकर declaration को avoid भी किया जा सकता है।
Function in C Programming in Hindi – Types of Functions
Function in C programming मुख्यतः चार तरह के होते हैं — return type और parameters के आधार पर।
Type 1: No Return Value, No Parameters
यह सबसे simple function होता है। न कोई input लेता है, न कोई value वापस देता है।
Program:
#include <stdio.h>
// Function definition
void showMessage() {
printf("This is a simple function.\n");
}
int main() {
// Call the function
showMessage();
return 0;
}
Type 2: No Return Value, With Parameters
यह function input तो लेता है लेकिन कोई value वापस नहीं देता।
Program:
#include <stdio.h>
// Function to calculate and display the sum of two numbers
void printSum(int a, int b) {
int sum = a + b;
printf("Sum = %d\n", sum);
}
int main() {
// Function calls
printSum(10, 20);
printSum(5, 15);
return 0;
}
Type 3: With Return Value, No Parameters
यह function कोई input नहीं लेता लेकिन एक value वापस देता है।
Program:
#include <stdio.h>
// Function that returns a number
int getNumber() {
return 42;
}
int main() {
// Store the returned value
int result = getNumber();
printf("Function returned: %d\n", result);
return 0;
}
Type 4: With Return Value, With Parameters
यह सबसे ज्यादा उपयोग होने वाला type है। Input भी लेता है और result भी देता है।
Program:
#include <stdio.h>
// Function to multiply two numbers
int multiply(int a, int b) {
return a * b;
}
int main() {
// Store the returned result
int result = multiply(6, 7);
printf("6 x 7 = %d\n", result);
return 0;
}
Function में Arguments कैसे Pass होते हैं?
Call by Value
Call by Value में function को variable की एक copy दी जाती है। Original variable में कोई बदलाव नहीं होता।
Call by Value Program:
#include <stdio.h>
// Function to double the value of a parameter
void doubleIt(int n) {
n = n * 2;
printf("Inside function: %d\n", n);
}
int main() {
int num = 10;
doubleIt(num);
printf("In main: %d\n", num);
return 0;
}
यहाँ num की original value नहीं बदली — क्योंकि function को सिर्फ copy मिली थी।
Call by Reference (Pointer के साथ)
Call by Reference में function को variable का address दिया जाता है, जिससे original value बदली जा सकती है।
Call by Reference Program:
#include <stdio.h>
// Function to double the actual value using a pointer
void doubleIt(int *ptr) {
*ptr = *ptr * 2;
}
int main() {
int num = 10;
printf("Before: %d\n", num);
doubleIt(&num);
printf("After: %d\n", num);
return 0;
}
Recursive Function क्या होता है?
जब कोई function खुद को ही call करे, उसे Recursive Function कहते हैं। यह complex problems को आसान steps में solve करने का तरीका देता है।
Factorial Program In C
#include <stdio.h>
// Recursive function to calculate factorial
int factorial(int n) {
if (n == 0 || n == 1) {
return 1; // Base condition
}
return n * factorial(n - 1); // Recursive call
}
int main() {
int num = 5;
printf("Factorial of %d = %d\n", num, factorial(num));
return 0;
}
यहाँ factorial(5) → 5 × factorial(4) → 5 × 4 × factorial(3)… इस तरह चलता है जब तक n = 1 न हो जाए।
Note: Recursive function में हमेशा एक base condition होना जरूरी है, वरना function हमेशा के लिए चलता रहेगा।
Library Functions And User Defined Functions
C में functions दो तरह के होते हैं — pre-defined (library functions) और user-defined (खुद बनाए गए functions)।
Pre-defined Functions
ये functions C की library (जैसे stdio.h, math.h, string.h) में पहले से बने होते हैं। इन्हें सिर्फ header file include करके सीधे इस्तेमाल कर सकते हैं, बनाना नहीं पड़ता।
उदाहरण: printf(), scanf(), strlen(), sqrt(), strcpy()
User-defined Functions
ये functions programmer खुद अपनी जरूरत के हिसाब से बनाते हैं। इनका नाम, parameters, return type और logic सब programmer तय करते हैं।
उदाहरण: add(), checkEven(), printTable()
| Points | Pre-defined Function | User-defined Function |
|---|---|---|
| बनाता कौन है | C library (पहले से तैयार) | Programmer खुद |
| Header file | जरूरी (stdio.h, math.h आदि) | जरूरत नहीं |
| Definition | पहले से लिखी होती है | खुद लिखनी पड़ती है |
| उदाहरण | printf(), strlen(), sqrt() | add(), display(), check() |
| Customization | बदल नहीं सकते | अपनी मर्जी से बदल सकते हैं |
एक Real Life Example — Calculator Program
अब तक जो सीखा उसे एक practical program में देखते हैं। यह function in C programming का बेहतरीन Example Program है:
#include <stdio.h>
// Function to add two numbers
int add(int a, int b) {
return a + b;
}
// Function to subtract two numbers
int subtract(int a, int b) {
return a - b;
}
// Function to multiply two numbers
int multiply(int a, int b) {
return a * b;
}
// Function to divide two numbers
float divide(int a, int b) {
if (b == 0) {
printf("Error: Division by zero is not allowed!\n");
return 0;
}
return (float)a / b;
}
int main() {
int x = 20, y = 4;
printf("Addition: %d\n", add(x, y));
printf("Subtraction: %d\n", subtract(x, y));
printf("Multiplication: %d\n", multiply(x, y));
printf("Division: %.2f\n", divide(x, y));
return 0;
}
देखिए कितना साफ और organized code बना — हर operation के लिए अलग function और main() में सिर्फ function calls।
Function Use करते समय इन बातों का ध्यान रखें
- Function को उपयोग से पहले declare करें — वरना compiler error देगा
- Return type match करें — अगर function int return करता है तो int variable में ही store करें
- Parameters की count और type सही रखें — function call में उतने ही arguments दें जितने declare किए हों
- Infinite recursion से बचें — recursive function में base condition जरूर लिखें
- Function के नाम meaningful रखें — add(), calculateArea() जैसे नाम code को self-explanatory बनाते हैं
Conclusion (निष्कर्ष)
Function in C Programming in Hindi में आज हमने पूरी journey की — function क्या होता है, इसकी structure कैसी होती है, इसके चार types कौन से हैं, Call by Value और Call by Reference का फर्क क्या है, Recursive Function कैसे काम करता है, और एक real Calculator Program भी बनाया। हर concept को simple भाषा और working code examples के साथ समझाया गया ताकि आपको कोई confusion न रहे। Functions C programming की रीढ़ की हड्डी हैं — इन्हें अच्छे से समझ लेने के बाद Arrays, Pointers, Structures जैसे topics और भी आसान लगेंगे।
अब आपकी बारी है — इस post में दिए गए सभी programs को अपने computer पर लिखकर run करें और खुद से छोटे-छोटे functions बनाने की practice करें। जैसे एक function जो दो numbers में से बड़ा number बताए, या एक function जो किसी number का square निकाले। जितना ज्यादा practice करेंगे, उतना concept मजबूत होगा। अगर कोई सवाल हो तो नीचे comment जरूर करें, और इस post को उन दोस्तों के साथ share करें जो C programming सीख रहे हैं!
Frequently Asked Questions
Function in C Programming in Hindi से जुड़े कुछ common सवाल जो students अक्सर पूछते हैं:
