{"id":413285,"date":"2024-02-28T16:14:51","date_gmt":"2024-02-29T00:14:51","guid":{"rendered":"https:\/\/linuxhint.com\/?p=413285"},"modified":"2025-01-04T14:25:36","modified_gmt":"2025-01-04T22:25:36","slug":"return-reference-cpp","status":"publish","type":"post","link":"https:\/\/linuxhint.com\/return-reference-cpp\/","title":{"rendered":"Return Reference in C++"},"content":{"rendered":"<div id=\"wpbody\">\n<p>C++ provides a facility to return a value or an address by its reference rather than pointers. Using references instead of pointers can make a C++ program simpler to read and manage. In C++, references and pointers are closely related to one another. The primary distinction is that although references are merely an alternate name, \u201calias\u201d for another variable, pointers can be used in operations such as adding values. A reference is an alternate name\u00a0or duplicate of the original value and is denoted by the \u201c&amp;\u201dsymbol.<\/p>\n<h2>Example 1:<\/h2>\n<p>We import the \u201ciostream\u201d header file and then utilize the \u201cstd\u201d namespace. The header files are imported in C++ codes as many functions are defined. Then, we create a return reference function by placing the \u201c&amp;\u201d symbol with the function\u2019s name, \u201creturnTheValue\u201d.<\/p>\n<p>Here, the \u201cvalue\u201d reference is now inserted. Underneath this, we print the value and the address with the \u201c&amp;value\u201d reference. Then, we place the return reference and place the \u201cvalue\u201d. Now, the \u201cmain()\u201d is invoked here, and we initialize \u201cn1\u201d with the value of \u201c44\u201d. Below this, \u201cint&amp; n2\u201d is initialized with the \u201creturnTheValue(n1)\u201d. Now, we print the value of \u201cn1\u201d as well as its address. Then, we print the value of \u201cn2\u201d as well as the address of \u201cn2\u201d by utilizing the \u201ccout\u201d.<\/p>\n<p><strong>Code 1: <\/strong><\/p>\n<div class=\"codecolorer-container bash blackboard\" style=\"width:100%;height:100%;\"><div class=\"bash codecolorer\"><span class=\"co0\">#include &lt;iostream&gt; <\/span><br \/>\nusing namespace std; <br \/>\nint<span class=\"sy0\">&amp;<\/span> returnTheValue<span class=\"br0\">&#40;<\/span>int<span class=\"sy0\">&amp;<\/span> value<span class=\"br0\">&#41;<\/span> <br \/>\n<span class=\"br0\">&#123;<\/span> <br \/>\n&nbsp; &nbsp; cout <span class=\"sy0\">&lt;&lt;<\/span> <span class=\"st0\">&quot;Value = &quot;<\/span> <span class=\"sy0\">&lt;&lt;<\/span> value <span class=\"sy0\">&lt;&lt;<\/span> endl<br \/>\n&nbsp; &nbsp; &nbsp; &nbsp; <span class=\"sy0\">&lt;&lt;<\/span> <span class=\"st0\">&quot; The address of value is &quot;<\/span><br \/>\n&nbsp; &nbsp; &nbsp; &nbsp; <span class=\"sy0\">&lt;&lt;<\/span> <span class=\"sy0\">&amp;<\/span>value <span class=\"sy0\">&lt;&lt;<\/span> endl; <br \/>\n&nbsp; &nbsp; <span class=\"kw3\">return<\/span> value; <br \/>\n<span class=\"br0\">&#125;<\/span> <br \/>\nint main<span class=\"br0\">&#40;<\/span><span class=\"br0\">&#41;<\/span> <br \/>\n<span class=\"br0\">&#123;<\/span> <br \/>\n&nbsp; &nbsp; int n1 = <span class=\"nu0\">44<\/span>; <br \/>\n&nbsp; &nbsp; int<span class=\"sy0\">&amp;<\/span> n2 = returnTheValue<span class=\"br0\">&#40;<\/span>n1<span class=\"br0\">&#41;<\/span>; <br \/>\n&nbsp; &nbsp; cout <span class=\"sy0\">&lt;&lt;<\/span> <span class=\"st0\">&quot;n1 = &quot;<\/span> <span class=\"sy0\">&lt;&lt;<\/span> n1 <span class=\"sy0\">&lt;&lt;<\/span> endl<br \/>\n&nbsp; &nbsp; &nbsp; &nbsp; <span class=\"sy0\">&lt;&lt;<\/span> <span class=\"st0\">&quot; The address of n1 is &quot;<\/span><br \/>\n&nbsp; &nbsp; &nbsp; &nbsp; <span class=\"sy0\">&lt;&lt;<\/span> <span class=\"sy0\">&amp;<\/span>n1 <span class=\"sy0\">&lt;&lt;<\/span> endl; <br \/>\n&nbsp; &nbsp; cout <span class=\"sy0\">&lt;&lt;<\/span> <span class=\"st0\">&quot;n2 = &quot;<\/span> <span class=\"sy0\">&lt;&lt;<\/span> n2 <span class=\"sy0\">&lt;&lt;<\/span> endl<br \/>\n&nbsp; &nbsp; &nbsp; &nbsp; <span class=\"sy0\">&lt;&lt;<\/span> <span class=\"st0\">&quot; The address of n2 is &quot;<\/span><br \/>\n&nbsp; &nbsp; &nbsp; &nbsp; <span class=\"sy0\">&lt;&lt;<\/span> <span class=\"sy0\">&amp;<\/span>n2 <span class=\"sy0\">&lt;&lt;<\/span> endl; <br \/>\n&nbsp; &nbsp; <span class=\"kw3\">return<\/span> <span class=\"nu0\">0<\/span>; <br \/>\n<span class=\"br0\">&#125;<\/span><\/div><\/div>\n<p>&nbsp;<br \/>\n<strong>Output:<\/strong><\/p>\n<p>Here, we can note that a reference is only an alternate name\u00a0of another variable as shown in the following. As the address of value, \u201cn1\u201d and \u201cn2\u201d never change.<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/linuxhint.com\/wp-content\/uploads\/2024\/02\/Picture1-17.png\" alt=\"\" width=\"381\" height=\"135\" class=\"alignnone size-full wp-image-413286\" srcset=\"https:\/\/linuxhint.com\/wp-content\/uploads\/2024\/02\/Picture1-17.png 381w, https:\/\/linuxhint.com\/wp-content\/uploads\/2024\/02\/Picture1-17-300x106.png 300w\" sizes=\"(max-width: 381px) 100vw, 381px\" \/><\/p>\n<h2>Example 2:<\/h2>\n<p>We use the &#8220;std&#8221; namespace after importing the &#8220;iostream&#8221; header file.\u00a0 Next, we use the function name &#8220;MyReturnValueFunc&#8221; and the &#8220;&amp;&#8221; symbol to build a return reference function. The reference to the &#8220;v1&#8221; variable is placed here. We print the value and the address with the &#8220;&amp;v1&#8221; reference underneath this. Next, we insert the \u201creturn reference\u201d by utilizing \u201creturn\u201d and &#8220;v1&#8221; in this location. Here, &#8220;main()&#8221; is called, and &#8220;num_1&#8221; is initialized with the value of &#8220;19&#8221;. The initialization of &#8220;int&amp; num_2&#8221; is done with &#8220;MyReturnValueFunc(num_1)&#8221;.<\/p>\n<p>Currently, we print the value and address of &#8220;num_1&#8221; and, using &#8220;cout&#8221;, we print the value and address of &#8220;num_2&#8221;. We now change the value of \u201cnum_1\u201d by utilizing the address that is returned here by \u201cMyReturnValueFunc\u201d. This function returns the alternate name of \u201cv1\u201d which is also the alternate name of \u201cnum_1\u201d. So, we change its value and set it to \u201c91\u201d. We assign \u201c91\u201d to \u201cMyReturnValueFunc(num_1)\u201d which acts as the alias here. Then, we print the value again and the address of \u201cnum_1\u201d.<\/p>\n<p><strong>Code 2: <\/strong><\/p>\n<div class=\"codecolorer-container bash blackboard\" style=\"width:100%;height:100%;\"><div class=\"bash codecolorer\"><span class=\"co0\">#include &lt;iostream&gt; <\/span><br \/>\nusing namespace std; <br \/>\nint<span class=\"sy0\">&amp;<\/span> MyReturnValueFunc<span class=\"br0\">&#40;<\/span>int<span class=\"sy0\">&amp;<\/span> v1<span class=\"br0\">&#41;<\/span> <br \/>\n<span class=\"br0\">&#123;<\/span> <br \/>\n&nbsp; &nbsp; cout <span class=\"sy0\">&lt;&lt;<\/span> <span class=\"st0\">&quot;The value of v1 = &quot;<\/span> <span class=\"sy0\">&lt;&lt;<\/span> v1 <span class=\"sy0\">&lt;&lt;<\/span> endl<br \/>\n&nbsp; &nbsp; &nbsp; &nbsp; <span class=\"sy0\">&lt;&lt;<\/span> <span class=\"st0\">&quot; The address of v1 variable is &quot;<\/span><br \/>\n&nbsp; &nbsp; &nbsp; &nbsp; <span class=\"sy0\">&lt;&lt;<\/span> <span class=\"sy0\">&amp;<\/span>v1 <span class=\"sy0\">&lt;&lt;<\/span> endl; <br \/>\n&nbsp; &nbsp; <span class=\"kw3\">return<\/span> v1; <br \/>\n<span class=\"br0\">&#125;<\/span> <br \/>\nint main<span class=\"br0\">&#40;<\/span><span class=\"br0\">&#41;<\/span> <br \/>\n<span class=\"br0\">&#123;<\/span> <br \/>\n&nbsp; &nbsp; int num_1 = <span class=\"nu0\">19<\/span>; <br \/>\n&nbsp; &nbsp; int<span class=\"sy0\">&amp;<\/span> num_2 = MyReturnValueFunc<span class=\"br0\">&#40;<\/span>num_1<span class=\"br0\">&#41;<\/span>; <br \/>\n&nbsp; &nbsp; cout <span class=\"sy0\">&lt;&lt;<\/span> <span class=\"st0\">&quot;The value of num_1 = &quot;<\/span> <span class=\"sy0\">&lt;&lt;<\/span> num_1 <span class=\"sy0\">&lt;&lt;<\/span> endl<br \/>\n&nbsp; &nbsp; &nbsp; &nbsp; <span class=\"sy0\">&lt;&lt;<\/span> <span class=\"st0\">&quot; The address of num_1 is &quot;<\/span><br \/>\n&nbsp; &nbsp; &nbsp; &nbsp; <span class=\"sy0\">&lt;&lt;<\/span> <span class=\"sy0\">&amp;<\/span>num_1 <span class=\"sy0\">&lt;&lt;<\/span> endl; <br \/>\n&nbsp; &nbsp; cout <span class=\"sy0\">&lt;&lt;<\/span> <span class=\"st0\">&quot;The value of num_2 = &quot;<\/span> <span class=\"sy0\">&lt;&lt;<\/span> num_2 <span class=\"sy0\">&lt;&lt;<\/span> endl<br \/>\n&nbsp; &nbsp; &nbsp; &nbsp; <span class=\"sy0\">&lt;&lt;<\/span> <span class=\"st0\">&quot; The address of num_2 is &quot;<\/span><br \/>\n&nbsp; &nbsp; &nbsp; &nbsp; <span class=\"sy0\">&lt;&lt;<\/span> <span class=\"sy0\">&amp;<\/span>num_2 <span class=\"sy0\">&lt;&lt;<\/span> endl; <br \/>\n&nbsp; &nbsp; MyReturnValueFunc<span class=\"br0\">&#40;<\/span>num_1<span class=\"br0\">&#41;<\/span> = <span class=\"nu0\">91<\/span>; <br \/>\n&nbsp; &nbsp; cout <span class=\"sy0\">&lt;&lt;<\/span> <span class=\"st0\">&quot;Now, the value of num_1 = &quot;<\/span> <span class=\"sy0\">&lt;&lt;<\/span> num_1 <span class=\"sy0\">&lt;&lt;<\/span> endl<br \/>\n&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span class=\"sy0\">&lt;&lt;<\/span> <span class=\"st0\">&quot;The address of num_1 is &quot;<\/span><br \/>\n&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span class=\"sy0\">&lt;&lt;<\/span> <span class=\"sy0\">&amp;<\/span>num_1 <span class=\"sy0\">&lt;&lt;<\/span> endl; <br \/>\n&nbsp; &nbsp; <span class=\"kw3\">return<\/span> <span class=\"nu0\">0<\/span>; <br \/>\n<span class=\"br0\">&#125;<\/span><\/div><\/div>\n<p>&nbsp;<br \/>\n<strong>Output:<\/strong><\/p>\n<p>As demonstrated in the following, we can see that a reference is merely an alternative name for another variable since the address of the \u201cv1\u201d, \u201cnum_1\u201d, and \u201cnum_2\u201d values remained constant:<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/linuxhint.com\/wp-content\/uploads\/2024\/02\/Picture2-15.png\" alt=\"\" width=\"456\" height=\"197\" class=\"alignnone size-full wp-image-413287\" srcset=\"https:\/\/linuxhint.com\/wp-content\/uploads\/2024\/02\/Picture2-15.png 456w, https:\/\/linuxhint.com\/wp-content\/uploads\/2024\/02\/Picture2-15-300x130.png 300w\" sizes=\"(max-width: 456px) 100vw, 456px\" \/><\/p>\n<h2>Example 3:<\/h2>\n<p>The &#8220;iostream&#8221; header file is imported and the &#8220;std&#8221; namespace is used. Since numerous functions are specified in the header files, we import them into the C++ codes. Here, we create a \u201cReturnRefFun()\u201d function in which we place \u201cint&amp; my_ref\u201d which returns the reference. The \u201cint&amp; ReturnRefFun\u201d is declared here as the reference function. After this, we increment the value of the \u201cmy_ref\u201d variable. Underneath this, we put \u201creturn\u201d which returns the reference of \u201cmy_ref\u201d.<\/p>\n<p>After this, the \u201cmain()\u201d method is invoked here. Then, we initialize the \u201cfirst_value\u201d variable with \u201c21\u201d. Below this, we return the copy of the reference by placing \u201cfirst_value\u201d in the \u201cReturnRefFun\u201d function and save it in the \u201ccopied_value\u201d variable. Then, we print both the \u201cfirst_value\u201d as well as the \u201ccopied_value\u201d by utilizing the \u201ccout\u201d. Underneath this, we increment the \u201ccopied_value\u201d variable by placing the \u201ccopied_value++\u201d. Then, we print the \u201ccopied_value\u201d after incrementing it and the \u201cfirst_value\u201d using \u201ccout\u201d. After this, we return the reference with the help of initializing the \u201cint&amp; ref_value\u201d variable with \u201cReturnRefFun(first_value)\u201d.<\/p>\n<p>After this, we print the value of the \u201cmy_ref\u201d variable which we copied. Then, we print the value of the \u201cfirst_value\u201d variable. Underneath this, we increment the value of \u201cref_value\u201d by putting \u201cref_value++\u201d. Below this, we print the incremented value of \u201cref_value\u201d and also the \u201cfirst_value\u201d variable with the aid of \u201ccout\u201d. When the \u201cref_value\u201d is changed, the \u201cfirst_value\u201d will also change.<\/p>\n<p><strong>Code 3: <\/strong><\/p>\n<div class=\"codecolorer-container bash blackboard\" style=\"width:100%;height:100%;\"><div class=\"bash codecolorer\"><span class=\"co0\">#include &lt;iostream&gt;<\/span><br \/>\nusing namespace std;<br \/>\nint<span class=\"sy0\">&amp;<\/span> ReturnRefFun<span class=\"br0\">&#40;<\/span>int<span class=\"sy0\">&amp;<\/span> my_ref<span class=\"br0\">&#41;<\/span><span class=\"br0\">&#123;<\/span><br \/>\n&nbsp; &nbsp; my_ref++;<br \/>\n&nbsp; &nbsp; <span class=\"kw3\">return<\/span> my_ref;<br \/>\n<span class=\"br0\">&#125;<\/span><br \/>\nint main<span class=\"br0\">&#40;<\/span><span class=\"br0\">&#41;<\/span> <span class=\"br0\">&#123;<\/span><br \/>\n&nbsp; &nbsp; int <span class=\"re2\">first_value<\/span>= <span class=\"nu0\">21<\/span>;<br \/>\n&nbsp; &nbsp; int <span class=\"re2\">copied_value<\/span>=ReturnRefFun<span class=\"br0\">&#40;<\/span>first_value<span class=\"br0\">&#41;<\/span>;<br \/>\n&nbsp; &nbsp; cout <span class=\"sy0\">&lt;&lt;<\/span> <span class=\"st0\">&quot;The first value is : &quot;<\/span> <span class=\"sy0\">&lt;&lt;<\/span> first_value <span class=\"sy0\">&lt;&lt;<\/span> endl;<br \/>\n&nbsp; &nbsp; &nbsp;cout<span class=\"sy0\">&lt;&lt;<\/span> <span class=\"st0\">&quot;The copied value is : &quot;<\/span> <span class=\"sy0\">&lt;&lt;<\/span> copied_value <span class=\"sy0\">&lt;&lt;<\/span> endl;<br \/>\n&nbsp; &nbsp; copied_value++;<br \/>\n&nbsp; &nbsp; cout<span class=\"sy0\">&lt;&lt;<\/span> <span class=\"st0\">&quot;The copied_value is incremented: &quot;<\/span> <span class=\"sy0\">&lt;&lt;<\/span> copied_value <span class=\"sy0\">&lt;&lt;<\/span> endl;<br \/>\n&nbsp; &nbsp; cout <span class=\"sy0\">&lt;&lt;<\/span> <span class=\"st0\">&quot;The first value : &quot;<\/span> <span class=\"sy0\">&lt;&lt;<\/span> first_value <span class=\"sy0\">&lt;&lt;<\/span> endl;<br \/>\n&nbsp; &nbsp; int<span class=\"sy0\">&amp;<\/span> <span class=\"re2\">ref_value<\/span>=ReturnRefFun<span class=\"br0\">&#40;<\/span>first_value<span class=\"br0\">&#41;<\/span>;<br \/>\n&nbsp; &nbsp; cout <span class=\"sy0\">&lt;&lt;<\/span> <span class=\"st0\">&quot;The reference copied value: &quot;<\/span> <span class=\"sy0\">&lt;&lt;<\/span> ref_value <span class=\"sy0\">&lt;&lt;<\/span> endl;<br \/>\n&nbsp; &nbsp; cout <span class=\"sy0\">&lt;&lt;<\/span> <span class=\"st0\">&quot;The first value : &quot;<\/span> <span class=\"sy0\">&lt;&lt;<\/span> first_value <span class=\"sy0\">&lt;&lt;<\/span> endl;<br \/>\n&nbsp; &nbsp; ref_value++;<br \/>\n&nbsp; &nbsp; cout <span class=\"sy0\">&lt;&lt;<\/span> <span class=\"st0\">&quot;The reference value is incremented : &quot;<\/span> <span class=\"sy0\">&lt;&lt;<\/span> ref_value <span class=\"sy0\">&lt;&lt;<\/span> endl;<br \/>\n&nbsp; &nbsp; cout <span class=\"sy0\">&lt;&lt;<\/span> <span class=\"st0\">&quot;The first value : &quot;<\/span> <span class=\"sy0\">&lt;&lt;<\/span> first_value <span class=\"sy0\">&lt;&lt;<\/span> endl;<br \/>\n&nbsp; &nbsp; <span class=\"kw3\">return<\/span> <span class=\"nu0\">0<\/span>;<br \/>\n<span class=\"br0\">&#125;<\/span><\/div><\/div>\n<p>&nbsp;<br \/>\n<strong>Output: <\/strong><\/p>\n<p>Here is the outcome of the previous code where we utilized the \u201creturn reference\u201d technique. The example shows the distinction between returning a duplicate of the reference variable and returning the reference variable itself.<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/linuxhint.com\/wp-content\/uploads\/2024\/02\/Picture3-16.png\" alt=\"\" width=\"444\" height=\"166\" class=\"alignnone size-full wp-image-413288\" srcset=\"https:\/\/linuxhint.com\/wp-content\/uploads\/2024\/02\/Picture3-16.png 444w, https:\/\/linuxhint.com\/wp-content\/uploads\/2024\/02\/Picture3-16-300x112.png 300w\" sizes=\"(max-width: 444px) 100vw, 444px\" \/><\/p>\n<h2>Example 4:<\/h2>\n<p>Here, \u201cint&amp; rByRef\u201d is declared as the reference function which returns the reference variable. We pass the \u201cint&amp; data\u201d to this \u201cint&amp; rByref()\u201d function. Here, we print the address of the \u201cdata\u201d variable and then utilize the return reference below this. Now, we initialize the \u201cx_var\u201d variable after invoking the \u201cmain()\u201d method. Then, we print the address of \u201cx_var\u201d here by putting the \u201c&amp;x_var\u201d in the \u201ccout\u201d.<\/p>\n<p>Underneath this, we utilize the reference variable by assigning \u201crByref(x_var)\u201d to the \u201cint&amp; y_var\u201d. Then, we also print the address of that \u201c&amp;y_var\u201d reference variable. Below this, we copy the \u201cx_var\u201d variable into the \u201cz_var\u201d variable and also print the address of this copied variable which is \u201c&amp;z_var\u201d. After this, we call the \u201crByref()\u201d function, pass the \u201cx_var\u201d variable as the parameter inside it, and assign \u201c93\u201d to this variable. We also render the address of the \u201cx_var\u201d again by putting the \u201c&amp;x_var\u201d in the \u201ccout\u201d.<\/p>\n<p><strong>Code 4: <\/strong><\/p>\n<div class=\"codecolorer-container bash blackboard\" style=\"width:100%;\"><div class=\"bash codecolorer\"><span class=\"co0\">#include &lt;iostream&gt;<\/span><br \/>\nusing namespace std;<br \/>\nint<span class=\"sy0\">&amp;<\/span> rByref<span class=\"br0\">&#40;<\/span>int<span class=\"sy0\">&amp;<\/span> data<span class=\"br0\">&#41;<\/span><br \/>\n<span class=\"br0\">&#123;<\/span><br \/>\n&nbsp; &nbsp; cout<span class=\"sy0\">&lt;&lt;<\/span> <span class=\"st0\">&quot;Address of data: &quot;<\/span><span class=\"sy0\">&lt;&lt;<\/span> <span class=\"sy0\">&amp;<\/span>data <span class=\"sy0\">&lt;&lt;<\/span> endl;<br \/>\n&nbsp; &nbsp; <span class=\"kw3\">return<\/span> data;<br \/>\n<span class=\"br0\">&#125;<\/span><br \/>\nint main<span class=\"br0\">&#40;<\/span><span class=\"br0\">&#41;<\/span><br \/>\n<span class=\"br0\">&#123;<\/span><br \/>\n&nbsp; &nbsp; int x_var = <span class=\"nu0\">42<\/span>;<br \/>\n&nbsp; &nbsp; cout<span class=\"sy0\">&lt;&lt;<\/span> <span class=\"st0\">&quot;Address of x_var: &quot;<\/span><span class=\"sy0\">&lt;&lt;<\/span> <span class=\"sy0\">&amp;<\/span>x_var <span class=\"sy0\">&lt;&lt;<\/span> endl;<br \/>\n&nbsp; &nbsp; int<span class=\"sy0\">&amp;<\/span> y_var = rByref<span class=\"br0\">&#40;<\/span>x_var<span class=\"br0\">&#41;<\/span>;<br \/>\n&nbsp; &nbsp; cout <span class=\"sy0\">&lt;&lt;<\/span> <span class=\"st0\">&quot;Address of y_var: &quot;<\/span><span class=\"sy0\">&lt;&lt;<\/span> <span class=\"sy0\">&amp;<\/span>y_var <span class=\"sy0\">&lt;&lt;<\/span> endl;<br \/>\n&nbsp; &nbsp; int z_var = rByref<span class=\"br0\">&#40;<\/span>x_var<span class=\"br0\">&#41;<\/span>;<br \/>\n&nbsp; &nbsp; cout <span class=\"sy0\">&lt;&lt;<\/span> <span class=\"st0\">&quot;Address of z_var: &quot;<\/span><span class=\"sy0\">&lt;&lt;<\/span> <span class=\"sy0\">&amp;<\/span>z_var <span class=\"sy0\">&lt;&lt;<\/span> endl;<br \/>\n&nbsp; &nbsp; rByref<span class=\"br0\">&#40;<\/span>x_var<span class=\"br0\">&#41;<\/span> = <span class=\"nu0\">93<\/span>; <br \/>\n&nbsp; &nbsp; cout<span class=\"sy0\">&lt;&lt;<\/span> <span class=\"st0\">&quot;Address of x_var: &quot;<\/span><span class=\"sy0\">&lt;&lt;<\/span> <span class=\"sy0\">&amp;<\/span>x_var <span class=\"sy0\">&lt;&lt;<\/span> endl;<br \/>\n&nbsp; &nbsp; <span class=\"kw3\">return<\/span> <span class=\"nu0\">0<\/span>;<br \/>\n<span class=\"br0\">&#125;<\/span><\/div><\/div>\n<p>&nbsp;<br \/>\n<strong>Output: <\/strong><\/p>\n<p>The result makes it clear that the cloned variable&#8217;s address, \u201cz_var\u201d, differs from all the other locations that the original variable, \u201cx_var\u201d, references.<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/linuxhint.com\/wp-content\/uploads\/2024\/02\/Picture4-11.png\" alt=\"\" width=\"382\" height=\"150\" class=\"alignnone size-full wp-image-413289\" srcset=\"https:\/\/linuxhint.com\/wp-content\/uploads\/2024\/02\/Picture4-11.png 382w, https:\/\/linuxhint.com\/wp-content\/uploads\/2024\/02\/Picture4-11-300x118.png 300w, https:\/\/linuxhint.com\/wp-content\/uploads\/2024\/02\/Picture4-11-380x150.png 380w\" sizes=\"(max-width: 382px) 100vw, 382px\" \/><\/p>\n<h2>Conclusion<\/h2>\n<p>The \u201creturn reference\u201d concept is explored in this tutorial in detail. We learned that the \u201creturn reference\u201d is similar to the \u201cpointers\u201d in C++ programming. We discussed that to indicate which function returns a reference, the \u201c&amp;\u201d symbol needs to be utilized with the function&#8217;s return type. We illustrated some examples and their outcomes and understood this concept in this tutorial.<\/p>\n<\/div>\n","protected":false},"excerpt":{"rendered":"<p>Tutorial on the \u201creturn reference\u201d concept in C++  by utilizing the \u201c&amp;\u201d symbol with the function&#8217;s return type to indicate which function returns a reference.<\/p>\n","protected":false},"author":145,"featured_media":413302,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_wp_convertkit_post_meta":{"form":"-1","landing_page":"","tag":"0","restrict_content":"0"},"footnotes":""},"categories":[1515],"tags":[],"class_list":["post-413285","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-cpp"],"_links":{"self":[{"href":"https:\/\/linuxhint.com\/wp-json\/wp\/v2\/posts\/413285","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/linuxhint.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/linuxhint.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/linuxhint.com\/wp-json\/wp\/v2\/users\/145"}],"replies":[{"embeddable":true,"href":"https:\/\/linuxhint.com\/wp-json\/wp\/v2\/comments?post=413285"}],"version-history":[{"count":0,"href":"https:\/\/linuxhint.com\/wp-json\/wp\/v2\/posts\/413285\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/linuxhint.com\/wp-json\/wp\/v2\/media\/413302"}],"wp:attachment":[{"href":"https:\/\/linuxhint.com\/wp-json\/wp\/v2\/media?parent=413285"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/linuxhint.com\/wp-json\/wp\/v2\/categories?post=413285"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/linuxhint.com\/wp-json\/wp\/v2\/tags?post=413285"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}