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 : CMM2 - New to BASIC, I need some help with Syntax
Page 2 of 2 | |||||
Author | Message | ||||
Atomizer_Zero Senior Member Joined: 04/07/2020 Location: United KingdomPosts: 134 |
I think I'm writing functions wrong. FUNCTION myfunc(value) do stuff.. END FUNCTION In that example, value is created at the top of the program with DIM value = 0 Now if I try something like A = myFunc(10) Print A It complains that myFunc isn't declared. If I make it a SUB, it works.. I'm probably using it wrong I'd imagine... |
||||
matherp Guru Joined: 11/12/2012 Location: United KingdomPosts: 8578 |
Are you assigning the value to myfunc? e.g. a=10 print myfunc(a) end function myfunc(x) myfunc=x*x end function |
||||
CaptainBoing Guru Joined: 07/09/2016 Location: United KingdomPosts: 1985 |
Functions (generally) are expected to return values but you have to set the value in the Function before Ending Subs do stuff Also, you should define the datatypes to make things absolutely clear unless you have a good memory and command of OPTION DEFAULT so Sub MySub(x As Integer) Local Integer Fred do stuff End Sub is literally a sub program. You can declare local variables (Fred) as shown and you can pass a value in (x) as shown. If you just want some work doing (or more than one value back - assign them to global variables in the Sub) use a Sub If you need to get a single value in return, then a function is the tidiest way to achieve that, but as already pointed out unless you set its value you'll get a complaint. So Function MyFunc(x As Integer) As String Local Integer Fred do stuff MyFunc=str$(x*200) End Function so you can see we passed an integer in (x), declared that MyFunc will return a string (As String - you could miss this bit out if you declared it as MyFunc$ then the string part is implicit in the name) and finally we set it's value. Now your code will not moan about MyFunc |
||||
Atomizer_Zero Senior Member Joined: 04/07/2020 Location: United KingdomPosts: 134 |
Thanks for the detailed information matherp and CaptainBoing. I was using "myFunc(10)" instead of "myFunc = 10", for example, for the function, hence why it wasn't working. Got it all figured out now, and making some decent headway on my program. (1500 lines in so far haha). Next thing I need to work out is loading external files into an array and checking certains bytes for certain values... fun fun fun! |
||||
Geoffg Guru Joined: 06/06/2011 Location: AustraliaPosts: 3165 |
Hey Atomizer_Zero, at the beginning you said that you have read the PDFs but from the sound of it you missed this tutorial: http://geoffg.net/Downloads/Maximite/Programming_with_the_Colour_Maximite_2.pdf It is worth reading and covers most of your questions. Geoff Geoff Graham - http://geoffg.net |
||||
Atomizer_Zero Senior Member Joined: 04/07/2020 Location: United KingdomPosts: 134 |
Hi GeoffG. Honestly, I have read through it, but without actually doing the things, it doesn't really stick. I really should be trying to use it more and I will from now on, I promise. |
||||
CaptainBoing Guru Joined: 07/09/2016 Location: United KingdomPosts: 1985 |
while we are at it, note that BASIC uses "=" for both comparison and assignation so in Java you might have fred=10 that will work the same in BASIC likewise If fred==10{ would become If Fred=10 then (think I got those the right way round) |
||||
Atomizer_Zero Senior Member Joined: 04/07/2020 Location: United KingdomPosts: 134 |
It's actually quite difficult for me to get me head out of the realm of Java lol. I have to keep rethinking how things work logically. I'm making some progress on reading in a data file, but there's a few things I can't quite get right.. For starters, I need to be able to open the file and save into an array, so that I can use the array to manipulate the data. I also need to be able to read from the array in hex. The size of the data file wont be known until it is opened, so I need to allocate the array with enough memory to handle whatever size the data file is. With mmbasic for dos, i'm limited to 512k of memory. That's 10 times less than the CMM2 (or thereabouts). So, in the meantime, I can at least limit it to the first 16 bytes or something of the data file, for testing purposes. open "data.dat" for random as #1 for i = 0 to 15 seek #1, i+1 print input$(1,#1); next i close #1 now, this works. it prints out the data. as a string. also, local data$ open "data.dat" for random as #1 data$ = input$(16,#1) print data$ close #1 Also works in much the same way, and of course, its a string. So, how can I print out the string as hex values OR how can I read in the data like this as integers (AS INTEGER doesnt work with "input(16,#1)" as far as I can tell)? Sorry for hundreds of questions on what is probably simple stuff... I am learning, it's just a lot of the conveniences of java arent here, which is making this a little more difficult than I expected. Thats a good thing though, I needed a new challenge and hopefully this will make me a better programmer in the long run. Edited 2020-07-07 01:15 by Atomizer_Zero |
||||
matherp Guru Joined: 11/12/2012 Location: United KingdomPosts: 8578 |
Check through the functions section of the manual VAL and HEX$ are your friends |
||||
Atomizer_Zero Senior Member Joined: 04/07/2020 Location: United KingdomPosts: 134 |
I must be blind. Alright, I'm going to print this manual out lol. I hate reading through PDF's, as I can never remember where anything is! Thanks so much, honestly. I feel useless right now lol |
||||
Atomizer_Zero Senior Member Joined: 04/07/2020 Location: United KingdomPosts: 134 |
While i'm still waiting for my CMM2 to be dispatched, I've been writing a whole bunch of code and I've come across a situation where I might need to only include certain files based on a select case. Is this possible on the CMM2? for example, SELECT CASE (file) CASE 0: #INCLUDE "FILE.INC" CASE 1: #INCLUDE "FILE2.INC" END SELECT |
||||
TassyJim Guru Joined: 07/08/2011 Location: AustraliaPosts: 5898 |
No Includes are done all at the beginning before the program is run. Jim VK7JH MMedit  MMBasic Help |
||||
Atomizer_Zero Senior Member Joined: 04/07/2020 Location: United KingdomPosts: 134 |
I figured as much. Just trying to think of some inventive ways of only iterating through code that I actually need at that specific time. I'll just use select cases. Gonna get messy lol Cheers |
||||
Page 2 of 2 |
Print this page |