User Tools

Site Tools


passing_20array_20and_20structure_20parameters

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

passing_20array_20and_20structure_20parameters [2018/03/31 13:19] (current)
Line 1: Line 1:
 +=====Passing array and structure parameters=====
  
 +//by Richard Russell, April 2009//\\ \\  The main //BBC BASIC for Windows// documentation explains that whilst scalar variables are (by default) passed to functions and procedures **by value** arrays and structures are passed **by reference**. However this somewhat simplifies exactly what happens, and this article attempts to provide additional detail that may be of value to '​advanced'​ users.\\ \\  It is somewhat more accurate to say that **all** parameters are passed by value //unless// the [[http://​www.bbcbasic.co.uk/​bbcwin/​manual/​bbcwin2.html#​passbyref|RETURN]] keyword is used in the function/​procedure definition. The complication with arrays and structures is that what is passed 'by value' is not the actual array or structure, but a **pointer** (in the case of an array it's a pointer to the array descriptor; in the case of a structure it's a pair of pointers, to the format and to the data). Details of these pointer formats can be found in the [[http://​www.bbcbasic.co.uk/​bbcwin/​manual/​bbcwind.html#​variablestorage|Format of data in memory]] section of the manual.\\ \\  So although the pointer(s) are passed by **value**, the data itself is effectively passed by **reference**. This means if you modify the **data** of a structure or array within the FN/PROC it will remain changed on exit, but if you modify the **pointer(s)** they will be restored on exit.\\ \\  In particular this explains why the manipulation of the structure pointer described in the article [[/​Passing%20substructures%20to%20procedures|Passing substructures to procedures]] does not affect the structure outside of the FN/PROC. Because the pointer is passed by value it is automatically restored on exit.
passing_20array_20and_20structure_20parameters.txt ยท Last modified: 2018/03/31 13:19 (external edit)