a = Function(param1, param2) (!STOP IT!)

BlueIshDan

☠
Local time
Today, 00:54
Joined
May 15, 2014
Messages
1,122
For the education of those out there that are assigning values to blank variables as a solution to stop the error when using a function.

The real solution to this is to simply take away the brackets!

NO:
Code:
a = Function(param1, param2)

YES:
Code:
Function param1, param2

You will have to use the brackets in the case of:
Using the function in a condition statement
Whenever you actually want to compare the returned value to something or capture the value into a variable.
 
But if I wanted to run a subroutine without returning a value, I would write a Sub, not a Function.

Code:
Function ReturnsAValue As String
    ReturnsAValue = "This is a value returned by a function"
End Function

Sub PerformsAnAction
    MsgBox ReturnsAValue
End Sub

Calling code might be . . .

Code:
Private Sub cmdShowMessage_Click()
   PerformsAnAction
End Sub
 
I'm a bit tired lol.
What are you pointing out here?
 
My understanding of your original post is that you are describing how to call a Function without returning a value. Is that right? It appears that you had a strategy that you would create a dummy variable and assign the return value and then just ignore it.

What you offer is that you can call a Function without parenthesis and then you don't need the dummy variable.

What I'm offering in that case, if it's code that you control, is don't write a Function, write a Sub, which doesn't return a value anyway, and the whole "return value" problem is moot.

Maybe I missed your point?
All the best,
 
I was telling people that they didn't have to do this. I have been seeing it everywhere.
 
M2CW, In a normal world, I would define as follows:

Procedure - A set of commands to be executed.
Function - Any procedure that would return a Value.
Sub - Any procedure that would not return a Value.

If you need some return value then you code it as a function, if not code it as a Sub. Sometimes, you might need to Call a function without the need to return a value. This is quiet common in Macros. For this reason, any macro to perform a VBA command has to be written as a function. Hence most of the inbuilt VBA procedures are Functions and not Sub.

Although functions return value do not have to be assigned to a variable, you can achieve it by using
Code:
Call MsgBox("Hello World", vbYesNo)
[COLOR=Green]'Or[/COLOR]
MsgBox "Hello World", vbYesNo
You have to realize this is forcing a Function to be treated/called as a Sub. Hence defying its whole purpose of returning a value.

I think this is what MarkK has tried to explain !

PS: Your link to My Threads in your signature does not work ! Just thought you might want to know. ;)
 

Users who are viewing this thread

Back
Top Bottom