[AccessD] Function vs Sub

John W. Colby jcolby at colbyconsulting.com
Thu Feb 13 11:09:01 CST 2003


John,

Functions can return a value, subs can't.  It is often useful when you move
to this method of programming to return a value that says that the function
accomplished it's purpose.  IOW, perhaps the function is supposed to build a
report.  The report is going to (in this example) be saved as a hardcopy on
the network somewhere.  There are all kinds of things that could prevent the
report from being generated, or being saved.  So the function returns a True
if the report process worked, or a false if it didn't.

I tend to do something like:

function BldReport() as Boolean
On Error goto BldReport_Error
	BldReport = false	'Specify a false value up front.  The process must work
to be set true
	Do my report process, try to store etc.
	.
	.
	BldReport = true
Exit_BlDReport
	Exit function
Err_BldReport
	'handle errors here
	resume Exit_BldReport
end function

As you might surmise, the function tries to build the report.  If anything
goes wrong the error causes control to vector to the error handler where
whatever you decide to do to handle errors is done.  When it is done,
control resumes at the exit.  The statement that sets the function name true
is never executed, so the function returns false.

If no error occurs, processing just continues down the function and
eventually sets the function return value true and exits.

Therefore the code that tries to generate the report can now do something
like:

	if BldReport() then
		'This is good, continue
	else
		'this is bad, notify the user, log the error etc.
	endif

So, to re-iterate, functions can return a value.  Even if they normally
wouldn't it is often useful to use that ability to return the fact that the
function succeeded in whatever it was supposed to do.

John W. Colby
Colby Consulting
www.ColbyConsulting.com

-----Original Message-----
From: accessd-admin at databaseadvisors.com
[mailto:accessd-admin at databaseadvisors.com]On Behalf Of John Clark
Sent: Thursday, February 13, 2003 11:20 AM
To: accessd at databaseadvisors.com
Subject: [AccessD] Function vs Sub


I recently smartened up and started using fuctions in my code, and I've
got what may be a very dumb question...should I be using "functions" or
should they be "Public Subs" or ?????? What is the difference. I
apologize if I am lowering the intelligence quota today, but I just
haven't come across this and I don't want to go down the wrong path.
_______________________________________________
AccessD mailing list
AccessD at databaseadvisors.com
http://databaseadvisors.com/mailman/listinfo/accessd
Website: http://www.databaseadvisors.com






More information about the AccessD mailing list