Forum - MCS Electronics

 

FAQFAQ SearchSearch RegisterRegister Log inLog in

passing string or value to funtion.

 
Post new topic   Reply to topic    www.mcselec.com Forum Index -> BASCOM-AVR
View previous topic :: View next topic  
Author Message
snipsnip

Bascom Member



Joined: 10 Feb 2014
Posts: 74
Location: Melbourne

australia.gif
PostPosted: Tue Feb 24, 2015 9:56 pm    Post subject: passing string or value to funtion. Reply with quote

Hi all,

I've got a function that I want to accept either a string or value passed to it.

Question is there an easy way to figure out what data type a variable is?

I'm thinking of using overlay and checking for null terminators to identify strings.
I'd rather not though - if there's an easier way.

Cheers

(BASCOM-AVR version : 2.0.7.8 )
Back to top
View user's profile
AdrianJ

Bascom Expert



Joined: 16 Jan 2006
Posts: 2483
Location: Queensland

australia.gif
PostPosted: Wed Feb 25, 2015 12:05 am    Post subject: Reply with quote

Checking for a null terminator will often fail. Consider that an integer of value say 65 decimal will be stored in memory as 2 bytes hex 41 00. So it will look exactly like a string of value "A", with its null terminator.

Bascom does not have the VB variable type "Any", or "Variant" for good reason.

Maybe the best you can do is define a parameter as string, and if you want to pass a number, convert it to string first. Seems a waste, since it costs time to do the conversions.

What exactly are you trying to achieve ?

_________________
Adrian Jansen
Computer language is a framework for creativity
Back to top
View user's profile Visit poster's website
snipsnip

Bascom Member



Joined: 10 Feb 2014
Posts: 74
Location: Melbourne

australia.gif
PostPosted: Wed Feb 25, 2015 2:01 am    Post subject: Reply with quote

Hi Adrian,

Thanks for the reply - I'm just trying to offload the "string to val" and "val to string" conversions to
the called functions rather than the main program loop.

Not a big deal, just would be easier - my brain gets muddled trying to keep track of data types Embarassed


How does the Bascom "time and date" functions do it? they can accept either.

Cheers
Back to top
View user's profile
AdrianJ

Bascom Expert



Joined: 16 Jan 2006
Posts: 2483
Location: Queensland

australia.gif
PostPosted: Wed Feb 25, 2015 3:19 am    Post subject: Reply with quote

Never looked to see how the Date/Time functions work in the library, but I would guess they parse the input first, looking for the separators ":" or "/" or "-", then figure out what to do with them, or if not present, assume a numeric. Easier when you have a relatively fixed format to look for.

Personally I prefer the relatively 'strong' data typing that Bascom has. At least then you are not guessing what the type is, or how it might be converted. But I come from an ASM background, where its a lot simpler.

My pet hate is that you can do things like Print A in Basic ( and including Bascom ) when you dont know what type of variable A is. Can lead to lots of confusion about the difference between the internal storage and external representation of a value as a string.

_________________
Adrian Jansen
Computer language is a framework for creativity
Back to top
View user's profile Visit poster's website
Display posts from previous:   
Post new topic   Reply to topic    www.mcselec.com Forum Index -> BASCOM-AVR All times are GMT + 1 Hour
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You cannot attach files in this forum
You cannot download files in this forum