function or gosub

Blitz3D Forums/Blitz3D Beginners Area/function or gosub

grindalf(Posted 2005) [#1]
what is the benefit of function, i'm not entirely sure what it does. i normaly use gosub and return, but i've noticed that other people use function in a place i would have just put a gosub.


Mustang(Posted 2005) [#2]
Gosub is SO 80's... :) Function is what "true" programming languages use... also you can pass/get info with functions. Gosub is just dummy GoSub.


Description:

A function is a routine of commands you may choose to call frequently within your program. Functions are considered 'proper' practice in this situation, instead of using GOSUB commands.



http://www.blitzbasic.co.nz/b3ddocs/command.php?name=Function&ref=2d_cat


Rob Farley(Posted 2005) [#3]
Gosubs:
a=5
b=6
gosub sum
print c
end

.sum
c = a + b
return

Functions:
a=5
b=6
c=sum(a,b)
print c
end


function sum(a,b)
return a+b
end function


Or a more elegant use would be
print sum(5,6)
end

function sum(a,b)
return a+b
end function


Other really handy uses are:
Without functions:
if x>10 then x=10
if x<0 then x=0
if y>10 then y=10
if y<0 then y=0
plot x,y


With functions:
plot clip(x),clip(y)

function clip(n)
if n>10 then n=10
if n<0 then n=0
return n
end function


Of course to demostrate this as effeciate would need a larger example:
; clip values at 10
if x1>10 then x1=10
if x1<0 then x1=0
if y1>10 then y1=10
if y1<0 then y1=0
plot x1,y1

; clip values at 50
if x2>50 then x2=50
if x2<0 then x2=0
if y2>50 then y2=50
if y2<0 then y2=0
plot x2,y2

; clip values at 25
if x3>25 then x3=25
if x3<0 then x3=0
if y3>25 then y3=25
if y3<0 then y3=0
plot x3,y3


With functions:
function clip(n,extent)
if n>extent then n=extent
if n<0 then n=0
return n
end function

plot clip(x1,10),clip(y1,10)
plot clip(x2,50),clip(y2,50)
plot clip(x3,25),clip(y3,25)



Ross C(Posted 2005) [#4]
I think it larger depends on your programming style. If your new to programming, i'd start using functions. They are alot more useful and keep you code modular. IE. you can take someone's function they write, and easily apply it to your code by passing across a mesh to it, and the function will do the rest. Have a look at the code archives. It's full of functions. :o)

Gosubs are a very tiny bit faster though. But the speed difference is very small. I know sswift uses them in in shadow system to try and squeeze more speed out of it. Or something like that :o)


jfk EO-11110(Posted 2005) [#5]
One important diffrence is: All Variables that are used inside a function are locals by default. If you want a function to be able to use a variable in a way that it won't "forget" its value after every function call, you need to define the variable as a global one:

global count

for i=0 to 100
 result= job(a,b)
 print count
 print count2
 print result
next
end

function job(la,lb)
 count = count +1
 count2 = count2 +1
 return la+lb
end function


You'll see, unlike count, count2 will remain 1 all the time since it was created locally each time the function was called, and of course initialized as Zero.

With Gosubs the Compiler treats everything as if it as inside the same function that includes the whole program, or let's say anything that is not inside a function is part of a virtual MAIN funtion.

Anyway, use Functions to prevent Spaghetti Style.


grindalf(Posted 2005) [#6]
thanks for the help guys, i think ive got it now, check out my code in the community project section to see if i have.
it took me ages to get to grips with the types though.
thanks again.