For Programmers: Free Programming Magazines  


Home > Archive > Visual Basic > February 2006 > What is an advantage of using Function instead of Sub, when....









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author What is an advantage of using Function instead of Sub, when....
Jack

2006-02-24, 6:55 pm

Hello,
I am browsing through someone's else code and I came across strange
thing.
There are many calls to the different functions, but the calling code does
not expect any return value carried by the function mechanism..
Instead, the Function routine stores result into variable, and that variable
is later on read by the function calling code.
What is an advantage of using Function in such situation?
Isn't just better to use Sub instead?
I believe Sub has less overhead than Function, is that true?
Jack


Bob Butler

2006-02-24, 6:55 pm

"Jack" <replyTo@newsgroup> wrote in message
news:%23ViDHTYOGHA.1028@TK2MSFTNGP11.phx.gbl
> Hello,
> I am browsing through someone's else code and I came across
> strange thing.
> There are many calls to the different functions, but the calling code
> does not expect any return value carried by the function mechanism..
> Instead, the Function routine stores result into variable, and that
> variable is later on read by the function calling code.
> What is an advantage of using Function in such situation?


None

> Isn't just better to use Sub instead?


It'd make the code clearer which makes it easier to maintain. At the very
least you don't have to waste time trying to figure out why it is a function
<g>

> I believe Sub has less overhead than Function, is that true?


Not really; there's extra space allocate on the stack for the return value
but if it's never being set and not being assigned to a variable by the
caller then it's not going to be anything worth considering unless you are
doing a heavily recursive routine and wasting the stack space.

Sounds like the original coder was ignorant or lazy or both

--
Reply to the group so all can participate
VB.Net: "Fool me once..."

Rick Rothstein [MVP - Visual Basic]

2006-02-24, 6:55 pm

> Sounds like the original coder was ignorant or lazy or both

Or maybe came from a C background. <g>

Rick


Bob Butler

2006-02-24, 6:55 pm

"Rick Rothstein [MVP - Visual Basic]"
<rickNOSPAMnews@NOSPAMcomcast.net> wrote in message
news:Ott5heYOGHA.2668@tk2msftngp13.phx.gbl
>
> Or maybe came from a C background. <g>


yep, ignorant of VB syntax and too lazy to learn it. Kind of like the
VB.Net designers <g>

--
Reply to the group so all can participate
VB.Net: "Fool me once..."

Ken Halter

2006-02-24, 6:55 pm

"Jack" <replyTo@newsgroup> wrote in message
news:%23ViDHTYOGHA.1028@TK2MSFTNGP11.phx.gbl...
> Hello,
> I am browsing through someone's else code and I came across
> strange thing.
> There are many calls to the different functions, but the calling code does
> not expect any return value carried by the function mechanism..
> Instead, the Function routine stores result into variable, and that
> variable is later on read by the function calling code.
> What is an advantage of using Function in such situation?
> Isn't just better to use Sub instead?
> I believe Sub has less overhead than Function, is that true?
> Jack


Others answered the "sub vs function" question. Just adding...... if the
previous developer was lazy enough to use Function everywhere instead of the
appropriate procedure types, watch out. There may be actual Functions in
there that, due to that same laziness, don't return a value when they
should..... which a far more common problem. CodeSMART (and many other code
analyzers) will let you know if there are any functions that don't return
values.

--
Ken Halter - MS-MVP-VB - Please keep all discussions in the groups..
DLL Hell problems? Try ComGuard - http://www.vbsight.com/ComGuard.htm
Freeware 4 color Gradient Frame? http://www.vbsight.com/GradFrameCTL.htm


J French

2006-02-25, 7:55 am

On Fri, 24 Feb 2006 15:56:24 -0500, "Jack" <replyTo@newsgroup> wrote:

>Hello,
> I am browsing through someone's else code and I came across strange
>thing.
>There are many calls to the different functions, but the calling code does
>not expect any return value carried by the function mechanism..


>Instead, the Function routine stores result into variable, and that variable
>is later on read by the function calling code.


Does the Function return anything ?

>What is an advantage of using Function in such situation?
>Isn't just better to use Sub instead?


>I believe Sub has less overhead than Function, is that true?

Marginally less overhead, particularly with Strings, but the
difference is trivial.

The original coder was either very dim, or he is doing something
fairly clever

It is hard to tell without looking at the code of the Functions that
are being used as Subs.

I've occasionally written Functions that hold additional information
in a local variable, that can be read by another Function
- typically detailed error information



Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com