Sorry your browser is not supported!

You are using an outdated browser that does not support modern web technologies, in order to use this site please update to a new browser.

Browsers supported include Chrome, FireFox, Safari, Opera, Internet Explorer 10+ or Microsoft Edge.

Newcomers DBPro Corner / Return a value from a function

Author
Message
jerryd
11
Years of Service
User Offline
Joined: 20th Feb 2013
Location:
Posted: 27th Apr 2013 07:26 Edited at: 27th Apr 2013 07:29
Darkbasic forum,

x as integer

x = getKey()

When I try this I get a compiler error at "x = getkey()"

function getKey()
do
if inkey$() = "1" then exitfunction 1
if inkey$() = "2" then exitfunction 2
loop
endfunction

Of course there's more to the code but if this illegal how
can I do this without creating a global variable.

jerryd
Burning Feet Man
16
Years of Service
User Offline
Joined: 4th Jan 2008
Location: Sydney, Australia
Posted: 27th Apr 2013 10:07 Edited at: 27th Apr 2013 10:14
Hey jerryd,

Welcome to DBPro! It's so awesome, I hope you have as much fun with it as I do.

Below is some code that I quickly wrote which will hopefully assist you in what you're trying to do.



Help build an online DarkBASIC Professional help archive.
DarkBasic Help Wikia
Rudolpho
18
Years of Service
User Offline
Joined: 28th Dec 2005
Location: Sweden
Posted: 27th Apr 2013 18:27 Edited at: 27th Apr 2013 18:28
Your example will not compile as the functions return type is first deduced from whatever follow the endfunction keyword. In your case nothing follows so the function will be assumed to return nothing. Then your exitfunction calls are encountered and seen to attempt to return a value, thus you have a conflict and the compilation will be aborted.
Just put a default return value after endfunction like Burning Feet Man's snippet suggests.


In case you're coming from a C-style syntaxed language (like java or what have you), what you're trying to do is similar to this:



"Why do programmers get Halloween and Christmas mixed up?"

Login to post a reply

Server time is: 2024-03-29 09:12:12
Your offset time is: 2024-03-29 09:12:12