Home
JAQForum Ver 24.01
Log In or Join  
Active Topics
Local Time 01:34 27 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 : MMB4W: Function error, possible bug?

Author Message
LeoNicolas

Guru

Joined: 07/10/2020
Location: Canada
Posts: 479
Posted: 06:11am 24 Dec 2023
Copy link to clipboard 
Print this post

The following code ends with the error: Error in line 15: FUNC is not declared.


option explicit

main()

sub main()
   local i%

   for i%=0 to 4
       func1()
   next
end sub

function func1() as integer
   func1=10
end function


If I change the code as follows it works fine:


option explicit

main()

sub main()
   local i%, ret

   for i%=0 to 4
       ret=func1()
   next
end sub

function func1() as integer
   func1=10
end function
 
TassyJim

Guru

Joined: 07/08/2011
Location: Australia
Posts: 6098
Posted: 07:13am 24 Dec 2023
Copy link to clipboard 
Print this post

It's a function so it needs to do something with the return.
Because you had no var to accept the return, it went looking for a SUB

Jim
VK7JH
MMedit   MMBasic Help
 
Geoffg

Guru

Joined: 06/06/2011
Location: Australia
Posts: 3194
Posted: 08:14am 24 Dec 2023
Copy link to clipboard 
Print this post

It is tempting to think that something not working as you expected as a bug in the compiler/interpreter - but 99.999% of the time it is not.

We all fall into this trap from time to time.

Geoff
Geoff Graham - http://geoffg.net
 
Mixtel90

Guru

Joined: 05/10/2019
Location: United Kingdom
Posts: 6792
Posted: 08:26am 24 Dec 2023
Copy link to clipboard 
Print this post

Oh yes....
Tell me about it....   :(
I'm forever being outsmarted by a bit of silicon!
Mick

Zilog Inside! nascom.info for Nascom & Gemini
Preliminary MMBasic docs & my PCB designs
 
LeoNicolas

Guru

Joined: 07/10/2020
Location: Canada
Posts: 479
Posted: 03:22pm 24 Dec 2023
Copy link to clipboard 
Print this post

Sorry for my false positive bug post.

This is interesting. I can imagine why It happens. Probably the variable pointer is used directly for the func routine to store the return value, instead of having another memory allocation just for holding the value while the function is executed. Of course I can be completely wrong.
 
matherp
Guru

Joined: 11/12/2012
Location: United Kingdom
Posts: 9118
Posted: 03:33pm 24 Dec 2023
Copy link to clipboard 
Print this post

  Quote  Of course I can be completely wrong.


Yes. The first thing in a line is always an in-built command or a user SUBROUTINE. A function must always be assigned to something, typically using the in-built command LET which is the default if there is an =

i.e. ret=func1() is actually interpreted ad LET ret=func1()

Func1() at the start of a line fails the interpreter lookup for either an in-built command or a use subroutine hence the error
 
Print this page


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

© JAQ Software 2024