Using functions and subroutines

What is the difference between a function and a subroutine? Actually, very little. There is the notional idea that a function usually returns a value whilst a subroutine never does. Everything else is the same – both subroutines and functions can be passed parameters and both return to the caller on completion.

If you intend to write a lot of script, you should plan to make good use of functions and subroutines and make an effort to keep each one short – this will aid in maintaining the code and should also make you a more efficient programmer. For example, if you have a For...Next or Do While loop that contains 30 lines of script – consider putting this script into a separate subroutine. Or if you find yourself re-writing some lines of script – make a general subroutine or function out it of instead.

The use of functions and subroutines are best explained by example.

See:

Subroutines - an example

Functions - an example