Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Functions
#1
Alright, something else! I thank you guys for the advice, it really helps.

How do I use funcions? As for as I know they keep your code clean and neat. I see them like this when I press F2

Untitled
Function

So can variables in untitled show up in my funtion? can I modify variables from my funtion for my main code? how do I call a funtion? please tell me all you guys can about these. I understand the concept but don't know how to use it.
Reply
#2
Okay, first you use functions by going to Edit->New FUNCTION.

Type a name, like printHello.

Now, you'll see this:
Code:
FUNCTION printHello
END FUNCTION

Say something like... PRINT "hello" inside there. So, now...
Code:
FUNCTION printHello
PRINT "hello"
END FUNCTION

Now go back - hit F2 and find your "main module". Now type in:
printHello

And hit F5. Walla! You see hello on the screen.

Using variables can be done, look up SHARED in the manual. However, that's a bad idea. It's better to use parameters.... you can find out about those in the online help too.

-[Unknown]
Reply
#3
Quote:"]Okay, first you use functions by going to Edit->New FUNCTION.

Type a name, like printHello.

Now, you'll see this:
Code:
FUNCTION printHello
END FUNCTION

Say something like... PRINT "hello" inside there. So, now...
Code:
FUNCTION printHello
PRINT "hello"
END FUNCTION

Now go back - hit F2 and find your "main module". Now type in:
printHello

And hit F5. Walla! You see hello on the screen.

Using variables can be done, look up SHARED in the manual. However, that's a bad idea. It's better to use parameters.... you can find out about those in the online help too.

-[Unknown]

You should really stick to pretending to code the YeBB SE, as QB isn't really your thing...

You can NOT call a FUNCTION the same way you call a SUB...

The difference between SUBs and FUNCTIONs are:

1) SUBs can be called with CALL() or just by the SUBs name but without parenthases.
2) SUBs do not return a value as itself, while FUNCTIONs do...

This is valid and will print "1" on your screen:
Code:
PRINT this%

FUNCTION this%
    this% = 1
END FUNCTION

This is NOT valid and will result in an error:
Code:
PRINT this%

SUB this%
    this% = 1
END SUB

The following ARE valid:
Code:
CALL this("test")
this "test"

SUB this (something$)
    PRINT something$
END SUB

That will print the word "test" twice on your screen.
I personally prefer skipping the word CALL and the parenthases, but that's up to you.

In that example, I used parameters, which are variables passed through a sub or function for use by that sub or function. Whatever string you put in place of something$ will be printed.

Make sense?

This also works backwards, meaning you can retrieve a variable from a sub or function in the parameters:
Code:
TestSub text$
PRINT text$

SUB TestSub (something$)
    something$ = "test"
END SUB

That will print the word "test" as the SUB has set that variable through the parameters.

I wanted to be a chemistry teacher but those dreams are long gone...
earn.
Reply
#4
You basically only want to use functions when you want to return a value. Person above gave good examples. You should check the QB help.

Examples of when you might want to Function:

Calculating distances
Calculating....
Returning position of player on screen

etc

Hail.
·~¹'°¨°'¹i|¡~æthérFòx~¡|i¹'°¨°'¹~·-
avinash.vora - http://www.avinashv.net
Reply
#5
Quote:You should really stick to pretending to code the YeBB SE, as QB isn't really your thing...
[Unknown] has been the most active developer of YaBB SE since 1.5.1 - I know this for a fact.

The upcoming release of YaBB SE, version 1.6.x, used to be called "[Unknown]'s secret project". The entire dev team decided to adopt his project as the next version in favor over what they were working on. Does that give you any idea as to how talented he is? Show a little respect.

Quote:You can NOT call a FUNCTION the same way you call a SUB...
Hey, give him a break Seph. He hasn't coded in QB for a while. So what if he forgot that it was functions he was supposed to be explaining instead of subs? A lapse like that isn't necessarily due to a lack of knowledge.
Reply
#6
Why the hell is a dev. of YeBB coming HERE!?
earn.
Reply
#7
Cuz I was browsing the forum, noticed a database question, and metioned it to him.
Reply
#8
Tense moment...

Right mistake found, solved. Woo Hoo.

Lets go home now and be happy alrite?
·~¹'°¨°'¹i|¡~æthérFòx~¡|i¹'°¨°'¹~·-
avinash.vora - http://www.avinashv.net
Reply
#9
Oh brother, I made a typo - I meant to say a = printHello. Then I was gonna go into returning... but I forgot while I was typing and started explaining general function theory.

Sawry. Won't try to be helpful again, *coughjerkcough* I mean seph Tongue.

-[Unknown]
Reply
#10
Ok, I think I see what you mean. Subs are what I'm looking for. The main thing I am concerned about is sharing variables. Could you guys tell me more about this? Thanks!

Charles A. Crane
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)