![]() |
Forum Index : Microcontroller and PC projects : Question re Library and Default Data Type
Author | Message | ||||
erbp Senior Member ![]() Joined: 03/05/2016 Location: AustraliaPosts: 195 |
I am looking for some clarification as to how specification of a Default Data Type affects code in both the library and a program. Suppose I have the following procedure loaded in the library: Sub SortStrArray(spArray$(),ipMaxIdx) I don't have any OPTION DEFAULT ... active at this time so I would expect that the ipMaxIdx argument would be interpreted as a FLOAT, as that is the default Default Data Type (except that until the code is actually called by a program it's data type is irrelevant). So, if I then load a program that calls the SortStrArray(...) procedure how does the inclusion (or not) of any OPTION DEFAULT statement in that program affect the data type of ipMaxIdx:- - If no OPTION DEFAULT is in the program? - If OPTION DEFAULT INTEGER is included in the program? - If OPTION DEFAULT NONE is included in the program? Also, is it permissible to include OPTION DEFAULT ... in the library? - If so, what happens if the program loaded also includes an OPTION DEFAULT but the data types specified in the library and the program do not match? Thanks, Phil. |
||||
Geoffg![]() Guru ![]() Joined: 06/06/2011 Location: AustraliaPosts: 3292 |
OPTION DEFAULT only has an effect when a variable is declared or a sub/fun is called. To determine the type of a variable MMBasic first checks if it was specifically specified (ie, INTEGER or a subscript) if it cannot find anything it will then look to OPTION DEFAULT to see what type to use. Once the variable has been created its type is fixed and cannot be changed, even if a different OPTION DEFAULT was later used. However, it is different with sub/fun arguments and local variables. They are created on the fly when the sub/fun is called and if their type is not specified it will change to whatever OPTION DEFAULT is set to at that time. In this example xxx changes type between calls: OPTION DEFAULT FLOAT MySub 4.321 OPTION DEFAULT INTEGER MySub 4.321 SUB MySub xxx PRINT xxx END SUB It does not matter if MySub is in the library or the main code. Yes, and because the library code is executed before the main code it will set the default type for the main program. It is better for OPTION DEFAULT to only be used once at the start of the library or main code. A much better strategy is to use OPTION DEFAULT NONE and don't change it. That way you will be forced into being specific as to the data types in your program and avoid the philosophical confusion that can happen with default types. Geoff Geoff Graham - http://geoffg.net |
||||
TassyJim![]() Guru ![]() Joined: 07/08/2011 Location: AustraliaPosts: 6283 |
Safest way would be to use OPTION DEFAULT NONE Jim VK7JH MMedit |
||||
erbp Senior Member ![]() Joined: 03/05/2016 Location: AustraliaPosts: 195 |
@Geoff and @TassieJim Thanks. I do normally use OPTION DEFAULT NONE, but I am working on a project which is involving trying to shoehorn more code into a MM+ Flash Memory space than you normally would, so every extra byte counts. I have exceeded the limit a couple of times already and then been able to figure out another way to save a couple hundred bytes and keep squeezing stuff in. By using OPTION DEFAULT INTEGER, I can save on declaring INTEGER after a DIM, or AS INTEGER following the variable name (or function if it returns an integer type), or even using the % suffix. I recently figured another way to get access to another KB or so, so I now have a little bit of leeway and will use the % suffix on the integers in the library rather than rely on the OPTION DEFAULT setting for them. Thanks for the explanation anyway, it is good to know a bit more about how the interpreter is treating stuff "behind the scenes". Cheers, Phil. |
||||
![]() |
![]() |
The Back Shed's forum code is written, and hosted, in Australia. | © JAQ Software 2025 |