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.

DarkBASIC Discussion / Question about functions

Author
Message
Yodaman Jer
User Banned
Posted: 18th Oct 2008 19:46
Sorry for the noobish question, everybody. I've been programming for a year and I still don't quite know how functions...function.

I have a couple of questions. First, does using functions actually keep your game running fast?

Second, how do you call them? I think I read somewhere that you use gosub, but I'm not certain.

Again, I apologize for noobish-ness. I can't believe I waited this long to ask about functions.

Yodaman Jer

That guy with no idea of what to say here. Except that he is a geek, and likes cheese. And pickles. But mostly cheese.
Sinani201
18
Years of Service
User Offline
Joined: 16th Apr 2007
Location: Aperture Science Enrichment Center
Posted: 18th Oct 2008 20:33
I don't know if functions make your games go faster, but they do make them much more organized. Here's how they work:

Lets say you make a function called "make_box" Here's how you do that:



Now, lets say you want it to take certain perimeters, so that the box can be different depending on how you call it. For a box, our perimeters would be l,r,t,b. Here's how you'd do that:



Now, you have to put stuff in the function. Since the function will make a box, we'll tell it to use the BOX command:



Now, look at the code. Our BOX command takes the perimeters l,r,t,b, and those are also the perimeters of the function. I also ended the function with ENDFUNCTION, which, ends the function. Now, we have to call the function:



Now, it is called. Notice how I put in the numbers as perimeters. Those numbers correspond with the perimeters you put when making the function; hence l=5, r=7, r=8, and b=4. Then, the box command knows what those variables are, so you'd be calling BOX 5,7,8,4.

Those are the basics of functions. Note that you don't have to use numbers for this; you can also use strings and floats, as well.


"I reveal my trap card, GEORGE DUBYA BUSH!
America loses 2000 Life Points! America loses." -Deucalion2
Yodaman Jer
User Banned
Posted: 18th Oct 2008 20:51
Thanks Sinani! I understand how that works. I'll give it a shot!

Yodaman Jer

That guy with no idea of what to say here. Except that he is a geek, and likes cheese. And pickles. But mostly cheese.
BN2 Productions
21
Years of Service
User Offline
Joined: 22nd Jan 2004
Location:
Posted: 19th Oct 2008 01:31
Here is a couple big things about using functions:

1. Don't go crazy, if you end up having 6+ parameters, you could probably just use a subroutine (using gosub).

2. Variables are only local. That means that variables in your main code won't have any value in the functions that you make unless....

3. Arrays, due to a glitch, are global variables. This means that array(1) in your main code and array(1) in a function are the SAME VARIABLE! This can be used for extra tricks, though it does create problems if you use #include.

4. The variables in the function maintain their value even after the function is finished. This means that if you have a flag variable, make sure to reset it at the beginning or end of the function, otherwise when you call it again, it will still have the same value as it did at the end of the functions last calling.

Ever notice how in Microsoft word, the word "microsoft" is auto corrected to be "Microsoft" but "macintosh" just gets the dumb red underline?
Sixty Squares
18
Years of Service
User Offline
Joined: 7th Jun 2006
Location: Somewhere in the world
Posted: 19th Oct 2008 16:54 Edited at: 19th Oct 2008 16:58
Quote: "4. The variables in the function maintain their value even after the function is finished. This means that if you have a flag variable, make sure to reset it at the beginning or end of the function, otherwise when you call it again, it will still have the same value as it did at the end of the functions last calling."


I beg to differ:



If what you said is true, then shouldn't the variable "g" keep increasing?

EDIT: Oh wait I'm using DBP it may be different in DBC.

EDIT 2: Odd, I just tested the code and the variable does keep increasing in DBC. They changed the way functions worked when they made DBP. I did not know that. Ignore this post

BN2 Productions
21
Years of Service
User Offline
Joined: 22nd Jan 2004
Location:
Posted: 19th Oct 2008 18:53
I think it is a bug, similar to the array bug. It seems too inconvenient to be expected.

Ever notice how in Microsoft word, the word "microsoft" is auto corrected to be "Microsoft" but "macintosh" just gets the dumb red underline?
Latch
18
Years of Service
User Offline
Joined: 23rd Jul 2006
Location:
Posted: 19th Oct 2008 19:16
The array thing isn't a bug. It's stated specifically in the help that you declare it global outside of a function and you declare it local inside of a function. A local array cannot be accessed outside of a function.

The variables inside of a function being static may or may not be a bug. In other programming languages, you can set a variable inside of a function to be static. The advantage of this may be to have the variable inside the function behave much like a global variable but be influenced and accessible only by the function. This is a lame example but:



You could create a function that performs an oscillation or something.

Enjoy your day.
Ed222
17
Years of Service
User Offline
Joined: 3rd Nov 2007
Location: Calgary
Posted: 19th Oct 2008 20:14
Quote: "Oh wait I'm using DBP it may be different in DBC"

Yes it is different. You can use the same variable in functions and elsewhere else in the game with out declaring it as global and still have the same value.

Yodaman Jer
User Banned
Posted: 19th Oct 2008 20:15 Edited at: 19th Oct 2008 20:35
Thanks everyone! I'm going to try out functions, now that I'm not scared of them...

Errmmm....I tried running it, and it displayed an error message.
It said 'Runtime error at line 16: Program ran into a function declaration.'

Here's my code:


Anything look wrong?

Yodaman Jer

That guy with no idea of what to say here. Except that he is a geek, and likes cheese. And pickles. But mostly cheese.
TheComet
17
Years of Service
User Offline
Joined: 18th Oct 2007
Location: I`m under ur bridge eating ur goatz.
Posted: 19th Oct 2008 20:31
Any variables inside a function don`t interact with the code outside of the function, and variables outside a function don`t interact with the code inside a function.

Here, g will be 1 outside the function, but inside it will be 0.



Now, if I put "g=1" in the function, g outside the function will be 0, but inside the function it will be 1:



Now, if I pass information into the function, both will be 1:



And if you wan`t info to leave the function outside the function, add the variable at the end of "endfunction":



Then it will look like this:



Hope that helps!

TheComet

Suicide is away of telling God, You can’t fire me I quit !!!!!
Latch
18
Years of Service
User Offline
Joined: 23rd Jul 2006
Location:
Posted: 19th Oct 2008 20:58
Quote: "Errmmm....I tried running it, and it displayed an error message.
It said 'Runtime error at line 16: Program ran into a function declaration.'"


As a general rule with DBC, create all of your functions at the end of the code. And before the first declaration, use END



Enjoy your day.
Sixty Squares
18
Years of Service
User Offline
Joined: 7th Jun 2006
Location: Somewhere in the world
Posted: 19th Oct 2008 20:59 Edited at: 19th Oct 2008 20:59
Quote: "Errmmm....I tried running it, and it displayed an error message.
It said 'Runtime error at line 16: Program ran into a function declaration.'"


There is nothing wrong with your code. However, it runs the "Make_Box" function, then keeps going. The compiler can't actually run the Function command, which declares the function. It can only run function calls. So, just declare all of your functions at the end (like you have) and write END before them. This way the program ends before it can get to the functions and give you that error.

EDIT: It seems Latch just beat me to it

Yodaman Jer
User Banned
Posted: 19th Oct 2008 21:03
Yes yes, that seems to have solved the problem.

Thanks latch and Sixty Squares!

Yodaman Jer

That guy with no idea of what to say here. Except that he is a geek, and likes cheese. And pickles. But mostly cheese.
Libervurto
18
Years of Service
User Offline
Joined: 30th Jun 2006
Location: On Toast
Posted: 20th Oct 2008 01:36
@Yodaman
You're not alone, it took me aaages to finally understand how to use functions properly.
I had a bit of a break-through recently when I realised I could make a function that used a "lower-level" function, this helped make everything a lot simpler.
Here's an example of this tiered function idea.

Writing the functions in tiers gives me more flexibility. If I decided I wanted to write some text at an isometric position I could use the lower-tier IsoX/Y/Z functions; if I'd written the IsoLine or IsoCube functions containing the formulas for the IsoX/Y/Z conversion I wouldn't have access to the formulas independently.

Here's a program I used them in.


@All
Does this method slow down the computer?

A small program that works is better than a large one that doesn't.

DBC Challenge Rank: Rookie
Ed222
17
Years of Service
User Offline
Joined: 3rd Nov 2007
Location: Calgary
Posted: 20th Oct 2008 02:14
Quote: "Here, g will be 1 outside the function, but inside it will be 0"

woops guess I was wrong but it does work if you declear is as global



TheComet
17
Years of Service
User Offline
Joined: 18th Oct 2007
Location: I`m under ur bridge eating ur goatz.
Posted: 20th Oct 2008 20:55
DBC doesn`t have the command "Global"... But yes, that`s true.

Suicide is away of telling God, You can’t fire me I quit !!!!!
Yodaman Jer
User Banned
Posted: 20th Oct 2008 21:17
Thanks everyone! Great help here! I now understand the basic use of a function.

Yodaman Jer

Guitarist, 3D modeler, Video editor, Renaissance Faire performer, Christian, and all-around GEEK. With a capital 'G'.
Latch
18
Years of Service
User Offline
Joined: 23rd Jul 2006
Location:
Posted: 22nd Oct 2008 18:10
Also make sure you check out TDK's tutorials:

Functions

Enjoy your day.

Login to post a reply

Server time is: 2025-06-07 08:16:59
Your offset time is: 2025-06-07 08:16:59