Subroutines and Functions

Subroutines and functions both are named blocks of code. You can declare your own function or subroutine like this :

*English*
Declare a subroutine named killBob.
If the subroutine is called, kill Bill bob.

*Code*
sub killBob
    BobAlive = false
end sub

Once a subroutine has been declared, you can call it by using it's name.

*English*
Call the subroutine named killBob

*Code*
killBob

Subroutines can take a bit of data to make a decision with.

*English*
Declare a subroutine called KillBob.
If the data given is "true" then kill Bob.

*Code*
sub killBob(killData)
   if killData = true then BobAlive = false
end sub

When you call a subroutine which requires data, you put the data in parenthesis ( )

*English*
Use the killBob subroutine to kill Bob.

*Code*
killBob(true)

Functions are just like subroutines, except they can give you a bit of data back.

Unless otherwise stated, the content of this page is licensed under Creative Commons Attribution 2.5 License.