more sample functions
Posted: Fri Mar 22, 2013 8:00 pm
This thread is designed to augment my earlier thread viewtopic.php?f=4&t=21 titled sample string functions and to provide advanced documentation for the user functions. The ability to define your own functions is an important capability in this basic. There are several things to note about the def command.
1. As noted in the online help the def command can be either a single line or multiple lines ending in end def (enddef will also work).
2. the function name can be either a numeric variable name or a string variable name (ends with a $) and functions can be defined for either just by picking the function name.
3. Functions can be used anywhere in the program even inside of print statements or other commands.
4. Functions must be defined earlier in the program and must be in the main code (Inside a subroutine will not work).
5. A function can have any number of inputs and zero or one output.
6. If the function does not have any inputs then the parentheses are optional, unlike builtin functions which always require them even if empty ().
7. variable names and values inside a function are local, they can be duplicates of other variable names and won't effect any of the values in global names.
8. The only way to pass the answer back is to assign it to the function name.
9. Functions can be freely copied unmodified from one program to another without having to worry about the variable names.
10. Any equation can be turned into a function.
Here are some sample functions that illustrate a few things you can do with a function. (Most of these were pasted in from running code.)
The first is a slightly modified version of the mid$ shown in the other thread. The standard mid$ in other basics has an optional third argument and if missing it will automatically assume the end of the string. The def function command in this basic requires all arguments be entered. To simulate the automatic function of the original I have allowed a 0 to be entered as the third argument and use that to trigger the calculation of the location of the end of the string.
def mid$(a$,a,b)
if b=0 then b=len(a$)-a+1
mid$=substr$(a$,a-1,a+b-2)
end def
This basic does automatic conversion of strings to numbers and vice versa using the assignment statement. However sometimes you might want to convert inside another statement that is performing another calculation without having to assign the value. This function allows that.
def val(a$)=a$
The next statement seems to do nothing but it takes advantage of the automatic conversion to provide a function that doesn't seem to require an assignment. The is useful for basics that use the STR$ function natively and may embed this as part of another statement.
def str$(a)=a
Here is a numeric function that will round a number to the supplied number of digits. Often a computer provides more precision than the accuracy of the supplied data has. This could fool the user into to believing the answer is more accurate than it really is, hence the need to round the value to a reasonable accurate value.
def round(a,p)
a$=abs(int(a))
p=p-len(a$)
round=int(10^p*a+.5)/10^p
enddef
This basic supports rotating the screen but there needs to be a way for a program to determine which way the user is holding the tablet or iPhone.
def isLandscape
if screen_width() > screen_height() then isLandscape=1 else isLandscape=0
enddef
To use this just write: If isLandscape then ....
def ScreenW=screen_width()
def ScreenH=screen_height()
If you need to reference the screen width several times in a program you might try this. It is a really simple way to avoid having to type the underscore and () which are not easy to get to on the screen keyboard.
Enjoy
Dale
1. As noted in the online help the def command can be either a single line or multiple lines ending in end def (enddef will also work).
2. the function name can be either a numeric variable name or a string variable name (ends with a $) and functions can be defined for either just by picking the function name.
3. Functions can be used anywhere in the program even inside of print statements or other commands.
4. Functions must be defined earlier in the program and must be in the main code (Inside a subroutine will not work).
5. A function can have any number of inputs and zero or one output.
6. If the function does not have any inputs then the parentheses are optional, unlike builtin functions which always require them even if empty ().
7. variable names and values inside a function are local, they can be duplicates of other variable names and won't effect any of the values in global names.
8. The only way to pass the answer back is to assign it to the function name.
9. Functions can be freely copied unmodified from one program to another without having to worry about the variable names.
10. Any equation can be turned into a function.
Here are some sample functions that illustrate a few things you can do with a function. (Most of these were pasted in from running code.)
The first is a slightly modified version of the mid$ shown in the other thread. The standard mid$ in other basics has an optional third argument and if missing it will automatically assume the end of the string. The def function command in this basic requires all arguments be entered. To simulate the automatic function of the original I have allowed a 0 to be entered as the third argument and use that to trigger the calculation of the location of the end of the string.
def mid$(a$,a,b)
if b=0 then b=len(a$)-a+1
mid$=substr$(a$,a-1,a+b-2)
end def
This basic does automatic conversion of strings to numbers and vice versa using the assignment statement. However sometimes you might want to convert inside another statement that is performing another calculation without having to assign the value. This function allows that.
def val(a$)=a$
The next statement seems to do nothing but it takes advantage of the automatic conversion to provide a function that doesn't seem to require an assignment. The is useful for basics that use the STR$ function natively and may embed this as part of another statement.
def str$(a)=a
Here is a numeric function that will round a number to the supplied number of digits. Often a computer provides more precision than the accuracy of the supplied data has. This could fool the user into to believing the answer is more accurate than it really is, hence the need to round the value to a reasonable accurate value.
def round(a,p)
a$=abs(int(a))
p=p-len(a$)
round=int(10^p*a+.5)/10^p
enddef
This basic supports rotating the screen but there needs to be a way for a program to determine which way the user is holding the tablet or iPhone.
def isLandscape
if screen_width() > screen_height() then isLandscape=1 else isLandscape=0
enddef
To use this just write: If isLandscape then ....
def ScreenW=screen_width()
def ScreenH=screen_height()
If you need to reference the screen width several times in a program you might try this. It is a really simple way to avoid having to type the underscore and () which are not easy to get to on the screen keyboard.
Enjoy
Dale