Home
JAQForum Ver 24.01
Log In or Join  
Active Topics
Local Time 21:22 26 Nov 2024 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 : Allow Simple Variable or Array as parameter?

Author Message
vegipete

Guru

Joined: 29/01/2013
Location: Canada
Posts: 1109
Posted: 03:44am 10 Jan 2024
Copy link to clipboard 
Print this post

Numerous MMBasic commands allow either simple variables or arrays to be passed as parameters. Is there any way to accomplish this in our own programs? Could you pass the location of a variable, then look there to determine what kind of variable it is? (Simple or array, number of dimensions, int/float/string etc.) For that to be truly useful, we'd need a way to make a local variable point to somewhere else, and in doing so, possibly change its type. Of, course, when the routine ends, garbage collection gets interesting - don't dump the wrong thing!  

As an aside, if you attempt BOUND(simplevar()) you get an error as simplevar is not an array. BOUND(simplevar) works as expected but then fails if simplevar is actually an array. I suppose you could ONERR SKIP to trap it, but that is kinda evil.
Edited 2024-01-10 13:47 by vegipete
Visit Vegipete's *Mite Library for cool programs.
 
toml_12953
Guru

Joined: 13/02/2015
Location: United States
Posts: 339
Posted: 12:49pm 10 Jan 2024
Copy link to clipboard 
Print this post

  vegipete said  
As an aside, if you attempt BOUND(simplevar()) you get an error as simplevar is not an array. BOUND(simplevar) works as expected but then fails if simplevar is actually an array. I suppose you could ONERR SKIP to trap it, but that is kinda evil.



Have you tried BOUND(simplevar(),1) for the first dimension?

Here's an example from the CMM2 manual. Maybe it works for the 'mite as well?

DIM myarray(44,45)
BOUND(myarray(),2) will return 45
 
EDNEDN
Senior Member

Joined: 18/02/2023
Location: United States
Posts: 118
Posted: 02:57pm 10 Jan 2024
Copy link to clipboard 
Print this post

It seems to me the BOUND() function should be tolerant of being passed either an array name or a variable name.   And if that were true, asking for the size of the first dimension would tell you what you were dealing with.

Specifically, It would make sense to me to allow either BOUND(xyz,1) or BOUND(xyz(),1) if xyz is an array.  And of course, both of those should return the same value.  That would allow the code to adapt and easily handle the situation.
Edited 2024-01-11 13:14 by EDNEDN
 
William Leue
Guru

Joined: 03/07/2020
Location: United States
Posts: 393
Posted: 02:46pm 11 Jan 2024
Copy link to clipboard 
Print this post

It's a bit clumsy, but you could always do:


ON ERROR SKIP 1
N = BOUND(PASSED_VAR)
IF MM.ERRNO <> 0 THEN
 ' You know it's not an array
ELSE
 ' You know it's an array
END IF


-Bill
 
Print this page


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

© JAQ Software 2024