'Function' can only appear in the main program

BlitzPlus Forums/BlitzPlus Beginners Area/'Function' can only appear in the main program

ThePict(Posted 2013) [#1]
Has anyone else seen this error when trying to run some code?

'Function' can only appear in the main program


My code was working fine, then I added more code to it and it won't run at all now giving me this unhelpful error message


Yasha(Posted 2013) [#2]
It means the compiler is seeing a function definition within some other block construct (type, If/EndIf, another function, etc).

If you have a really large main loop, check that you didn't inadvertently define a function within it when part of the loop was offscreen or something?


Floyd(Posted 2013) [#3]
You probably began a block but forgot to end it. Example:
For n = 1 To 5

Function nothing()
End Function
The missing Next is the real error but the message is misleading. The function definition seems to be inside a For/Next loop. The compiler has seen For and thinks it is inside the loop until it sees Next, which it never does.


ThePict(Posted 2013) [#4]
Good call Floyd, I remember seeing this before now you mention it. The message is a bit misleading, I was looking for a bug inside my function...

Sure enough, a two minute scan of my code reveals a missing Next. I spent over two fruitless hours looking for bug elsewhere. On the plus side I tidied up my code a lot, it had gotten a bit sloppy.

Watch out for my game: One Million 2-Minute Puzzles coming soon!!!