A quick way to differentiate between Functions and Subs is to remember that Functions
*return something*, while Subs
*do something*.
Let me give you a few quick examples of SUBS and FUNCTIONS that you use all the time:
SUB | FUNCTION |
CLS | SIN |
PRINT | ABS |
PSET | INT |
END | SQR |
WIDTH | WIDTH |
Now, look at the table above and take a moment to think of those commands and what they do for us.
CLS -- this clears the screen for us. (does something)
PRINT --- this prints to the screen. (does something)
PSET -- this sets a point a particular color for us. (does something)
END -- this ends our program. (does something)
See how all those SUB routines *do* something?
Now, compare to the FUNCTIONS:
SIN(x) --- this returns the sine of x to use. (return something)
ABS(x) -- this returns the absolute value of x. (return something)
INT(x) -- this rounds down the value of x and returns it to us. (return something)
SQR(x) -- this returns the square root of x to us. (return something)
The SUBS *do* something; the FUNCTIONS *return* the value of something to us.
Another simple way to tell the difference between the two is SUBS are standalone, while FUNCTIONS arent'. Write a line of code and you'll quickly see what I mean.
What does the above line of code do? It clears the screen, of course!
Code: (Select All)
SIN(2*_PI)
Now, what does the above line of code do?
It's the exact same as asking, what this line of code does...
By itself, it does nothing. (Except toss an error for us, for doing nothing.)
That SUB can standalone and *do* something. A FUNCTION only works in places where it can *return* a value to something else.
x = SIN(_PI)
y = ABS(-2)
z = INT(3.14)
PRINT SQR(9)
And with that said, you may have noticed that the last entries in my table above are the same command.
WHAT??!!
HOW?!!
Think of what I've just told you, and see if you can see the difference in those two uses of WIDTH.
WIDTH, by itself, is a SUB which sets the width of the screen.
Code: (Select All)
WIDTH 80
WIDTH, as a return command, is a FUNCTION which tells us the width of the screen.
Code: (Select All)
x = WIDTH
How we use the command, tells us if were using the SUB or the FUNCTION version of it.
WIDTH x <-- this is the SUB, which sets the screen to x characters wide
x = WIDTH <--- this is the FUNCTION, which returns to us the width of the screen.
And that's the differences between SUB and FUNCTION, in a nutshell.