Function parameters in SmileBASIC are not type-checked - a parameter can have any type, regardless of the variable name, depending only on what value was passed to the function.
If you want to treat different types of parameters differently, it is useful to be able to identify the type of a parameter. Currently there is no known way to identify arrays, but it is possible to differentiate between real number type, integer type, and string type. Consider the following function:
'Returns 0 for real, 1 for integer, 2 for string
DEF TYPEOF(X)
IF X*0 && X*0==X*0 THEN RETURN 2
RETURN X*0-&H80000000<0
END
So, how does this work? First, let's consider differentiating strings from numbers. The critical point of this is that all strings are considered to be True in IF statements or the && and || operators. So if you multiply a string by 0, you get the empty string, which is considered True. On the other hand, if you multiply a number by 0, usually you get 0, which is False.
I say "usually" because there are some special cases as far as real numbers are concerned. Multiplying Infinity or NaN (Not a Number) by 0 will produce NaN, which is considered non-zero, or True. So if we check only X*0, then strings, Infinity, and NaN will be detected. The key to solving this problem is that NaN is considered to be not equal to itself, while on the other hand the empty string is always equal to itself. So X*0==X*0 out of these possible values will be True only if X is a string.
After strings are ruled out, it is fairly simple to differentiate between reals and integers. We will detect an instance of integer overflow - calculating (0 - (-2^31)) should return 2^31, but if we are working with integers, that overflows and becomes negative, so we return True or 1. On the other hand, if X is real it will return 2^31 as expected, so we return False or 0. Note also that if X*0 is our good friend NaN, comparing NaN<0 will also return False. All possible cases are handled.

8 Comment(s)snail_Power UserHelperReceived for being very helpful around SmileBASIC SourceAchievementsExpert ProgrammerProgramming no longer gives me any trouble. Come to me for help, if you like!Programming StrengthAvatar TabooI didn't change my avatar for 180 daysWebsiteThe first statement can be changed to
IF (X||0)==3 THEN RETURN 2
This is because string || anything always returns 3 instead of 1 or 0, bizarrely enough. This saves you 3 multiplies and is generally shorter.12Me21AdminSyntax HighlighterReceived for creating the code syntax highlighter on SBSNight PersonI like the quiet night and sleep late.Express YourselfSecond YearMy account is over 2 years oldWebsiteIF X==0==3 THEN RETURN 21 character shorter.
EDIT:
IF X>0>1 THEN RETURN 2
EDIT 2:
SmileBASIC, 57 52 bytesDEF Y(X)IF X>0>2THEN RETURN 2
RETURN.>X*0-(1<<31)ENDSquareFingersWow, this exposes a very strange aspect of SmileBasic.
Your code relies on &H80000000 being interpreted as the 'integer' type, not the 'floating-point' type. But...
PRINT &H0-&H80000000
gives a positive result. So, &H0 appears to be getting treated as a floating-point, and &H80000000 appears to be getting treated as integer. Just for fun I tried
PRINT &H00000000-&H80000000
and that also showed the zero was treated as floating-point.
calc84maniacOSP Contest 1 WinnerI won the first SmileBASIC Source OSP Contest!Programming ContestAmazing PageHiddenAchievementsScholarReceived for knowing a great deal about programming topicsAchievementsAs I said in the relevant forum thread, none of those constants individually are floating-point, but combined the result are automatically extended to floating point because of the overflow (as a part of the preprocessing step of constant folding). This does not happen at runtime.randomousRobotHiddenEaster EggsSecond YearMy account is over 2 years oldWebsiteDrawingI like to draw!HobbiesOh... yeah, there's no bbcode on comments yet. Sorry!GuzzlerFirst WeekJoined in the very first week of SmileBASIC SourceWebsiteDrawingI like to draw!HobbiesIntermediate ProgrammerI can make programs, but I still have trouble here and there. Programming StrengthHey randomouse, FIX THAT ALREADY . Lol, see what I did?kldck_hulFirst MonthJoined in the very first month of SmileBASIC SourceWebsiteScholarReceived for knowing a great deal about programming topicsAchievementsZelda Is Awesome!I love The Legend Of Zelda!Express YourselfThis is plain brilliant. I'm working on an object-oriented language extension and this was one of the big things missing. I will probably include this in my library.randomousRobotHiddenEaster EggsSecond YearMy account is over 2 years oldWebsiteDrawingI like to draw!HobbiesThis is REALLY useful, and I'm glad you took the time to figure all this out. Thank you for posting this!

`IF (X||0)==3 THEN RETURN 2`

This is because`IF X==0==3 THEN RETURN 2`

1 character shorter. EDIT:`IF X>0>1 THEN RETURN 2`

EDIT 2:SmileBASIC,~~57~~52 bytes`DEF Y(X)IF X>0>2THEN RETURN 2 RETURN.>X*0-(1<<31)END`

`PRINT &H0-&H80000000`

gives a positive result. So, &H0 appears to be getting treated as a floating-point, and &H80000000 appears to be getting treated as integer. Just for fun I tried`PRINT &H00000000-&H80000000`

and that also showed the zero was treated as floating-point.FIX THAT ALREADY. Lol, see what I did?