Photo Gallery Member List Search Calendars FAQ Ticket List Log Out


Subs and Functions

 
Logged in as: Guest
arrSession:exec spGetSession 2,2,40908
 Active Users: There are 0 members and 0 guests.
 Users viewing this topic: none
 

 

 
  
  Printable Version
All Forums >> [Scripting] >> WSH & Client Side VBScript >> Subs and Functions
  Do you like VisualBasicScript.com? Link to us and help spread the word about our forum. Thanks!
Page: [1] 2   next >   >>
Login
Message << Older Topic   Newer Topic >>
 Subs and Functions - 12/13/2006 8:28:43 AM   
  TNO


Posts: 1397
Score: 16
Joined: 12/18/2004
From: thenewobjective.com
Status: offline
As noobish as this sounds, what is the real difference between a Sub and a Function besides the fact that a Sub can't return a value? When is the best situation to use each?

_____________________________

To iterate is human, to recurse divine. -- L. Peter Deutsch
 
 
Post #: 1
 
 RE: Subs and Functions - 12/13/2006 8:36:38 AM   
  ebgreen


Posts: 5246
Score: 31
Joined: 7/12/2005
Status: offline
The return value is the only difference. There is no advantage of one over the other that I have ever been able to find. To be honest with you IMO it is silly for the language to have both.

_____________________________

"... when you are good and crazy, oooh, oooh, oooh, the sky is the limit!" - The Tick
Goog places to start:http://www.visualbasicscript.com/m_24727/tm.htm
http://www.visualbasicscript.com/m_47117/tm.htm

(in reply to TNO)
 
 
Post #: 2
 
 RE: Subs and Functions - 12/13/2006 9:35:12 AM   
  ehvbs

 

Posts: 2220
Score: 50
Joined: 6/22/2005
From: Germany
Status: offline
I disagree:

   (1) some NamedCodeBlocks (generic term for Subs and Functions) shouldn't return any
         value: con/destructors

   (2) I don't want to abandon functions (obviously)

   (3) I don't want to be forced to dream up some dummy return value, if I need
         a NamedCodeBlock used for a side effect

If the syntactical method to express this difference looks like

    return;        vs.        return X;

that's ok.

(in reply to ebgreen)
 
 
Post #: 3
 
 RE: Subs and Functions - 12/14/2006 1:07:57 AM   
  ebgreen


Posts: 5246
Score: 31
Joined: 7/12/2005
Status: offline
I just think it is silly to have two different construct to do essentially the same thing. I prefer the way most other laguages handle it either by declaring a return in the function definition or always returning something and just making that something be nothing if you didn't really need a return at all. I suspect that my dislike for the way VBScript handles this issue comes from my desire to make code as re-useable as possible. I find that the use of subs for some reason tends to promote the use of global variables which decreases code re-useability.

_____________________________

"... when you are good and crazy, oooh, oooh, oooh, the sky is the limit!" - The Tick
Goog places to start:http://www.visualbasicscript.com/m_24727/tm.htm
http://www.visualbasicscript.com/m_47117/tm.htm

(in reply to ehvbs)
 
 
Post #: 4
 
 RE: Subs and Functions - 12/14/2006 1:20:16 AM   
  gdewrance


Posts: 587
Score: 3
Joined: 3/16/2006
Status: offline
ginolard explained it to me like this

You assign a value to the Function itself (this was in answerto my question).  You can't do that with a Sub. 
There have been countless arguments on why people would even bother using Subs and, mostly, it comes down to code "cleanliness". 

If you don't need to return a value, use a Sub. 
If you do, use a Function (though that's not set in stone!).

One other thing, you might see people using the Call command when calling a Sub or Function.  It's not always necessary.
All CALL does is tell the function it's calling to NOT return a value.

(in reply to TNO)
 
 
Post #: 5
 
 RE: Subs and Functions - 12/14/2006 1:27:23 AM   
  ehvbs

 

Posts: 2220
Score: 50
Joined: 6/22/2005
From: Germany
Status: offline
The bigger part of the disagreement is my fault. I should have written:

If the syntactical method to express this difference looks like

    return;        vs.        return X;

that's  better than

    Sub ... End Sub   vs.   Function  ...  End Function

but I can live the VBScript way (no floorboards on this motor bike? - so what).

I insist that there is (and should be) a difference between NamedCodeBlocks that
return a value on those that do not.

Using global variables or not has little to do with  the Sub/Function distinction.
Just think of COM: The return value is used up by the HRESULT, all 'interesting'
info is passed using parameters and global variables are just impossible.

(in reply to ebgreen)
 
 
Post #: 6
 
 RE: Subs and Functions - 12/14/2006 1:38:01 AM   
  DiGiTAL.SkReAM


Posts: 1194
Score: 7
Joined: 9/6/2005
From: Florida, USA
Status: offline
I had read on some site that Subs were faster than Functions.  Granted the difference was negligble, but I have never been able to validate this claim.


_____________________________

"Would you like to touch my monkey?" - Dieter (Mike Meyers)

"It is better to die like a tiger, than to live like a pussy."
-Master Wong, from Balls of Fury

(in reply to ehvbs)
 
 
Post #: 7
 
 RE: Subs and Functions - 12/14/2006 1:53:44 AM   
  TNO


Posts: 1397
Score: 16
Joined: 12/18/2004
From: thenewobjective.com
Status: offline
quote:


One other thing, you might see people using the Call command when calling a Sub or Function.  It's not always necessary.
All CALL does is tell the function it's calling to NOT return a value.


I'm not so sure about that. Every time I try to use an event listener Function from an HTML element I get an error if I don't use Call: "Can't use parenthesis when calling a Sub"

<edit>Almost forgot to mention. I have to use Call if using an objects method alot of times too Ex: oPopup.show("blah")</edit>

< Message edited by TNO -- 12/14/2006 1:57:43 AM >


_____________________________

To iterate is human, to recurse divine. -- L. Peter Deutsch

(in reply to DiGiTAL.SkReAM)
 
 
Post #: 8
 
 RE: Subs and Functions - 12/14/2006 2:31:20 AM   
  ebgreen


Posts: 5246
Score: 31
Joined: 7/12/2005
Status: offline
quote:

Every time I try to use an event listener Function from an HTML element I get an error if I don't use Call: "Can't use parenthesis when calling a Sub"


Well, are you using () with a sub? If you are then lose the () and you shouldn't need Call. I have yet to find an instance where Call is required.

_____________________________

"... when you are good and crazy, oooh, oooh, oooh, the sky is the limit!" - The Tick
Goog places to start:http://www.visualbasicscript.com/m_24727/tm.htm
http://www.visualbasicscript.com/m_47117/tm.htm

(in reply to TNO)
 
 
Post #: 9
 
 RE: Subs and Functions - 12/14/2006 4:15:10 AM   
  TNO


Posts: 1397
Score: 16
Joined: 12/18/2004
From: thenewobjective.com
Status: offline
I can't honestly remember the last time I used a sub...

Maybe I program in such a convulated manner that the engine has no idea what the hell I'm going:


      

_____________________________

To iterate is human, to recurse divine. -- L. Peter Deutsch

(in reply to ebgreen)
 
 
Post #: 10
 
 RE: Subs and Functions - 12/14/2006 4:16:30 AM   
  ebgreen


Posts: 5246
Score: 31
Joined: 7/12/2005
Status: offline
Ewwww....global variables.

_____________________________

"... when you are good and crazy, oooh, oooh, oooh, the sky is the limit!" - The Tick
Goog places to start:http://www.visualbasicscript.com/m_24727/tm.htm
http://www.visualbasicscript.com/m_47117/tm.htm

(in reply to TNO)
 
 
Post #: 11
 
 RE: Subs and Functions - 12/14/2006 4:34:09 AM   
  TNO


Posts: 1397
Score: 16
Joined: 12/18/2004
From: thenewobjective.com
Status: offline
Yum....global variables....

I dont see the logic behind recreating an object each time I want to display it.

_____________________________

To iterate is human, to recurse divine. -- L. Peter Deutsch

(in reply to ebgreen)
 
 
Post #: 12
 
 RE: Subs and Functions - 12/14/2006 4:40:53 AM   
  ebgreen


Posts: 5246
Score: 31
Joined: 7/12/2005
Status: offline
I wouldn't recreate it either. I would pass it into the func. That way I can drop the func into any other script that I want and the definition clearly shows me what I need to have to make the function work.

_____________________________

"... when you are good and crazy, oooh, oooh, oooh, the sky is the limit!" - The Tick
Goog places to start:http://www.visualbasicscript.com/m_24727/tm.htm
http://www.visualbasicscript.com/m_47117/tm.htm

(in reply to TNO)
 
 
Post #: 13
 
 RE: Subs and Functions - 12/14/2006 5:46:22 AM   
  TNO


Posts: 1397
Score: 16
Joined: 12/18/2004
From: thenewobjective.com
Status: offline
But why on earth would I have to use call for a function? Is it because its a method of an object? This isn't making any sense....

_____________________________

To iterate is human, to recurse divine. -- L. Peter Deutsch

(in reply to ebgreen)
 
 
Post #: 14
 
 RE: Subs and Functions - 12/14/2006 5:54:17 AM   
  ebgreen


Posts: 5246
Score: 31
Joined: 7/12/2005
Status: offline
You don't need to use Call for a function.

_____________________________

"... when you are good and crazy, oooh, oooh, oooh, the sky is the limit!" - The Tick
Goog places to start:http://www.visualbasicscript.com/m_24727/tm.htm
http://www.visualbasicscript.com/m_47117/tm.htm

(in reply to TNO)
 
 
Post #: 15
 
 RE: Subs and Functions - 12/14/2006 7:42:21 AM   
  TNO


Posts: 1397
Score: 16
Joined: 12/18/2004
From: thenewobjective.com
Status: offline
Hence my dilemma. This is not the case in all instances as I've shown in my example. If I remove call from window.createPopup.show() It will throw an exception regardless of the fact that it is a function.


Belated Note: window.createPopup() MUST remain outside of functions or it will not work without irrational behavior (failure to display, partial display, incorect display, etc.)

_____________________________

To iterate is human, to recurse divine. -- L. Peter Deutsch

(in reply to ebgreen)
 
 
Post #: 16
 
 RE: Subs and Functions - 12/14/2006 8:01:55 AM   
  TNO


Posts: 1397
Score: 16
Joined: 12/18/2004
From: thenewobjective.com
Status: offline
Heres another example of what I mean by this strange behavior:

This works:


      

This does not:


      

_____________________________

To iterate is human, to recurse divine. -- L. Peter Deutsch

(in reply to TNO)
 
 
Post #: 17
 
 RE: Subs and Functions - 12/14/2006 8:08:56 AM   
  ehvbs

 

Posts: 2220
Score: 50
Joined: 6/22/2005
From: Germany
Status: offline
Sometimes you want to have something done and are not interested in getting
information: Let's say you want to startle the user. You use a Sub (or - if the
compiler and the runtime system are a bit more flexible than Pascal - you use
a versatile NamedCodeBlock AS a Sub):

  MsgBox "Pay attention!", vbAbortRetryIgnore

Other VBScript Programmers (including you) and the compiler know you won't use
the virtual/possible information about clicked buttons, because

  you didn't assign to a variable
  you didn't put parameter list () around the arguments

Sometimes you need information. You call a function (or a NamedCodeBlock
that can be called AS a function) to do that:

  iRet = MsgBox( "Pay attention!", vbAbortRetryIgnore )
  .... many lines of code ....
  If vbOk = iRet Then

(sorry about the iRet, but I can't come up with a suitable name just now).

Doing the first thing in a language that only has/allows functions, you
must write

  iDummy = MsgBox( "Pay attention!", vbAbortRetryIgnore )

do to the first thing - and worry whether iDummy will be used at other
places in your program; another programmer (or you in about 5 weeks) will
have to scan your code to make sure - all your code if you don't heed
ebgreen's advice to shun global variables. That's why I objected to his
proposal to have functions only.

The Call statement is provided as an additional way to call a function
AS a Sub:

  Call MsgBox( "Pay attention!", vbAbortRetryIgnore )

You are not allowed to drop the (), because Call needs a function to call
to discard its return value. Now one of the high priority aims of VBScript
is to let even starting/inexperienced/sloppy programmers succeed (definitely
no irony intented, there is not so much difference to the highly acclaimed
mission statements of Perl: "Making Easy Things Easy and Hard Things Possible"
and "Do The Right Thing"). That's the reason you can use Call to call something
that is definitely a Sub - as long as you help the compiler/parser a little
bit: you'll have to use those ()

  Call MySub( 1, 2, 3 )

which will be "VERBOTEN" if you drop the unnecessary "Call":

  MySub( 1, 2, 3 ) ' don't expect this to work

But - you'll code:

  MsgBox( "But I *can* use ()!" )

Yes, it works; but those () aren't parameter list (), but "please pass per
value" parantheses, that - by nature and  definition - can be put around
just one element.

Add to this that the VBScript Docs WRONGLY put parameter list () all over the
place - probably the docs and/or the documenters were infected with a heavy
dose of Javascript - it's no wonder, Subs, Functions, and () aren't easy to
get right in VBScript.

(in reply to TNO)
 
 
Post #: 18
 
 RE: Subs and Functions - 12/14/2006 8:10:51 AM   
  TNO


Posts: 1397
Score: 16
Joined: 12/18/2004
From: thenewobjective.com
Status: offline
Alright, I did a little more research and this is what  came up with:

Subroutine Code Structure
If a subroutine does not receive any arguments, then you do not use any parentheses when declaring the subroutine. Note the required use of the Sub and End Sub statements which define the start and finish of the subroutine code.

Sub MySubroutine
   'place your code here
End Sub


However, if a subroutine does receive arguments, then you must use parentheses in its declaration. If there is more than one argument, you must separate the arguments with commas.


Sub MySubroutine(intUsageFee, intTimeInHours, strCompanyName)
   'place your code here
End Sub


Function Code Structure
As with subroutines, if a function does not receive any arguments, then you do not use any parentheses in the function declaration. Note the required use of the Function and End Function statements which define the start and finish of the function code.

Function MyFunction
   'place your code here
End Function


However, if a function does receive arguments, then you must enclose its argument list in parentheses when declaring the function. If there is more than one argument, you must separate the arguments with commas.

Function MyFunction(intUsageFee, intTimeInHours, strCompanyName)
   'place your code here
End Function


Calling the Subroutine
There are two possible ways to call a subroutine. You may either call the subroutine directly by name only, or you may call it by using the VBScript Call statement.

Calling a Subroutine by Name
If a subroutine does not pass any arguments, then you do not use any parentheses.

MySubroutine

Even if a subroutine has arguments, you still do not use parentheses. If there is more than one argument, you must separate the arguments with commas. If you enclose the argument list in parentheses when attempting to call a subroutine that takes more than one argument, you will receive the runtime error "Cannot use parentheses when calling a Sub".

MySubroutine intUsageFee, intTimeInHours, "Blah"

Using the VBScript Call Statement to Call a Subroutine
The use of Call statement is optional when you wish to call a subroutine. The purpose of the Call statement when used with a Sub is to allow you to enclose the argument list in parentheses. However, if a subroutine does not pass any arguments, then you still should not use parentheses when calling a Sub using the Call statement.

Call MySubroutine

If a subroutine has arguments, you must use parentheses when using the Call statement. If there is more than one argument, you must separate the arguments with commas.

Call MySubroutine(intUsageFee, intTimeInHours, "blah")

Calling the Function
There are two possible ways to call a function. You may either call the function directly, by name only, or you may call it by using the VBScript Call statement.

Calling a Function by Name
When calling a function directly by name and when there is no assignment to a returned value, all of the following are legal syntax:

MyFunction
MyFunction()
MyFunction intUsageFee, intTimeInHours, "blah"


If you want a returned value, you can assign the function to a variable. Note that if there is one or more arguments, you must use the parentheses.

returnval = MyFunction
returnval = MyFunction()
returnval = MyFunction(intUsageFee, intTimeInHours, "blah")


Using the Call Statement
If a function has arguments, you must use parentheses when using the Call statement. If there is more than one argument, you must separate the arguments with commas. There is no returned value when using Call.

Call MyFunction(intUsageFee, intTimeInHours, "Blah")

_____________________________

To iterate is human, to recurse divine. -- L. Peter Deutsch

(in reply to TNO)
 
 
Post #: 19
 
 RE: Subs and Functions - 12/14/2006 8:15:50 AM   
  TNO


Posts: 1397
Score: 16
Joined: 12/18/2004
From: thenewobjective.com
Status: offline
heh, we cross-posted


_____________________________

To iterate is human, to recurse divine. -- L. Peter Deutsch

(in reply to TNO)
 
 
Post #: 20
 
 
Page:   [1] 2   next >   >>
 
  

If you found our site useful please link to us <a href="http://www.visualbasicscript.com">VisualBasicScript.com</a>.
All Forums >> [Scripting] >> WSH & Client Side VBScript >> Subs and Functions Page: [1] 2   next >   >>
Jump to:





New Messages No New Messages
Hot Topic w/ New Messages Hot Topic w/o New Messages
Locked w/ New Messages Locked w/o New Messages
 Post New Thread
 Reply to Message
 Post New Poll
 Submit Vote
 Delete My Own Post
 Delete My Own Thread
 Rate Posts