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 / Function Basics

Author
Message
Travis Gatlin
15
Years of Service
User Offline
Joined: 14th May 2009
Location: Oxford, Alabama
Posted: 27th Oct 2010 02:25
I cant get how to make a function, i even copied and pasted one out of some code and it said that it had a syntax error.

The Object of war is not to die for your country but make the other guy die for his
M squared
13
Years of Service
User Offline
Joined: 20th Oct 2010
Location:
Posted: 27th Oct 2010 04:34 Edited at: 27th Oct 2010 04:47
Well, i'm new so don't take anything i say to heart, but here's what i believe your problem is. When you create a function, you have to use it somewere in the program. So if your program loop(s) doesn't have the function in it, then it won't work. For instance.



If i didn't have the text
FunctionExample() in my loop it wouldn't exicute the function.... I think. If this doesn't solve your problem ignore me. I'm just taking a stab at what your problem may be because i had a similar problem.
PS. It may help if you post a code snippet of the code your useing.

details....
Hodgey
14
Years of Service
User Offline
Joined: 10th Oct 2009
Location: Australia
Posted: 27th Oct 2010 08:22
Hi, take a look at this tutorial written by TDK.

http://forum.thegamecreators.com/?m=forum_view&t=96040&b=7

A clever person solves a problem, a wise person avoids it - Albert Einstein
Travis Gatlin
15
Years of Service
User Offline
Joined: 14th May 2009
Location: Oxford, Alabama
Posted: 29th Oct 2010 04:31
What i understand of it, it is a shortcut to declare a set of codes
so this is what i came up with after reading that,


The Object of war is not to die for your country but make the other guy die for his
Indicium
16
Years of Service
User Offline
Joined: 26th May 2008
Location:
Posted: 29th Oct 2010 04:38
Okay... Lets break that down.

Function Travis'sFunction(size)

I'm not sure if you can have an apostrophe in a function for starters, but that's not really relevant here. In this function, you pass the variable size to it. In functions, all variables are local, unless they have been declared as global.

Make object cube 1,10
This makes a cube of size 10. This makes passing the variable size irrelevant. The function would be a little more functional if this line were something like:
make object cube 1,size
Although it's still completely pointless as that can be performed any ways.

endfunction 1
This will end the fucntion, and return the variable 1.

To give you a better example of how functions are useful, take this very basic example.


If you have any particular questions, feel free to ask.

Travis Gatlin
15
Years of Service
User Offline
Joined: 14th May 2009
Location: Oxford, Alabama
Posted: 29th Oct 2010 21:36
Ok, i get an error, it says "you must hit a FUNCTION declaration mid-program!" why does it do that?

The Object of war is not to die for your country but make the other guy die for his
Indicium
16
Years of Service
User Offline
Joined: 26th May 2008
Location:
Posted: 30th Oct 2010 00:07
Sorry, I should have said, the wait key command stops the program until you press a key, after that command it reaches the function, if you ever hit the function declaration mid-program, you will encounter this error.

To avoid this, in simple programs like this with no loop, place the end command after wait key.

bergice
17
Years of Service
User Offline
Joined: 5th Jun 2007
Location: Oslo,Norway
Posted: 30th Oct 2010 01:11 Edited at: 30th Oct 2010 01:12
I like to structurize my program like this:

Initialize
Load Data
Main Loop
//end of program goes here
Functions


This way you know where to put the different parts of your program, and you are less likely to get errors like the Function declaration in mid-program.

51fa1db0ec7c4af52d93a6f5d0e86bc5


Login to post a reply

Server time is: 2024-09-28 22:22:46
Your offset time is: 2024-09-28 22:22:46