C++ Function Call By Value And Call By Reference

📔 : C++ 🔗

पिछले topics में हमें Parameterized  Function के बार में पढ़ा , ये functions call by value type के functions थे , means function call करते समय हम जो value pass करते हैं , और जब function run होता है तो इन values का बाहरी scope से कुछ मतलब नहीं होता वो function में as a local variable treat किये जाते हैं।


Actually ! Calling के bases पर functions 2 categories में define किया गया है।

  1. Call By Value
  2. Call By Reference

C++ Function Call By Value

जैसा कि आपने ऊपर पढ़ा कि अभी तक हम जो functions define और use कर रहे थे ये Call By Value functions थे । जिसमे हम simply value को pass करते थे।


अगर आप function के अंदर उस value को manipulate भी करोगे तो उस variable की actual value ( जो कि function के बाहर है ) पर कोई फर्क नहीं पड़ता है।

C++ Call By Value Example

CopyFullscreenClose FullscreenRun
#include <iostream>
using namespace std;

// define a simple function.
void test(int number)
{
  // change it's value , so that we can differentiate.
  number += 10;
  cout << "Value inside function : " << number << endl;
}
int main() {
  int number = 5;
  cout << "Value before function : " << number << endl;
  
  // now call the function.
  test(number);
  cout << "Value after function : " << number << endl;
  return 0;
}
Output
Value before function : 5
Value inside function : 15
Value after function : 5

Explain : Example में आप देख सकते हैं कि Function के अंदर value को manipulate किया गया है , but function call होने के बाद define किये गए external variable की value पर कोई असर नहीं पड़ा।

C++ Function Call By Reference

इस तरह के functions में function define करते समय parameter से पहले ampersand (&) का use करते हैं . Reference means variable content को different नाम के साथ access करना।
C++ Reference Variable

C++ Call By Reference Example

CopyFullscreenClose FullscreenRun
#include <iostream>
using namespace std;

// now use & to access it's reference.
void test(int &number)
{
  // change it's value , so that we can differentiate.
  number += 10;
  cout << "Value inside function : " << number << endl;
}
int main() {
  int number = 5;
  cout << "Value before function : " << number << endl;
  
  // now call the function.
  test(number);
  cout << "Value after function : " << number << endl;
  return 0;
}
Output
Value before function : 5
Value inside function : 15
Value after function : 15

Explain :
Same Example में value की जगह variable से पहले value का reference (& ampersand) use किया गया है , इसलिए function के अंदर value को modify करते ही उसकी actual value भी change हो गयी।

C++ Difference Between Call By Value And Call By Reference

Call By Value और Call By Reference Functions में सबसे बड़ा difference भी यही है , Call By Value में हम सिर्फ value को accept करते हैं , इसलिए इस value को हम function के अंदर ही manipulate कर सकते हैं function के बाहर नहीं जबकि Call By Reference में हम function में value की जगह value का reference (जो कि & ampersand से access करते हैं ) use करते हैं।

Related Topics :

Rahul Kumar

Rahul Kumar

Hi ! I'm Rahul Kumar Rajput founder of learnhindituts.com. I'm a software developer having more than 4 years of experience. I love to talk about programming as well as writing technical tutorials and blogs that can help to others. I'm here to help you navigate the coding cosmos and turn your ideas into reality, keep coding, keep learning :)

Get connected with me. :) LinkedIn Twitter Instagram Facebook

b2eprogrammers