Smart BASIC Programming. Lesson 3

Post Reply
User avatar
Mr. Kibernetik
Site Admin
Posts: 4782
Joined: Mon Nov 19, 2012 10:16 pm
My devices: iPhone, iPad, MacBook
Location: Russia
Flag: Russia

Smart BASIC Programming. Lesson 3

Post by Mr. Kibernetik »

Lesson 3 - User functions

One of important smart BASIC features is user functions. User functions can work both as functions and as commands.

In program:

Code: Select all

DEF F (X,Y) = SQR (X^2 + Y^2)
A = 3 ! B = 4
PRINT "For catheti";A;"and";B;"hypotenuse is";F (A,B)
user function F is set to expression SQR (X^2 + Y^2), where X and Y are function parameters. Using function F with different parameters will give corresponding results.

User functions can contain several lines:

Code: Select all

DEF F (X,Y)
X2 = X^2
Y2 = Y^2
F = X2 + Y2
END DEF
And this is an example of using user function as a command:

Code: Select all

'function outputs current time
DEF CUR_TIME
H = CURRENT_HOUR()
M = CURRENT_MINUTE()
S = CURRENT_SECOND()
TEXT CLEAR
PRINT H & ":" & M & ":" & S
END DEF
'infinite loop
LOOP: 'label
CUR_TIME 'function call
PAUSE 1
GOTO LOOP 'going to label
As you see, in this program user function neither has any parameters nor returns any result. It just performs some action here, so there is no need to use brackets in function definition or in function call - anyway they will be empty.

EXERCISE 4
Solve one of previous exercises using user functions.

Chooch912
Posts: 35
Joined: Thu Apr 12, 2018 6:16 pm
My devices: iPad
Flag: United States of America

Re: Smart BASIC Programming. Lesson 3

Post by Chooch912 »

When I entered the program example below and ran it I get a message indicating the following error- WHY?

"DUPLICATED FUNCTION"
"DEF TIME"

'function outputs current time
DEF TIME
H = CURRENT_HOUR()
M = CURRENT_MINUTE()
S = CURRENT_SECOND()
TEXT CLEAR
PRINT H & ":" & M & ":" & S
END DEF
'infinite loop
LOOP: 'label
TIME 'function call
PAUSE 1
GOTO LOOP 'going to label

User avatar
Mr. Kibernetik
Site Admin
Posts: 4782
Joined: Mon Nov 19, 2012 10:16 pm
My devices: iPhone, iPad, MacBook
Location: Russia
Flag: Russia

Re: Smart BASIC Programming. Lesson 3

Post by Mr. Kibernetik »

Chooch912 wrote:
Sat Mar 23, 2019 1:06 am
When I entered the program example below and ran it I get a message indicating the following error- WHY?

"DUPLICATED FUNCTION"
"DEF TIME"

'function outputs current time
DEF TIME
H = CURRENT_HOUR()
M = CURRENT_MINUTE()
S = CURRENT_SECOND()
TEXT CLEAR
PRINT H & ":" & M & ":" & S
END DEF
'infinite loop
LOOP: 'label
TIME 'function call
PAUSE 1
GOTO LOOP 'going to label
Because function TIME already exists. Please look at documentation, section "Misc".

Chooch912
Posts: 35
Joined: Thu Apr 12, 2018 6:16 pm
My devices: iPad
Flag: United States of America

Re: Smart BASIC Programming. Lesson 3

Post by Chooch912 »

I did read the "Misc" section about TIME, but I was just trying the example you wrote and copied it exactly. That is when I got the error. I appreciate your prompt reply, but how can I get the example to work?

User avatar
Mr. Kibernetik
Site Admin
Posts: 4782
Joined: Mon Nov 19, 2012 10:16 pm
My devices: iPhone, iPad, MacBook
Location: Russia
Flag: Russia

Re: Smart BASIC Programming. Lesson 3

Post by Mr. Kibernetik »

Chooch912 wrote:
Sat Mar 23, 2019 5:23 pm
I did read the "Misc" section about TIME, but I was just trying the example you wrote and copied it exactly. That is when I got the error. I appreciate your prompt reply, but how can I get the example to work?
Probably this example was created before built-in function TIME was implemented. Thank you for your note, example is corrected.

Chooch912
Posts: 35
Joined: Thu Apr 12, 2018 6:16 pm
My devices: iPad
Flag: United States of America

Re: Smart BASIC Programming. Lesson 3

Post by Chooch912 »

Great! I really am having fun with this program and look forward to spending many of my retirement "hours" learning all I can about all the commands so I can create a unique program for sighting in my airgun rifle scopes!
Thank you for your help.

User avatar
GeorgeMcGinn
Posts: 435
Joined: Sat Sep 10, 2016 6:37 am
My devices: IPad Pro 10.5in
IMac
Linux i386
Windows 7 & 10
Location: Venice, FL
Flag: United States of America
Contact:

Re: Smart BASIC Programming. Lesson 3

Post by GeorgeMcGinn »

***** POST MOVED TO OTHER TOPICS *****
Last edited by GeorgeMcGinn on Thu Mar 28, 2019 9:47 am, edited 1 time in total.
George McGinn
Computer Scientist/Cosmologist/Writer/Photographer
Member: IEEE, IEEE Computer Society
IEEE Sensors Council & IoT Technical Community
American Association for the Advancement of Science (AAAS)

User avatar
Mr. Kibernetik
Site Admin
Posts: 4782
Joined: Mon Nov 19, 2012 10:16 pm
My devices: iPhone, iPad, MacBook
Location: Russia
Flag: Russia

Re: Smart BASIC Programming. Lesson 3

Post by Mr. Kibernetik »

GeorgeMcGinn wrote:
Thu Mar 28, 2019 8:25 am
WOW, I had an adhoc program that I wrote that does a similar thing, but it produces text only, and currently I input where each shot goes in order of fire.

Since I reload shotgun shells, rile and pistol cartridges, I also factor in wind speed/direction, altitude, differences in height between gun and target, bullet style and weight, powder type and amount, and primer to name a few. It was one of the first programs I worked on. I use some of your example code and right now I use a weather provider for all weather conditions.

My future would be to attach by WIFI my Olympus OM-1D (right now I can only use this in TechBASIC as it can watch an image live (setting up the camera as a sensor) and the program will be modified to measure the distance from a dot I put on the target in the center of my x-ring. I do not know if in the future you will allow IoT and code that allows sensor input of raw data. I'm not asking for this to be in the first releases, but will the product evolve in that direction?

I have all the algorithms, some are not in the program, others exist in tables I get from manufacturers of reloading components, which, unlike an air rifle, bullet weight and type, powder used and how much, how fast it burns, and even the primer effects the speed which the powder ignights.

I look forward to the new BASIC and I too can spend my free time on what I would like to do.

George






Chooch912 wrote:
Sat Mar 23, 2019 7:32 pm
Great! I really am having fun with this program and look forward to spending many of my retirement "hours" learning all I can about all the commands so I can create a unique program for sighting in my airgun rifle scopes!
Thank you for your help.
This is another topic. This topic is about BASIC lesson.
If you want to discuss future BASIC development, then please use dedicated topic instead. I am very interested to know what possibilities are required for IoT and your camera (you meant Olympus OM-D E-M1?).

Post Reply