Page 1 of 1

more sample functions

Posted: Fri Mar 22, 2013 8:00 pm
by Dalede
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

Re: more sample functions

Posted: Fri Mar 22, 2013 8:54 pm
by Mr. Kibernetik
Very nice expanded survey of DEF implementation.

But I wanted to add: strictly speaking, assigning the value to function name is not the only way to get result back from function. You can also use arrays as function parameters, and arrays are treated as references. This means that modifying array values in a function has global effect - array values will be changed for outside part of function code. This can be a way to return many values at a time from the function.

Re: more sample functions

Posted: Fri Mar 22, 2013 9:28 pm
by Dalede
Thanks very much for the clarification. I haven't tried working with dimensioned objects. Sounds like a useful feature.

I also forgot to mention that RETURN can be used inside a function definition to exit the function before the last statement.

Dale

Re: more sample functions

Posted: Sat Mar 30, 2013 1:46 am
by Dalede
Did I ever mention that I really like the def function capability of this basic. Here is another neat function.

Note that the restore statement for data cannot currently (as of 1.5) accept a label so there is not a way to restore it to the list except back at the beginning. A function to the rescue. Here is a function that uses a data statement inside the function. It does not need a restore and will not interfere with other data statements. It just works.

def weekday$
for i = 0 to current_day()
read a$
next i
weekday$=a$
data "Sunday","Monday","Tuesday","Wednesday"
data "Thursday","Friday","Saturday"
end def

This function returns the current day of the week. A similar function could easily be written to return the month of the year. Note that I had to put quotes around the names of the week days. ECMA55 says it should work with unquoted strings but I couldn't get it to work that way.

Dale

Re: more sample functions

Posted: Fri Apr 12, 2013 9:08 am
by Elchoud
Many Thanks Dalede for Your replies & posts.
Elchoud

Re: more sample functions

Posted: Sat Nov 16, 2013 2:08 am
by Dalede
Note that some of the functions defined in this thread and the earlier thread referenced in the first post have now been implemented in the code itself. If you have them in some of your programs you will need to remove them when you see the error message. They work the same so no other coding problems should occur as a result of this.

Dale