Introduction
जब हम functions का उपयोग करते हैं, तो कई बार हमें function के अंदर कुछ data भेजना होता है ताकि वह उस data पर operation कर सके।
उदाहरण के लिए:
- दो numbers का sum निकालना
- user से input लेकर calculation करना
- values को process करना
अगर हम function के अंदर ही fixed values रखेंगे, तो function reusable नहीं रहेगा। इसलिए हमें function को data देना पड़ता है।
इसी process को Passing Arguments कहा जाता है।
Passing arguments programming में बहुत important concept है क्योंकि:
- इससे function dynamic बनता है
- same function को अलग-अलग values के साथ use कर सकते हैं
- modular programming आसान होती है
C language में arguments को function में भेजने के दो मुख्य तरीके होते हैं:
- Call by Value
- Call by Reference
Definition
जब हम किसी function को call करते समय values (data) उसके parameters में भेजते हैं, तो इस process को Passing Arguments कहा जाता है।
Types of Passing Arguments
1. Call by Value
Definition
Call by Value में actual values की copy function को दी जाती है, इसलिए function के अंदर बदलाव original variable को affect नहीं करता।
Example 1
#include <stdio.h>
void change(int x)
{
x = x + 10;
printf("Inside function: %d\n", x);
}
int main()
{
int a = 5;
change(a);
printf("Outside function: %d", a);
return 0;
}
Output:
Inside function: 15
Outside function: 5
Explanation:
- function को value की copy मिली
- अंदर value change हुई (15)
- original variable (a) पर कोई असर नहीं पड़ा
Example 2: Addition
#include <stdio.h>
int add(int a, int b)
{
return a + b;
}
int main()
{
int result = add(3, 4);
printf("%d", result);
return 0;
}
Output:
7
2. Call by Reference
Definition
Call by Reference में variable का address function को दिया जाता है, जिससे function original variable की value को change कर सकता है।
Example 1
#include <stdio.h>
void change(int *x)
{
*x = *x + 10;
}
int main()
{
int a = 5;
change(&a);
printf("%d", a);
return 0;
}
Output:
15
Explanation:
- address pass किया गया
- function ने original variable को modify किया
- इसलिए value 15 हो गई
Example 2: Swap Two Numbers
#include <stdio.h>
void swap(int *a, int *b)
{
int temp;
temp = *a;
*a = *b;
*b = temp;
}
int main()
{
int x = 5, y = 10;
swap(&x, &y);
printf("%d %d", x, y);
return 0;
}
Output:
10 5
Explanation:
- दोनों variables के address pass हुए
- function ने actual values swap कर दी
Difference: Call by Value vs Call by Reference
| Call by Value | Call by Reference |
|---|---|
| Value की copy pass होती है | Address pass होता है |
| Original value change नहीं होती | Original value change हो सकती है |
| Safe लेकिन limited | Powerful लेकिन careful use |
Technical Understanding
Passing arguments का use इन situations में होता है:
- function को input देना
- calculations perform करना
- data modify करना
- memory efficient programming
Exam Points
- Passing arguments = function को data देना
- दो प्रकार: Call by Value, Call by Reference
- Value में original data change नहीं होता
- Reference में original data change हो सकता है
- Pointer concept call by reference में use होता है