Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>If you want a bigger picture about this read about passing parameters by value and passing by reference it also applies for returning parameters.</p> <p>As you mentioned:</p> <blockquote> <p>You can return multiple data items using a structure but using pointer (I don't understand this) will use memory more efficiently. Is this correct?</p> </blockquote> <p>Lets say you have some structure:</p> <pre><code>struct BigStructure{ int array[1000]; double otherData[1000]; //... etc }; </code></pre> <p>and some method:</p> <pre><code>BigStructure workWhenCopying(BigStructure bigStruct){ // some work with a structure return bigStruct; } </code></pre> <p>This method illustrates the case when you pass parameters to the method and return it by value, which means that each time you call this method you are copying the method's argument into another place in memory. This copying takes time and when you have bigger structures it slows down your program run time thus it is more efficient to pass big structures by reference.</p> <pre><code>void workWithReference(BigStructure &amp; bigStruct){ // do some work } </code></pre> <p>In this case you are not copying whole data structure, just passing the address of the memory where this structure resides. You also don't need a return statement since changes to that structure object will be visible outside this method. But if you will reassign bigStruct to a new memory location it will be visible only within local method: <code>workWithReference</code></p> <p>I hope it's more clearer now;)</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload