Passing Arguments and Scope Rules
How data moves in and out of functions, and where variables are valid.
Passing Arguments and Scope Rules
C++ में function को data या values देने के लिए arguments का उपयोग किया जाता है। Function के अंदर variables की visibility और lifetime को समझने के लिए scope rules महत्वपूर्ण हैं। Passing Arguments और Scope Rules functions के साथ काम करते समय C++ के महत्वपूर्ण concepts हैं।
What is Passing Arguments?
जब किसी function को call करते समय उसे कुछ values दी जाती हैं, तो इन values को Arguments कहा जाता है। ये arguments function के parameters को values प्रदान करते हैं।
उदाहरण:
#include <iostream>
using namespace std;
void display(int n)
{
cout << "Number = " << n;
}
int main()
{
display(25);
return 0;
}
Number = 25
यहाँ 25 argument है और n parameter है।
Parameters and Arguments
Parameter वह variable है जो function definition में लिखा जाता है, जबकि Argument वह actual value है जो function call करते समय दी जाती है।
int add(int a, int b)
{
return a + b;
}
int result = add(10, 20);
| Term | Example | Meaning |
|---|---|---|
| Parameter | a, b |
Function definition में variables |
| Argument | 10, 20 |
Function call में actual values |
Types of Argument Passing
C++ में arguments को function में अलग-अलग तरीकों से pass किया जा सकता है। Basic level पर प्रमुख रूप से निम्न methods महत्वपूर्ण हैं:
- Call by Value
- Call by Reference
1. Call by Value
Call by Value में argument की value की एक copy function के parameter में pass की जाती है। Function के अंदर parameter में किया गया change original variable को affect नहीं करता है।
#include <iostream>
using namespace std;
void change(int x)
{
x = 100;
}
int main()
{
int a = 10;
change(a);
cout << "a = " << a;
return 0;
}
a = 10
यहाँ a की value की copy x में गई। इसलिए x को 100 करने पर original variable a की value नहीं बदली।
Example of Call by Value
दो numbers को function में pass करके उनका sum निकाला जा सकता है।
#include <iostream>
using namespace std;
int add(int a, int b)
{
return a + b;
}
int main()
{
int x = 15;
int y = 25;
cout << "Sum = " << add(x, y);
return 0;
}
Sum = 40
2. Call by Reference
Call by Reference में function parameter को original variable का reference दिया जाता है। इसलिए function के अंदर parameter में किया गया change original variable को भी affect कर सकता है।
C++ में reference parameter के लिए ampersand (&) का उपयोग किया जाता है।
#include <iostream>
using namespace std;
void change(int &x)
{
x = 100;
}
int main()
{
int a = 10;
change(a);
cout << "a = " << a;
return 0;
}
a = 100
यहाँ x original variable a का reference है। इसलिए x में किया गया change a में भी दिखाई देता है।
Call by Value vs Call by Reference
| Call by Value | Call by Reference |
|---|---|
| Value की copy pass होती है। | Original variable का reference pass होता है। |
| Original variable सामान्यतः change नहीं होता। | Original variable को change किया जा सकता है। |
| Parameter अलग copy पर काम करता है। | Parameter original variable को refer करता है। |
Example: void fun(int x) |
Example: void fun(int &x) |
Swapping Using Call by Value
Call by Value में दो variables को function के अंदर swap करने पर original variables की values change नहीं होतीं।
#include <iostream>
using namespace std;
void swapNumbers(int a, int b)
{
int temp = a;
a = b;
b = temp;
}
int main()
{
int x = 10;
int y = 20;
swapNumbers(x, y);
cout << "x = " << x << endl;
cout << "y = " << y;
return 0;
}
x = 10
y = 20
Swapping Using Call by Reference
Call by Reference की सहायता से function के अंदर किए गए changes original variables में दिखाई देते हैं। इसलिए swapping के लिए इसका उपयोग किया जा सकता है।
#include <iostream>
using namespace std;
void swapNumbers(int &a, int &b)
{
int temp = a;
a = b;
b = temp;
}
int main()
{
int x = 10;
int y = 20;
swapNumbers(x, y);
cout << "x = " << x << endl;
cout << "y = " << y;
return 0;
}
x = 20
y = 10
What is Scope?
Scope का अर्थ है program का वह area या region जहाँ कोई variable या identifier accessible होता है। दूसरे शब्दों में, scope यह निर्धारित करता है कि किसी variable को program के किस हिस्से में use किया जा सकता है।
Types of Scope
C++ में variables के scope को basic रूप से निम्न प्रकार समझा जा सकता है:
- Local Scope
- Global Scope
- Block Scope
1. Local Scope
Function के अंदर declare किया गया variable सामान्यतः उसी function के अंदर accessible होता है। ऐसे variable को Local Variable कहा जाता है।
#include <iostream>
using namespace std;
void display()
{
int x = 10;
cout << x;
}
int main()
{
display();
return 0;
}
10
यहाँ x display() function का local variable है। इसे main() में directly access नहीं किया जा सकता।
Example of Local Scope
#include <iostream>
using namespace std;
void function1()
{
int x = 50;
cout << "Inside function = " << x;
}
int main()
{
function1();
return 0;
}
Inside function = 50
2. Global Scope
जो variable सभी functions के बाहर declare किया जाता है, उसे Global Variable कहा जाता है। Global variable का scope उसके declaration के बाद program के कई parts तक हो सकता है।
#include <iostream>
using namespace std;
int x = 100;
void display()
{
cout << "Inside function = " << x;
}
int main()
{
cout << "Inside main = " << x << endl;
display();
return 0;
}
Inside main = 100
Inside function = 100
यहाँ x global variable है, इसलिए इसे main() और display() दोनों में access किया जा सकता है।
3. Block Scope
किसी block के अंदर declare किया गया variable उस block के अंदर ही accessible होता है। Block सामान्यतः curly braces { } से निर्धारित होता है।
#include <iostream>
using namespace std;
int main()
{
int x = 10;
if (x > 0)
{
int y = 20;
cout << "x = " << x << endl;
cout << "y = " << y << endl;
}
return 0;
}
x = 10
y = 20
यहाँ y if block के अंदर declare हुआ है, इसलिए इसका scope उसी block तक सीमित है।
Local Variable and Global Variable
| Local Variable | Global Variable |
|---|---|
| Function या block के अंदर declare होता है। | Functions के बाहर declare होता है। |
| इसका scope सीमित होता है। | इसका scope program के बड़े हिस्से तक हो सकता है। |
| केवल अपने accessible region में use किया जा सकता है। | कई functions से access किया जा सकता है। |
Example: int x = 10; inside a function |
Example: int x = 10; outside all functions |
Variable Scope and Function Parameters
Function parameters भी local variables की तरह function के अंदर accessible होते हैं। उनका scope function body तक सीमित होता है।
#include <iostream>
using namespace std;
void display(int n)
{
cout << n;
}
int main()
{
display(50);
return 0;
}
50
यहाँ n function parameter है और इसे display() function के अंदर access किया जा सकता है।
Same Variable Name in Different Scopes
C++ में अलग-अलग scopes में समान नाम के variables declare किए जा सकते हैं। ऐसी स्थिति में local variable अपने scope में global variable को hide कर सकता है।
#include <iostream>
using namespace std;
int x = 100;
int main()
{
int x = 50;
cout << x;
return 0;
}
50
यहाँ main() के अंदर local variable x मौजूद है। इसलिए उसी scope में local x को प्राथमिकता मिलती है।
Accessing Global Variable Using Scope Resolution Operator
यदि local और global variable का नाम समान हो, तो global variable को access करने के लिए scope resolution operator :: का उपयोग किया जा सकता है।
#include <iostream>
using namespace std;
int x = 100;
int main()
{
int x = 50;
cout << "Local x = " << x << endl;
cout << "Global x = " << ::x;
return 0;
}
Local x = 50
Global x = 100
:: को Scope Resolution Operator कहा जाता है। इसका उपयोग global scope में मौजूद variable या member को access करने के लिए किया जा सकता है।
Scope of a Variable in a Function
एक function में declare किया गया local variable दूसरे function में directly accessible नहीं होता। यदि दूसरे function को data देना हो, तो arguments और parameters का उपयोग किया जा सकता है।
#include <iostream>
using namespace std;
void display(int n)
{
cout << "Value = " << n;
}
int main()
{
int x = 25;
display(x);
return 0;
}
Value = 25
Passing Multiple Arguments
एक function में एक से अधिक arguments pass किए जा सकते हैं। Arguments का order parameters के order से match होना चाहिए।
#include <iostream>
using namespace std;
void student(string name, int marks)
{
cout << "Name = " << name << endl;
cout << "Marks = " << marks;
}
int main()
{
student("Rahul", 85);
return 0;
}
Name = Rahul
Marks = 85
Important Points
- Function call करते समय दी गई actual values को arguments कहा जाता है।
- Function definition में दिए गए variables को parameters कहा जाता है।
- Call by Value में argument की copy function को pass होती है।
- Call by Reference में original variable का reference function को दिया जाता है।
- Call by Value में parameter में किया गया change original variable को affect नहीं करता।
- Call by Reference में parameter में किया गया change original variable को affect कर सकता है।
- Scope किसी variable के accessible region को निर्धारित करता है।
- Function के अंदर declare variable का scope सामान्यतः local होता है।
- Functions के बाहर declare variable global scope में हो सकता है।
- Block के अंदर declare variable का scope उस block तक सीमित होता है।
::Scope Resolution Operator है।
Board Focus
Argument → Function call में दी गई actual value
Parameter → Function definition में variable
Call by Value → Value की copy pass होती है
Call by Reference → Reference pass होता है
Local Scope → Function/block तक सीमित scope
Global Scope → Functions के बाहर declared variable का scope
Block Scope → Curly braces
{ } के अंदर का scope:: → Scope Resolution Operator
Board Important Questions
Very Short Answer Questions
Q1. Argument क्या है?
Answer: Function call के समय दी गई actual value को Argument कहा जाता है।
Q2. Parameter क्या है?
Answer: Function definition में दिए गए variable को Parameter कहा जाता है।
Q3. Call by Value क्या है?
Answer: ऐसी method जिसमें argument की value की copy function parameter को pass होती है, Call by Value कहलाती है।
Q4. Call by Reference क्या है?
Answer: ऐसी method जिसमें function parameter original variable के reference को प्राप्त करता है, Call by Reference कहलाती है।
Q5. Scope क्या है?
Answer: Program का वह region जहाँ कोई variable या identifier accessible होता है, उसका Scope कहलाता है।
Q6. Scope Resolution Operator क्या है?
Answer: :: को Scope Resolution Operator कहा जाता है।
Short Answer Questions
Q7. Call by Value और Call by Reference में अंतर बताइए।
| Call by Value | Call by Reference |
|---|---|
| Value की copy pass होती है। | Reference pass होता है। |
| Original variable सामान्यतः प्रभावित नहीं होता। | Original variable प्रभावित हो सकता है। |
Example: int x |
Example: int &x |
Q8. Local और Global Variable में अंतर बताइए।
Answer: Local variable किसी function या block के अंदर declare होता है और उसका scope सीमित होता है। Global variable सभी functions के बाहर declare होता है और उसे program के विभिन्न accessible parts में use किया जा सकता है।
Q9. Arguments और Parameters को उदाहरण सहित समझाइए।
int add(int a, int b)
{
return a + b;
}
add(10, 20);
यहाँ a और b parameters हैं तथा 10 और 20 arguments हैं।
Q10. Scope Resolution Operator का क्या उपयोग है?
Answer: Scope Resolution Operator :: का उपयोग global scope के variable या अन्य scope-related members को explicitly access करने के लिए किया जाता है।
Long Answer Questions
Q11. Call by Value को उदाहरण सहित समझाइए।
Answer: Call by Value में function को argument की value की copy प्राप्त होती है। Function के अंदर parameter में किए गए changes original variable को प्रभावित नहीं करते।
#include <iostream>
using namespace std;
void change(int x)
{
x = 100;
}
int main()
{
int a = 10;
change(a);
cout << a;
return 0;
}
10
Q12. Call by Reference को उदाहरण सहित समझाइए।
Answer: Call by Reference में function parameter original variable को reference करता है। इसलिए parameter में किया गया change original variable में भी दिखाई देता है।
#include <iostream>
using namespace std;
void change(int &x)
{
x = 100;
}
int main()
{
int a = 10;
change(a);
cout << a;
return 0;
}
100
Q13. Local और Global Scope को उदाहरण सहित समझाइए।
Answer: Function के अंदर declare variable का scope उस function तक सीमित होता है और उसे local variable कहा जाता है। Functions के बाहर declare variable global variable होता है और उसका scope program के विभिन्न parts तक हो सकता है।
Q14. निम्न program का output बताइए:
#include <iostream>
using namespace std;
int x = 100;
int main()
{
int x = 50;
cout << x << endl;
cout << ::x;
return 0;
}
50
100
Q15. निम्न program का output बताइए:
#include <iostream>
using namespace std;
void change(int &x)
{
x = x + 10;
}
int main()
{
int a = 20;
change(a);
cout << a;
return 0;
}
30
Quick Revision
- Argument: Function call में दी गई actual value
- Parameter: Function definition में variable
- Call by Value: Value की copy pass करना
- Call by Reference: Original variable का reference pass करना
- Local Variable: Function या block के अंदर accessible variable
- Global Variable: Functions के बाहर declared variable
- Block Scope: किसी block
{ }तक सीमित scope - Scope Resolution:
::
Practice Questions
- Argument और Parameter में क्या अंतर है?
- Call by Value की परिभाषा लिखिए।
- Call by Reference की परिभाषा लिखिए।
- Call by Value और Call by Reference में अंतर बताइए।
- Scope से आप क्या समझते हैं?
- Local Variable क्या है?
- Global Variable क्या है?
- Block Scope क्या है?
- Scope Resolution Operator क्या है?
- Call by Value का उदाहरण लिखिए।
- Call by Reference का उदाहरण लिखिए।
- Call by Reference का उपयोग करके दो numbers को swap करने का C++ program लिखिए।
- Local और Global variables का उपयोग करते हुए एक C++ program लिखिए।
- निम्न program का output बताइए:
int x = 20; int main() { int x = 10; cout << x; } - एक C++ program लिखिए जिसमें function को दो arguments pass किए जाएँ और उनका product return किया जाए।