c++ වල pass by value සහ pass by reference

Sea1men

Well-known member
  • Oct 25, 2015
    889
    413
    63
    හදේ
    c++ වල pass by value සහ pass by reference වල වෙනස පොඩ්ඩක් පැහැදිලි කරලා දෙන්නකෝ....:sorry::confused::rolleyes:
     
    Last edited:

    charith84

    Well-known member
  • Jun 22, 2006
    10,397
    7,412
    113
    Value : කියන්නෙහ් උබ පාස් කරන්නහ් ඔනි එකට වෙරියබල් එකක් දැම්මොත් පාස් වෙන්නෙහ් එ වෙරියබල් එකේ තියෙනහ් වැලිව් එක විතරයි
    Reference: දැම්මොත් උබ පාස් කරන්නහ් ඔනි තැනට දාන වෙරියබල් එක තමා පාස් වෙන්නෙහ් උබට එ වෙරියබල් එකෙ වැලිව් එක චෙන්ජ් කරන්නහ් පුලුවන් එ ෆන්ශන් එක තෘ
     

    KingCM

    Well-known member
  • Jul 23, 2013
    6,920
    948
    113
    www.biogen.lk
    Value : කියන්නෙහ් උබ පාස් කරන්නහ් ඔනි එකට වෙරියබල් එකක් දැම්මොත් පාස් වෙන්නෙහ් එ වෙරියබල් එකේ තියෙනහ් වැලිව් එක විතරයි
    Reference: දැම්මොත් උබ පාස් කරන්නහ් ඔනි තැනට දාන වෙරියබල් එක තමා පාස් වෙන්නෙහ් උබට එ වෙරියබල් එකෙ වැලිව් එක චෙන්ජ් කරන්නහ් පුලුවන් එ ෆන්ශන් එක තෘ

    QdcG2.gif
     

    KingCM

    Well-known member
  • Jul 23, 2013
    6,920
    948
    113
    www.biogen.lk
    If I tell you the URL, I'm passing by reference. You can use that URL to see the same web page I can see. If that page is changed, we both see the changes. If you delete the URL, all you're doing is destroying your reference to that page - you're not deleting the actual page itself.

    If I print out the page and give you the printout, I'm passing by value. Your page is a disconnected copy of the original. You won't see any subsequent changes, and any changes that you make (e.g. scribbling on your printout) will not show up on the original page. If you destroy the printout, you have actually destroyed your copy of the object - but the original web page remains intact.​

    QdcG2.gif
     

    Sea1men

    Well-known member
  • Oct 25, 2015
    889
    413
    63
    හදේ
    Value : කියන්නෙහ් උබ පාස් කරන්නහ් ඔනි එකට වෙරියබල් එකක් දැම්මොත් පාස් වෙන්නෙහ් එ වෙරියබල් එකේ තියෙනහ් වැලිව් එක විතරයි
    Reference: දැම්මොත් උබ පාස් කරන්නහ් ඔනි තැනට දාන වෙරියබල් එක තමා පාස් වෙන්නෙහ් උබට එ වෙරියබල් එකෙ වැලිව් එක චෙන්ජ් කරන්නහ් පුලුවන් එ ෆන්ශන් එක තෘ

    thanx :):):)
     

    Sea1men

    Well-known member
  • Oct 25, 2015
    889
    413
    63
    හදේ
    If I tell you the URL, I'm passing by reference. You can use that URL to see the same web page I can see. If that page is changed, we both see the changes. If you delete the URL, all you're doing is destroying your reference to that page - you're not deleting the actual page itself.

    If I print out the page and give you the printout, I'm passing by value. Your page is a disconnected copy of the original. You won't see any subsequent changes, and any changes that you make (e.g. scribbling on your printout) will not show up on the original page. If you destroy the printout, you have actually destroyed your copy of the object - but the original web page remains intact.​

    QdcG2.gif

    බොහොම ස්තුතියි. ගොඩක් පින් :yes:
     

    Sea1men

    Well-known member
  • Oct 25, 2015
    889
    413
    63
    හදේ
    තව කට හරි පුලුවන්නම් example එකකින් පොඩ්ඩක් පැහැදිලි කරන්න පුලුවන්ද?
     

    tery123

    Well-known member
  • Feb 25, 2011
    6,031
    177
    63
    34
    c++ වල pass by value සහ pass by reference වල වෙනස පොඩ්ඩක් පැහැදිලි කරලා දෙන්නකෝ....:sorry::confused::rolleyes:

    variable ekata assign karana value eka pass karannath puluwan...naththan variable eka thiyena memory location adress eka pass karannath puluwan...
     

    Daxter

    Well-known member
  • Sep 10, 2014
    171
    8
    48
    In C++ you can use both pass by value and pass by reference.In C++There are two types of pass by reference.1)Pass by pointers
    2)Pass by reference
    pointer types store the address of particular type. Reference is just another name for that type .Once you assign a value to reference , you can not reassign it.Unlike references you can reassign values to pointers.

    int a = 12;
    int *b = &a; //this is a integer type pointer that stores the address of a
    int &c =a; //this integer type reference

    i think previous answers tell difference between pass by value and pass by reference.In pass by value , you pass an exact copy of values.Performance wise this is not good.In pass by reference ,you do not pass copy values or objects, just pass address or reference .
    int myFuntion(int a,int b); //funtion prototype


    Pass by value
    int x=2;
    int y =3;
    myFuntion(x,y);

    pass by reference using pointers

    int x=2;
    int y =3;

    int myFuntion(int *a, int *b);
    myFuntion(&x,&y);

    Pass by reference by references
    int myFuntion(int &a, int &b);
    int x=2;
    int y =3;
    myFuntion(x,y);

    There is an another use for pass by reference is that you can modify original variables .In pass by value you can not do this.
    void swap (int& first, int& second)
    {
    int temp = first;
    first = second;
    second = temp;
    }
    int a = 2;
    int b = 3;
    swap( a, b );

    Some times you do not need to modify originals .Just pass as const references or pointers

    myFuntion(const int &z);

    Diffidence between pass by pointers vs Pass by references
    pointers are more prone to bugs. so if possible use references .Use pointers when needed.
    reference can not take null values directly. when your function need to take null values use pointers.

    Other than this , pass by references is a good way of returning multiple values .Normally a function can return a single value.But using pass by references you can return more than one values.
     
    Last edited:

    Sea1men

    Well-known member
  • Oct 25, 2015
    889
    413
    63
    හදේ
    In C++ you can use both pass by value and pass by reference.In C++There are two types of pass by reference.1)Pass by pointers
    2)Pass by reference
    pointer types store the address of particular type. Reference is just another name for that type .Once you assign a value to reference , you can not reassign it.Unlike references you can reassign values to pointers.

    int a = 12;
    int *b = &a; //this is a integer type pointer that stores the address of a
    int &c =a; //this integer type reference

    i think previous answers tell difference between pass by value and pass by reference.In pass by value , you pass an exact copy of values.Performance wise this is not good.In pass by reference ,you do not pass copy values or objects, just pass address or reference .
    int myFuntion(int a,int b); //funtion prototype


    Pass by value
    int x=2;
    int y =3;
    myFuntion(x,y);

    pass by reference using pointers

    int x=2;
    int y =3;

    int myFuntion(int *a, int *b);
    myFuntion(&x,&y);

    Pass by reference by references
    int myFuntion(int &a, int &b);
    int x=2;
    int y =3;
    myFuntion(x,y);

    There is an another use for pass by reference is that you can modify original variables .In pass by value you can not do this.
    void swap (int& first, int& second)
    {
    int temp = first;
    first = second;
    second = temp;
    }
    int a = 2;
    int b = 3;
    swap( a, b );

    Some times you do not need to modify originals .Just pass as const references or pointers

    myFuntion(const int &z);

    Diffidence between pass by pointers vs Pass by references
    pointers are more prone to bugs. so if possible use references .Use pointers when needed.
    reference can not take null values directly. when your function need to take null values use pointers.

    Other than this , pass by references is a good way of returning multiple values .Normally a function can return a single value.But using pass by references you can return more than one values.


    බොහොම ස්තුතියි සහෝදරයා :):yes::yes:
     

    Sea1men

    Well-known member
  • Oct 25, 2015
    889
    413
    63
    හදේ
    variable ekata assign karana value eka pass karannath puluwan...naththan variable eka thiyena memory location adress eka pass karannath puluwan...

    Subscribed for entertainment purposes :P


    precisely :yes:

    Pass by value kiwwama original eke copy ekak thama pass wenne. Pass by reference kiwwama original eke memory location ekata reference ekak thama pass wenne.

    thanx all :):):)