Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Purpose of using sub routines over functions

Tags:

vba

ms-access

I've been working with Access for a while now, and although I understand the obvious benefit of a Function over a Sub, been that it can return values as a result, I'm not sure as to why I should use a Sub over a Function. After all unless I'm mistaken; Functions can do everything Subs can do?

Note: I'm fully aware of how to use both Sub's and Function's so not looking for an explanation of how they work.

like image 757
Matt Donnan Avatar asked Jul 18 '12 11:07

Matt Donnan


People also ask

Why should a subroutine be use instead of a function?

A function is used when a value is returned to the calling routine, while a subroutine is used when a desired task is needed, but no value is returned.

What is a subroutine and what is the purpose of it?

In computer programming, a subroutine is a sequence of program instructions that performs a specific task, packaged as a unit. This unit can then be used in programs wherever that particular task should be performed.

What are the main reasons for using a subprogram?

Subprograms are small programs that are written within a larger, main program. The purpose of a subprogram is to perform a specific task. This task may need to be done more than once at various points in the main program.


3 Answers

In terms of performance, this would not be any significant issue here.

The main difference is that user defined function can be used in expression in your code, where as a sub cannot.

This is really a HUGE Mount Everest of a difference here.

This difference is not really limited to Access, but tends to applies to every programing language and system I can think of that supports the creating of user defined functions.

The key advantage of using defined function are MANY but the most basic issue is that such function can be used in EXPRESSIONS.

For example, in an on click setting for a button on a form, you can generally have a single VBA [Event Code] routine attached to that button.

However you can ALSO place an expression in the property sheet like this:

=MyUserFunction()

The above is a handy tip, since then you can highlight 10 controls on a form, and type in the above expression and you just assigned the above function to those 10 buttons. You cannot do the above with a sub.

Another significant difference is you can use a function as a data source (expression) for a text box on a form or report (again you cannot do this with a sub).

Another significant difference is you can utilize these functions in SQL. This is a truly fantastic ability as then you can have code "run" for each row of a query. And this means you can extend the ability and functionally of SQL.

And you can even use this idea to display a VBA variable in a sql query as you simply build a public function that returns the VBA variable and this can be used in a query – you cannot however use VBA variables in a query!

And this extending of SQL opens up endless ideas:

So I can build a public function called ToMorrow()

Public Function Tomorrow() as date

   Tomorrow() = date() + 1

End Function.

Now in the query builder, I can go:

Select FirstName, lastName, Tomorrow() as NextDay from tblCustomers

And you can even make custom conversions such as:

Select FirstName, LastName, Celsius([DailyGreenHouseTemp]) from tblGreenHouse.

The above Daily temperature reading could in in Fahrenheit and you simply have to define a public function called Celsius like this:

Public Function Celsius(Temperature As Variant) As Variant

   Celsius = (Temperature * 1.8) + 32

End Function

Now while the above function is simple, it could do complex record set processing a complex algorithm to determine the moisture above a flower pot based on temperature and humidity.

So once we define such a public function, then the key concept is such a function can be used not only in VBA code as an expression, but ALSO can be used amazing enough this ability includes SQL.

So even in code, you can go:

If MyCustomfucntion(SomeVar) = lngTestValue then

Again in the above, you cannot use a sub in VBA expressions.

And even more interesting is when using custom XML for ribbons in Access, then if you use a function() expression for the "on action" attribute then you can avoid the need for ribbon call backs. Even better is the ribbon will call those functions() in the current form, not a public code module like you MUST do with ribbon call backs.

I could probably type on for another 10+ pages as to the difference, but I think that would start to be redundant and I don't want to appear condensing in any way here.

So the basic difference between a sub and function in VBA or in fact in most programming languages is quite much the same.

And the benefits of using a function in Access or just about any programing language are also much the same. For example I can define a user defined function in t-sql (scalar) – and again you then are free to use that t-sql function in any of your t-sql code or even quires that you create and use for sql server.

So this is basic and simple difference between a sub and a function, and I dare say those who have written computer code will in just about any programing language will instantly realize the above significant and useful differences between a subroutine and a function.

like image 122
Albert D. Kallal Avatar answered Oct 22 '22 12:10

Albert D. Kallal


The main difference is not only the return value, it seems that subs are faster than functions (at least in .net) because the MSIL code of subs is much shorter when no value is returned. so overall subs are faster when no value is returned. oh i've just found a great source for it (talks about .net), maybe you would like to read further about it- Functions vs. Subroutines

like image 45
TheNewOne Avatar answered Oct 22 '22 12:10

TheNewOne


Yes, a Function is just a Sub that returns a value.

like image 1
Cargo23 Avatar answered Oct 22 '22 12:10

Cargo23