Tuesday, October 20, 2009

Functions in vb.

What is function? some of you may ask.
a function may be a formula/converter.
it requires input or object(in add math functions).
but in programing we called those parameters.(input)
then it comes out with an output. called image in add math function.
get the picture?

why do we need functions in programing?
here is an example:
this is an quadratic formula:
x = (-b±(b^2-(4*a*c))^(1/2))/(2*a)
So,ur input will be a, b and c.
Which has the output of x.
If you are creating a program which may use this formula a lot of times.
dun tell me you are gonna retype it?
even if you copy and paste, it will end up look like a mess.
u can predefine it as a function.
type the code below:

Private Function Quad(a as single, b as single, c as single) as single
Quad = (-b±(b^2-(4*a*c))^(1/2))/(2*a)
End function

So, whenever you want to use it in the same form:
you just have to type

x = Quad(a,b,c)

you just have to fill up the a,b,c.
or get their value from somewhere you wan.
its better if u use a modular and predefine the functions in it.
so, all your forms can use it=)
This is just a simple example.
bare that in mind.
i had made a very complicated function before.
if you are creative enough, you can do a function within a function.
thats not something stupid.
its just ethical.
it will make your program more structural and neat.
it also allows you to easily debug your program.
thats all today.
i had weird dreams lately.
i dream 3 times that i was doing spm add math on the next day.
Are those what they called as signs?
busy man talking.

No comments: