[AccessD] Function vs Sub (was: Classes and Events - EVENTS NOT REQUIRED)

Heenan, Lambert Lambert.Heenan at AIG.com
Tue Feb 24 12:24:34 CST 2009


I also favor using *real* function calls.

I especially don't like subs mysteriously modifying data. The example given was

RoundAmount curAmount, 2
Debug.Print curAmount   ' Returns 12.35

Here we have a Sub that is modifying its input parameter curAmount, but the only way to rally know this is by examining the source code of the Sub, which may not be available to you. One might say that the fact that the sub is Named "RoundAmount" tells you what it's going to do, but even that's not really the case. The RoundAmount sub is doing more than just rounding. I'll stick my neck out and say in most cases where rounding is done it's because you want to *display* data at a certain precision, but the underlying data should remain unchanged.

So  curRounded = Round(curAmount,2) does just that, we get a rounded value to display, and curAmount does not alter.
Whereas RoundAmount curAmount, 2 is throwing data away.

My 2 cents.

Lambert


-----Original Message-----
From: accessd-bounces at databaseadvisors.com [mailto:accessd-bounces at databaseadvisors.com] On Behalf Of jwcolby
Sent: Tuesday, February 24, 2009 1:04 PM
To: Access Developers discussion and problem solving
Subject: Re: [AccessD] Function vs Sub (was: Classes and Events - EVENTS NOT REQUIRED)

In that case I prefer the function returning a value, because the = tells the mind that a value is coming back.

John W. Colby
www.ColbyConsulting.com


Gustav Brock wrote:
> Hi Charlotte
>
> No, you are right, that is seldom.
> However, a sub may call(!) for more elegant code.
>
> Instead of:
>
>   curAmount = 12.345
>   curAmount = RoundAmount(curAmount, 2)
>   Debug.Print curAmount   ' Returns 12.35
>
> then this:
>
>   curAmount = 12.345
>   Call RoundAmount(curAmount, 2)
>   Debug.Print curAmount   ' Returns 12.35
>
> or even:
>
>   curAmount = 12.345
>   RoundAmount curAmount, 2
>   Debug.Print curAmount   ' Returns 12.35
>
> Still a matter of taste, I guess.
>
> /gustav
>
>>>> cfoust at infostatsystems.com 24-02-2009 18:24 >>>
> Absolutely when you need multiple return values, but a simple boolean
> or single value?
>
> Charlotte Foust
>
> -----Original Message-----
> From: accessd-bounces at databaseadvisors.com
> [mailto:accessd-bounces at databaseadvisors.com] On Behalf Of Gustav
> Brock
> Sent: Tuesday, February 24, 2009 1:45 AM
> To: accessd at databaseadvisors.com
> Subject: Re: [AccessD] Function vs Sub (was: Classes and Events -
> EVENTS NOTREQUIRED)
>
> Hi Charlotte
>
> Not tidy? In some cases this feature is very useful.
> Here is a simple example of sub returning multiple values:
>
> <code>
> Public Sub CompositeRGB( _
>   ByVal lngRGB As Long, _
>   ByRef intRed As Integer, _
>   ByRef intGreen As Integer, _
>   ByRef intBlue As Integer)
>
> ' Calculate discrete RGB colours from composite colour Value.
> '
> ' 1999-08-20. Cactus Data ApS, CPH
>
>   If lngRGB < 0 Then
>     ' Nothing to do.
>     intRed = 0
>     intGreen = 0
>     intBlue = 0
>   Else
>     ' Dissolve composite RGB into discrete colours.
>     intRed = lngRGB And vbRed
>     intGreen = (lngRGB And vbGreen) / &H100
>     intBlue = (lngRGB And vbBlue) / &H10000
>   End If
>   Debug.Print intRed, intGreen, intBlue End Sub
>
> </code>
>
> The only reason I see to not write a sub when a return value is not
> needed, is if the (sub)function will be used in a macro as these can't
> call subfunctions.
>
> /gustav
>
>
>>>> cfoust at infostatsystems.com 24-02-2009 01:14 >>>
> I didn't say functions HAD to return a value, just that you had to
> have a function in order to return the value in this case.  Actually,
> using ByRef for arguments passed into a sub allows you to get values
> back too, but it isn't as tidy.
>
> Charlotte Foust
>
>
>
--
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