Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Function declarations not working (sometimes) (*solved*)
#1
Okay, I think I figured out what my other problem is. Smile

In 1.3 I could call a function in the code before it was declared providing I declared the function with a prototype at the top of the class. So this would've worked okay, despite the 'addWibble' function appearing after the call to 'addWibble':

Code:
'function prototypes
declare function addWibble(msg as String) as String

'call addWibble even though it's declared down there vvvv
dim newMsg as String
newMsg = addWibble("Hello")
print newMsg

'return a string suffixed with 'wibble'
function addWibble(msg as String) as String
    dim newString as String
    newString = msg
    newString = newString + "wibble"
    return newString
end function

In 1.4 this won't compile, it seemingly ignores the 'declare' prototype of the function and doesn't recognise 'addWibble' when it first encounters it, giving an error like this:

Code:
TestFunction.bas:8: 'addWibble' is neither an array nor a function.

If you move the call below function like this then everything is fine:

Code:
'return a string suffixed with 'wibble'
function addWibble(msg as String) as String
    dim newString as String
    newString = msg
    newString = newString + "wibble"
    return newString
end function

'call addWibble even though it's declared down there vvvv
dim newMsg as String
newMsg = addWibble("Hello")
print newMsg

HOWEVER.... it's a bit more complicated than that. It seems that I only see this effect if the function returns a String. If I quickly switch the function to return a ubyte instead then everything compiles fine.

Code:
'function prototypes
declare function addWibble(msg as string) as ubyte

'call addWibble even though it's declared down there vvvv
dim newMsg as ubyte
newMsg = addWibble("test")
print newMsg

'return the number 1
function addWibble(msg as string) as ubyte
    return 1
end function

Hope that makes sense! Big Grin
Reply
#2
Thanks, LTee.
Will check it ASAP.
Reply
#3
Ok. It seems fixed now.
Please download ZXBasic-s1893, and tell me if it works.
Reply
#4
Hi, sorry for the delay - I've been away on my holidays. Smile

Just tried the new version out and you'll be pleased to hear I agree - it's working perfectly! In fact I can now compile all of my old code with the latest version without any obvious errors, so that's brilliant! Thanks, boriel!
Reply
#5
LTee Wrote:Hi, sorry for the delay - I've been away on my holidays. Smile

Just tried the new version out and you'll be pleased to hear I agree - it's working perfectly! In fact I can now compile all of my old code with the latest version without any obvious errors, so that's brilliant! Thanks, boriel!
Thanks to you for this!! :roll:
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)