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 : Im confused about function syntax.
Author | Message | ||||
Paul_L Guru Joined: 03/03/2016 Location: United StatesPosts: 769 |
Hi guys, When you define a function the function name is used to return a value. It therefore becomes a variable. Does it have to be pre-declared with DIM or LOCAL? Will this crash because CRUNCH is not pre-declared? Will it crash because D$ and E$ are assigned as parameters before being declared LOCAL? OPTION EXPLICIT DIM A$="Hello", B$="world.", C$ C$=CRUNCH(A$,B$) : PRINT C$ FUNCTION CRUNCH(D$,E$) LOCAL D$,E$ CRUNCH=D$+" "+E$ END FUNCTION Will it fail because CRUNCH is not typed as a string? Should it have been this, FUNCTION CRUNCH$(D$,E$) or possibly this?FUNCTION CRUNCH(D$,E$) AS STRING Paul in NY |
||||
Grogster Admin Group Joined: 31/12/2012 Location: New ZealandPosts: 9082 |
Hi there. MicroMite manual, page 42 explains functions in a bit more detail. Basically, using your example: OPTION EXPLICIT DIM A$="Hello", B$="world.", C$ C$=CRUNCH$(A$,B$) : PRINT C$ FUNCTION CRUNCH$(D$,E$) CRUNCH$=D$+" "+E$ END FUNCTION Your original example would fail, because CRUNCH is not defined as a string variable, so when you try to combine D$ and E$ in the function, and pass it back as CRUNCH$, the code falls over. I have not tested this, but I expect that is your problem. Smoke makes things work. When the smoke gets out, it stops! |
||||
WhiteWizzard Guru Joined: 05/04/2013 Location: United KingdomPosts: 2794 |
I too am confused Trying all the above examples, nothing works (although trying on MZ as surrounded by them!). Will keep going until I see how to make it work as expected!! For everything Micromite visit micromite.org Direct Email: whitewizzard@micromite.o |
||||
WhiteWizzard Guru Joined: 05/04/2013 Location: United KingdomPosts: 2794 |
IF you add PRINT CRUNCH$ at the end of the Function, you indeed see the result. But no matter what I try back in the 'main code', it is as if the contents of CRUNCH$ are wiped out. Can any 'expert' help out here please WW For everything Micromite visit micromite.org Direct Email: whitewizzard@micromite.o |
||||
Geoffg Guru Joined: 06/06/2011 Location: AustraliaPosts: 3167 |
You should not declare parameters supplied to the function as LOCAL. What LOCAL does is create a new variable that is local to the function and hides previous declarations. So, when you declared D$ and E$ as LOCAL you hid their previous value. WW, I presume that you are testing PaulL's code. Grogster's code should work perfectly (good on you Grogs ). Geoff Geoff Graham - http://geoffg.net |
||||
matherp Guru Joined: 11/12/2012 Location: United KingdomPosts: 8605 |
crunch$ can be considered as a LOCAL variable automatically defined within the function so you cannot use CRUNCH$ outside the function You must always use it as the function call i.e. c$=crunch$(a$,b$) This is why even in a function that takes no parameters you must include the brackets in a call to the function e.g. [code] print fred$() end function fred$() fred$="fred" end function [/code] |
||||
WhiteWizzard Guru Joined: 05/04/2013 Location: United KingdomPosts: 2794 |
Neither Paul's or Grogster's code works here on an MMX - not sure if we're uncovering a bug. Peter; can you give the equivalent of Paul's example as you know will work so that I can then test it on the MMX144 For everything Micromite visit micromite.org Direct Email: whitewizzard@micromite.o |
||||
matherp Guru Joined: 11/12/2012 Location: United KingdomPosts: 8605 |
Option explicit Option default none Dim string a="Hello", B$="world", c$ c$=crunch(a$,b$) Print c$ End Function crunch(D$, E$) As string crunch=d$+" "+e$ End Function works Option explicit Option default none Dim string a="Hello", B$="world", c$ c$=crunch$(a$,b$) Print c$ End Function crunch$(D$, E$) crunch$=d$+" "+e$ End Function works Option explicit Dim string a="Hello", B$="world", c$ c$=crunch$(a$,b$):Print c$ End Function crunch$(D$, E$) crunch$=d$+" "+e$ End Function works |
||||
WhiteWizzard Guru Joined: 05/04/2013 Location: United KingdomPosts: 2794 |
Peter, Something strange going on here. I ran Grogs' example above and it gave no result (I had even added the END command after the 'main code'). I then worked your examples and all worked - BUT your last example is the same as Grogsters? Trying to recreate the issue but no joy yet I definitely had it typed correctly but had many comments in as were trying different things to get it to work. Also some blank lines in code. I know this shouldn't make any difference whatsoever BUT it did not work initially until after I ran your three examples :-( Not going to spend too long on this, but it is now annoying me . . . . WW For everything Micromite visit micromite.org Direct Email: whitewizzard@micromite.o |
||||
JohnS Guru Joined: 18/11/2011 Location: United KingdomPosts: 3678 |
It's not the same. e.g. his has DIM STRING John |
||||
WhiteWizzard Guru Joined: 05/04/2013 Location: United KingdomPosts: 2794 |
You are correct, but then I went on to remove 'STRING' and it still worked (and without STRING, it is the same as Grogs' example). I should have been clearer - sorry. Also 'a=' and 'a$=' - all options played with initially and nothing worked. I had just loaded the MMBASIC firmware, and sometimes I see strange initial behaviour until the MM is reset. Perhaps it is another one of these scenarios which are so difficult to replicate without a ton of time thrown in refreshing the PIC each time! Anyway, I will see if it crops up again . . . . For everything Micromite visit micromite.org Direct Email: whitewizzard@micromite.o |
||||
matherp Guru Joined: 11/12/2012 Location: United KingdomPosts: 8605 |
no but that variant works as well Option explicit Dim a$="Hello", B$="world", c$ c$=crunch$(a$,b$):Print c$ End Function crunch$(D$, E$) crunch$=d$+" "+e$ |
||||
Paul_L Guru Joined: 03/03/2016 Location: United StatesPosts: 769 |
OK, now I'm really confused. Geoff said don't declare parameters supplied to a function as LOCAL because that hides the passed parameters. I understand that. The parameters are passed by reference which means they are just pointers to the same spaces pointed to by the original variables in the calling routine. If you modify the parameters in the function they will change in the calling routine even if they have different names there. I also understand that that Crunch$ is created in the function as a LOCAL and the value is passed back to the calling code to be assigned to C$, therefore Crunch$ should not be used as a variable elsewhere in the calling program. It seems clear to me. I was initially just confused about the function "automatically" creating crunch$ with its own memory, and the parameters, d$ and e$, as pointers to the memory locations allocated to a$ and b$. But why can't WW get it to work? Paul in NY |
||||
WhiteWizzard Guru Joined: 05/04/2013 Location: United KingdomPosts: 2794 |
That was only initially immediately after loading the MMBASIC .hex file. After a reset (button press) all worked as expected. For everything Micromite visit micromite.org Direct Email: whitewizzard@micromite.o |
||||
Print this page |