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 / Functions, trouble is its not (functioning that is)

Author
Message
Toxitalk
18
Years of Service
User Offline
Joined: 13th Feb 2006
Location:
Posted: 15th Feb 2006 22:24
Its come to the stage in my program where I want to function areas of code. The trouble is when I move the code into a function block it stops working.

The function I want to create will be one to detect player input.

I am using DB classic.

do
moveplayer(x#)
loop

function moveplayer(x#)
if leftkey()=1 then x#=x#-0.1
if rightkey()=1 then x#=x#+0.1
endfunction x#

Any help would be great

Cheers

Mark

Smoke me a kipper.....
SirFire
19
Years of Service
User Offline
Joined: 4th Apr 2005
Location: North America
Posted: 16th Feb 2006 09:30 Edited at: 16th Feb 2006 10:14
When you call the function with moveplayer(x#), you are passing the variable x# to be used by the function. At the end of your function, you are returning x# back to the caller, yet your caller is not written properly to accept return values.

If you had put x#=moveplayer(x#), then you wouldn't have a problem.

So change that line to read x#=moveplayer(x#).

Alternately, you could declare x as a global variable, and do away with passing the variables to/from the function.

Just remember, to pass data into a function, call it like DOTHIS(X). If you need data to be returned from the function, use A=DOTHIS(X) and also put a return value at the end of your ENDFUNCTION. And for functions to have their variable scope seen outside of the function (without passing the data directly), you must use global variables.

Toxitalk
18
Years of Service
User Offline
Joined: 13th Feb 2006
Location:
Posted: 16th Feb 2006 10:16
Thanks.

The funny thing was I was playing with functions and ended up coping the format from the manual.

Pity the manual isnt a bit more verbose as it would have saved this question.

once again cheers.

Mark.

Smoke me a kipper.....
SirFire
19
Years of Service
User Offline
Joined: 4th Apr 2005
Location: North America
Posted: 17th Feb 2006 07:27
No prob. The help file truly lacks detail in command descriptions, directly relavent example code, and is very much outdated. Hopefully this will change soon.

Login to post a reply

Server time is: 2024-09-24 15:29:12
Your offset time is: 2024-09-24 15:29:12