Tuesday, 25 June 2013

Difference between pass by Value and pass by Reference? [ simple explanation both in English and Alien accent :P +tip ]

alright about this, 2 explanation helped a lot. i'm gonna post both of em.


a. in english:
lets say i kinda want to share a web page with you.
now if i give you the URL of the web site, you can use that URL and see the very same web page from your home pc, that i can see it from mine home pc. now if you choose to delete the URL the thing you actually doing is destroying the reference to that web page, but the page is still there and i still can have access to it.
so this will be passing by Reference.

now how about if i take a print out of that very same page and give it to you, then as you have guessed it will be passing by Value. now you only have  isolated copy of the original 'thing', you wont be seeing any subsequent changes if i make after that little gift incident. and vise versa, any change you make on your print out copy wont change the original copy. now destroying or deleting your copy only will delete a copy version of the original 'thing',original web page shall remain intact.



b.this one is in alien language:
pass by value is using a copy of the original value stored in another memory. like allocate some more memory Then copy the value in that memory and Then use that exact memory through the way of the certain function / method. original variable aint affected by the function at all.

now about pass by reference is using the actual memory storing that value. if function changes the value then that change is immediate and preserved when the function exits / executed.



in short : pass by Reference is like having a phone number of someone, and pass by Value is like having a photo of that certain someone.

Tip:
pasing by Reference makes a faster program execution.

No comments:

Post a Comment