This shows you the differences between two versions of the page.
Next revision | Previous revision | ||
passing_20substructures_20to_20procedures [2018/03/31 13:19] 127.0.0.1 external edit |
passing_20substructures_20to_20procedures [2021/12/04 10:08] richardrussell Modified to be compatible with 64-bit platforms |
||
---|---|---|---|
Line 2: | Line 2: | ||
//by Richard Russell, August 2006//\\ \\ As described in the [[http://www.bbcbasic.co.uk/bbcwin/manual/bbcwin2.html#structprocfn|main documentation]] you can pass entire structures to a procedure or function as follows:\\ \\ | //by Richard Russell, August 2006//\\ \\ As described in the [[http://www.bbcbasic.co.uk/bbcwin/manual/bbcwin2.html#structprocfn|main documentation]] you can pass entire structures to a procedure or function as follows:\\ \\ | ||
+ | <code bb4w> | ||
DIM object{name$,pos{x,y,z}} | DIM object{name$,pos{x,y,z}} | ||
object.pos.x = 1.0 | object.pos.x = 1.0 | ||
Line 11: | Line 12: | ||
DEF FN_distance(s{}) | DEF FN_distance(s{}) | ||
= SQR(s.pos.x^2 + s.pos.y^2 + s.pos.z^2) | = SQR(s.pos.x^2 + s.pos.y^2 + s.pos.z^2) | ||
+ | </code> | ||
However you //cannot// pass an entire **substructure** to a procedure or function. The following code doesn't work (it will crash BBC BASIC):\\ \\ | However you //cannot// pass an entire **substructure** to a procedure or function. The following code doesn't work (it will crash BBC BASIC):\\ \\ | ||
+ | <code bb4w> | ||
REM This doesn't work - don't try it at home! | REM This doesn't work - don't try it at home! | ||
DIM object{name$,pos{x,y,z}} | DIM object{name$,pos{x,y,z}} | ||
Line 22: | Line 25: | ||
DEF FN_distance(p{}) | DEF FN_distance(p{}) | ||
= SQR(p.x^2 + p.y^2 + p.z^2) | = SQR(p.x^2 + p.y^2 + p.z^2) | ||
+ | </code> | ||
In most cases passing the parent structure instead, as in the first example, isn't a major inconvenience, however it does mean that the substructure //must always have the same name// (in this case **pos{}**).\\ \\ It would be desirable to be able to use a common **FN_distance** function (for example) even if the substructure doesn't always have the same name; in particular this would make it easier to incorporate the function in a library. There is a way of achieving this, which involves passing to the procedure or function //both// the parent structure //and// the substructure. Here's how you would do it for this particular example:\\ \\ | In most cases passing the parent structure instead, as in the first example, isn't a major inconvenience, however it does mean that the substructure //must always have the same name// (in this case **pos{}**).\\ \\ It would be desirable to be able to use a common **FN_distance** function (for example) even if the substructure doesn't always have the same name; in particular this would make it easier to incorporate the function in a library. There is a way of achieving this, which involves passing to the procedure or function //both// the parent structure //and// the substructure. Here's how you would do it for this particular example:\\ \\ | ||
+ | <code bb4w> | ||
DIM object{name$,pos{x,y,z}} | DIM object{name$,pos{x,y,z}} | ||
object.pos.x = 1.0 | object.pos.x = 1.0 | ||
Line 31: | Line 36: | ||
DEF FN_distance(s{},p{}) | DEF FN_distance(s{},p{}) | ||
- | !(^p{}+4) += !(^s{}+4) | + | PTR(p{}) = PTR(s{}) + PTR(p{}) |
= SQR(p.x^2 + p.y^2 + p.z^2) | = SQR(p.x^2 + p.y^2 + p.z^2) | ||
+ | </code> | ||
Note the manipulation of memory in **FN_distance** which makes this work.\\ \\ Now you //can// use the same function with a differently-named substructure:\\ \\ | Note the manipulation of memory in **FN_distance** which makes this work.\\ \\ Now you //can// use the same function with a differently-named substructure:\\ \\ | ||
+ | <code bb4w> | ||
DIM newobj{type%,loc{x,y,z}} | DIM newobj{type%,loc{x,y,z}} | ||
newobj.loc.x = 4.0 | newobj.loc.x = 4.0 | ||
Line 40: | Line 47: | ||
PRINT FN_distance(newobj{},newobj.loc{}) | PRINT FN_distance(newobj{},newobj.loc{}) | ||
END | END | ||
+ | </code> |