<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Ishann Daultani 👨‍💻</title>
    <description>The latest articles on DEV Community by Ishann Daultani 👨‍💻 (@daultaniishann).</description>
    <link>https://dev.to/daultaniishann</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F613653%2Fe77ed365-a647-4115-bab1-866c73313564.jpg</url>
      <title>DEV Community: Ishann Daultani 👨‍💻</title>
      <link>https://dev.to/daultaniishann</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/daultaniishann"/>
    <language>en</language>
    <item>
      <title>Call by Value vs Call by Reference</title>
      <dc:creator>Ishann Daultani 👨‍💻</dc:creator>
      <pubDate>Wed, 09 Jun 2021 09:32:44 +0000</pubDate>
      <link>https://dev.to/daultaniishann/call-by-value-vs-call-by-reference-4e62</link>
      <guid>https://dev.to/daultaniishann/call-by-value-vs-call-by-reference-4e62</guid>
      <description>&lt;p&gt;Call by value and Call by reference are the two types of generic methods to pass parameters to a function. &lt;br&gt;
C++ uses call by value method while Java uses call by reference method.&lt;br&gt;
But what does Python use? does Python use call by value or call by reference to pass parameters to a function? &lt;br&gt;
&lt;strong&gt;Let's find out ......&lt;/strong&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Example 1
&lt;/h2&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;def add_num(number):
    number += 1

n = 12

add_num(n)
print(n)                          # 12

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;here it does not change value of n at all. So it seems Python uses call by value method to pass the parameters.&lt;br&gt;
But is it correct?&lt;br&gt;
let's see another example to confirm it...&lt;/p&gt;

&lt;h2&gt;
  
  
  Example 2
&lt;/h2&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;def add_element(num_list):
    num_list.append(5)

my_list = [1,2,3,4]
add_element(my_list)
print(my_list)                             # [1,2,3,4,5]

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;That's Weird!&lt;br&gt;
Example 2 tells us that Python uses call by reference method to pass the parameters.&lt;/p&gt;

&lt;p&gt;These are two contradicting examples by themselves !&lt;/p&gt;

&lt;p&gt;Don't get confused , let's decode what's actually going on here&lt;/p&gt;

&lt;h2&gt;
  
  
  Explanation
&lt;/h2&gt;

&lt;p&gt;first of all remember a simple basic thing about Python i.e everything in Python is an Object.&lt;/p&gt;

&lt;p&gt;In the 2nd example when we call the function &lt;em&gt;add_element&lt;/em&gt; and pass &lt;em&gt;my_list&lt;/em&gt; as the parameter we are passing the entire object as a parameter and that object gets mapped onto &lt;em&gt;num_list&lt;/em&gt;. &lt;br&gt;
So now &lt;em&gt;num_list&lt;/em&gt; and &lt;em&gt;my_list&lt;/em&gt; are pointing to the same list i.e [1,2,3,4] so now any changes made to this list whether by &lt;em&gt;my_list&lt;/em&gt; or by &lt;em&gt;num_list&lt;/em&gt; will be made in the original list&lt;/p&gt;

&lt;p&gt;If you want to confirm this you can use the &lt;strong&gt;id()&lt;/strong&gt; function on both &lt;em&gt;my_list&lt;/em&gt; and &lt;em&gt;num_list&lt;/em&gt; and can see the same for yourself.&lt;/p&gt;

&lt;p&gt;This type of parameter passing method is known as call by object.&lt;br&gt;
In this method you pass the entire object as the parameter and then all the changes made to this object is global&lt;/p&gt;

&lt;p&gt;Now you would ask , in example 1 isn't &lt;em&gt;n&lt;/em&gt; also an Int object? So technically the value of &lt;em&gt;n&lt;/em&gt; should also change , but it doesn't.&lt;br&gt;
Why?&lt;br&gt;
&lt;strong&gt;let's solve that mystery also&lt;/strong&gt;...&lt;/p&gt;

&lt;p&gt;let's go to basics again.&lt;br&gt;
In the 2nd example we have used a list object and lists are mutable while in the 1st example we have used an Int object and Int is immutable.&lt;br&gt;
So what actually is happening in example 1 is when we increment the value of &lt;em&gt;n&lt;/em&gt; since Int is immutable the function creates a new container and stores in it the incremented value of &lt;em&gt;n&lt;/em&gt; while the original &lt;em&gt;n&lt;/em&gt; stays the same. &lt;/p&gt;

&lt;h2&gt;
  
  
  Conclusion
&lt;/h2&gt;

&lt;p&gt;So to conclude , Python uses call by object method to pass around the parameters and it depends on the object type whether it uses call by value or call by reference method underneath . &lt;br&gt;
when you pass the function an immutable object Python uses call by value method . &lt;br&gt;
when you pass the function a mutable object Python uses call by reference method. &lt;/p&gt;

&lt;p&gt;Hope your confusion is clear&lt;/p&gt;

&lt;p&gt;If you still have some doubts feel free to connect with me on &lt;strong&gt;&lt;a href="https://twitter.com/DaultaniIshann"&gt;Twitter&lt;/a&gt;&lt;/strong&gt;&lt;/p&gt;

</description>
      <category>python</category>
      <category>programming</category>
      <category>oop</category>
    </item>
  </channel>
</rss>
