Introduction
जब हम programming करते हैं, तो कई बार एक ही काम को बार-बार करना पड़ता है, जैसे:
- numbers का sum निकालना
- factorial calculate करना
- input/output handle करना
अगर हम हर बार वही code बार-बार लिखेंगे, तो program लंबा, complex और error-prone हो जाएगा।
इसी problem को solve करने के लिए C language में Functions का concept दिया गया है।
Function एक ऐसा block of code होता है जो किसी specific task को perform करता है और जिसे हम program में बार-बार use कर सकते हैं।
Functions के main advantages:
- Code Reusability (बार-बार use कर सकते हैं)
- Modular Programming (program को छोटे parts में divide करना)
- Easy Debugging (error ढूँढना आसान)
- Better Readability
इसलिए Functions programming का बहुत important concept है और exam में भी frequently पूछा जाता है।
Definition
Function एक block of code होता है जो किसी specific task को perform करता है और जिसे program में call करके execute किया जाता है।
Function Definition
Syntax
return_type function_name(parameters)
{
// statements
return value;
}
Example 1: Simple Function
#include <stdio.h>void greet()
{
printf("Hello Student");
}
int main()
{
greet();
return 0;
}
Output:
Hello Student
Explanation:
greet()function define किया गया- main() से call किया गया
- function execute हुआ और message print हुआ
Example 2: Function with Return Value
#include <stdio.h>
int add()
{
int a = 5, b = 3;
return a + b;
}
int main()
{
int result = add();
printf("%d", result);
return 0;
}
Output:
8
Explanation:
- function ने value return की (5 + 3)
- main() ने उस value को receive करके print किया
Types of Functions in C
Functions को दो main categories में divide किया जाता है:
1. Library Functions
- पहले से defined होते हैं
- header files में मिलते हैं
Example:
printf(), scanf(), sqrt()
2. User-defined Functions
- programmer खुद बनाता है
- specific task के लिए use होते हैं
Example:
int add()
{
return 5 + 3;
}
Types Based on Arguments and Return Value
Exam में बहुत important
| Type | Description |
|---|---|
| No arguments, No return value | केवल काम करता है |
| Arguments, No return value | input लेता है, return नहीं करता |
| No arguments, Return value | return करता है |
| Arguments, Return value | input लेता है और return भी करता है |
Example 3: No Argument, No Return
#include <stdio.h>
void display()
{
printf("Hello");
}
int main()
{
display();
return 0;
}
Output:
Hello
Example 4: With Argument, With Return
#include <stdio.h>
int add(int a, int b)
{
return a + b;
}
int main()
{
int result = add(4, 6);
printf("%d", result);
return 0;
}
Output:
10
Explanation:
- arguments (4, 6) function में गए
- function ने sum करके return किया
Technical Understanding
Functions का use इन problems में होता है:
- repeated calculations
- large program को modules में divide करना
- reusable logic बनाना
- structured programming
Exam Points
- Function = reusable block of code
- Function definition में return type, name, parameters होते हैं
- दो प्रकार: library और user-defined
- 4 types (argument/return base) बहुत important हैं
- function call से execution होता है