Home
JAQForum Ver 24.01
Log In or Join  
Active Topics
Local Time 11:09 01 Aug 2025 Privacy Policy
Jump to

Notice. New forum software under development. It's going to miss a few functions and look a bit ugly for a while, but I'm working on it full time now as the old forum was too unstable. Couple days, all good. If you notice any issues, please contact me.

Forum Index : Microcontroller and PC projects : Question re Library and Default Data Type

Author Message
erbp
Senior Member

Joined: 03/05/2016
Location: Australia
Posts: 195
Posted: 01:15am 22 Jun 2019
Copy link to clipboard 
Print this post

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: Australia
Posts: 3292
Posted: 02:34am 22 Jun 2019
Copy link to clipboard 
Print this post

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.

  erbp said  is it permissible to include OPTION DEFAULT ... in the library?

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: Australia
Posts: 6283
Posted: 02:38am 22 Jun 2019
Copy link to clipboard 
Print this post

Safest way would be to use OPTION DEFAULT NONE

  Quote  Rather than using the type suffix (eg, the $ in arg2$) you can use the suffix AS <type> in the definition of the subroutine argument and then the argument will be known as the specified type, even when the suffix is not used. For example:
SUB MYSUB (arg1, arg2 AS STRING, arg3)
IF arg2 = "Cat" THEN …
END SUB
If you do not define the type of the argument it will default to float or that set by OPTION DFAULT. Further, if you used OPTION DEFAULT NONE to force you to define the type of all variables you will also have to define them in the subroutine or function definition.


Jim
VK7JH
MMedit
 
erbp
Senior Member

Joined: 03/05/2016
Location: Australia
Posts: 195
Posted: 05:53am 22 Jun 2019
Copy link to clipboard 
Print this post

@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.
 
Print this page


To reply to this topic, you need to log in.

The Back Shed's forum code is written, and hosted, in Australia.
© JAQ Software 2025