Site hosted by Angelfire.com: Build your free website today!

Function is a high langauge command brought down to Basic and simplified.  In Euphoria for example you have 2 commands to make your own commands,
FUNCTION and PROCEDURE these are in Pascal and c as well.
PROCEDURE makes a subroutine you can pass arguments to.
FUNCTION makes a subroutine you can pass arguments to and get a return from.

In Dark Basic they combine these to commands in to 1, FUNCTION

Lets look at the statement

FUNCTION(arguments)   - - declares the function you can have arguments or not
END FUNCTION return - - declares the end , you can have a return or not

Why Should you use function?  Function are proper programming style, they give greater flexiabity and you can combine function into libraries to include in you code.

Can't I just use GOSUB or GOTO?
Yes you can but, gosub and goto are hard to fallow and considered to be sloppy programming.  You code when seen by other coder will be considered less advance and you might not receive the due praise you deserve. 

Now for some examples

example 1 a function without a return

rem make a function to draw a outlined box
function Line_Box(x1,y1,x2,y2)
line x1,y1,x2,y1
line x1,y2,x2,y2
line x1,y1,x1,y2
line x2,y1,x2,y2
end function

rem loop repeated boexs on the screen

do
rem call our new command
Line_Box(rnd(640),rnd(480),rnd(640),rnd(480))
rem update the screen
sync
rem loop back to do
loop



example 2 a function with a return

Rem a function to compare 2 numbers and return the higher one

function High_Compare(n1,n2)
if n1 > n2
returnval = n1
endif
if n2 > n1
returnval = n2
endif
end function returnval

do
a = rnd(100)
b = rnd(100)
print High_Compare(a,b)," "
sync
loop


Explanation of examples.

Example 1 take 4 arguments to define a rectangle region, transposes those to lines and makes a box.
Expand it - can you add another argument to set the box color?

example 2 take 2 arguments and returns the highest value.

Notice how example 1 calls the function just like a command and example example to call it as a function only wanting the return value.

BACK