functions vs. subs

1. Junior Member
Join Date
Dec 1969
Posts
5

## functions vs. subs

besides syntax,what&#039;s the difference between the two? like when should i use a function and when should i use a sub?

2. Senior Member
Join Date
Dec 1969
Posts
11,334

## RE: functions vs. subs

Functions return values, subs don&#039;t. Use a function if you want to return a value.

3. Senior Member
Join Date
Dec 1969
Posts
96,118

## functions return a value...

&#060;%<BR>Function Sum( v1, v2 )<BR>&nbsp; &nbsp; Sum = v1 + v2 <BR>End Function <BR><BR>Response.Write "The sum of 13 and 58 is " & Sum(13,58)<BR>%&#062;<BR><BR>Subs don&#039;t return any value, though they *can* alter their arguments if the argument(s) is/are passed "ByRef":<BR><BR>&#060;%<BR>Sub Swap( ByRef arg1, ByRef arg2 )<BR>&nbsp; &nbsp; Dim temp<BR>&nbsp; &nbsp; temp = arg1<BR>&nbsp; &nbsp; arg1 = arg2<BR>&nbsp; &nbsp; arg2 = temp<BR>End Sub<BR><BR>x = "xxxxxxxx"<BR>y = "why?"<BR>Swap x, y &#039; you could also write: Call Swap(x,y)<BR>Response.Write "x is " & x & " and y is " & y<BR>%&#062;<BR><BR>Most Subs do *not* alter their arguments though, so typically a sub is used to perform an external action while a Function is used to retrieve some information.<BR><BR>Note: And just to be complete, a Function CAN also alter its arguments, if they are passed ByRef.<BR> <BR>

4. Junior Member
Join Date
Dec 1969
Posts
5

## RE: functions vs. subs

so does a function use more memory even if it doesn&#039;t return a value?

5. Senior Member
Join Date
Dec 1969
Posts
96,118

## Non sequitir...

A function *ALWAYS* returns a value.<BR><BR>It returns the VBS value "Empty" if you don&#039;t assign a value yourself. <BR><BR>Does a function take more memory than a SUB? <BR><BR>What kind of memory? On the stack? Yeah, 16 whole bytes. Whoopee.<BR><BR>In the heap, where the program is stored? Not because it is a function; program memory is strictly a function of the complexity of the code. Period.<BR><BR>

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•