Is there a way to determine which optional arguments were given in a VBA function call? - function

Say I have a VBA function with an optional argument. Is there a way to tell, from within that function, whether the calling code has supplied the optional argument or not?
Public Function SomeFunction(optional argument as Integer = 0) As Integer
End Function
i.e. Is there a way to tell the difference between the following calls?
x = SomeFunction()
x = SomeFunction(0)

As far as I am aware it is not possible. If no argument is passed then the argument is initiliazed to its default value (0 in this case).
One way around this is to change the variable type to Variant and use the IsMissing function to check whether an argument is passed or not. Example:
Public Function SomeFunction(Optional argument As Variant) As Integer
If IsMissing(argument) Then
argument = 0
Else
// Code if argument not = 0....
End If
End Function
The IsMissing function works only with the Variant data type as any other data type will always have a default initialization value.

No, there is not.
The same problem exists in other language, such as C#, as expressed clearly in "C# In Depth", Chapter 13.1.1 (I read this part 15mins ago, it's normal I remember this!)
What I suggest you is not to set the default value in the function declaration. In the function, if argument is null, you set it to 0 and you know it wasn't supplied in the function call.
Edit : Just like #Remnant said in his answer, to be able to do this, the parameter type needs to be a variant.

Related

Understanding the difference between object.function(argument) and object:function(argument) in Lua

obj ={x=30}
function obj:printpos()
print(self.x)
end
other = {x=15}
obj.printpos(other)
The obj.printpos(other) gives expected output viz. 15. However the call obj:printpos(other) doesn't give expected output.It still prints 30. Why the call obj:printpos(other) is not taking other as its argument? Basically what is the difference between object.function(argument) and object:function(argument)? Is object:function(argument) same as object:function() i.e. whether argument is ignored?
obj:printpos(other) is equivalent to obj.printpos(obj, other).
function obj:printpos() end is equivalent to function obj.printpos(self) end.
From Lua 5.4 Reference Manual - §3.4.11 – Function Definitions (formatting mine):
The colon syntax is used to emulate methods, adding an implicit extra parameter self to the function. Thus, the statement
function t.a.b.c:f (params) body end
is syntactic sugar for
t.a.b.c.f = function (self, params) body end
From this, we can see the colon syntax implicitly adds the self parameter to the function scope.
Inversely, calling a function defined with the colon syntax using the dot syntax will cause the first argument passed to it to be assigned to the self parameter.
Thus, with
local thing = {}
function thing:func()
print(self)
end
the calls
thing:func()
-- and
thing.func(thing)
have the same result of assigning thing to self, and
thing.func(other_thing)
will assign other_thing to self.
The problem with
thing:func(other_thing)
is that, as previously seen, thing is assigned to self. other_thing is assigned to no parameter, as no other parameters were defined.

Julia - Change method definition within a function

So here's what I want, I have a function: f(x,t)=... where x is the "true" variable and t is a parameter. However, I need to use this function as a parameter in the Calculus package's derivative. This function requires a function with only one parameter. For this what I thought off was to redefine a method f(x)=f(x,t) where I fix the t parameter before passing it to the derivative function. This has to be done within another function.
However, doing this literally returns this error :
syntax: cannot add method to function argument f
I believe due to variable scope issues you should simply select a different name for your new function. You can also use anonymous functions instead of named definitions.
function foo(t_val)
newf(x) = f(x, t_val)
derivative(newf, otherparams...)
end
With anonymous functions,
function foo(t_val)
derivative(x -> f(x, t_val), otherparams...)
end
You can also assign anonymous functions to variables and use the variables as a function.
function foo(t_val)
newf = x -> f(x, t_val)
derivative(newf, otherparams...)
end

lua not modifying function arguments

I've been learning lua and can't seem to make a simple implementation of this binary tree work...
function createTree(tree, max)
if max > 0 then
tree = {data = max, left = {}, right = {}}
createTree(tree.left, max - 1)
createTree(tree.right, max - 1)
end
end
function printTree(tree)
if tree then
print(tree.data)
printTree(tree.left)
printTree(tree.right)
end
end
tree = {}
createTree(tree, 3)
printTree(tree)
the program just returns nil after execution. I've searched around the web to understand how argument passing works in lua (if it is by reference or by value) and found out that some types are passed by reference (like tables and functions) while others by value. Still, I made the global variable "tree" a table before passing it to the "createTree" function, and I even initialized "left" and "right" to be empty tables inside of "createTree" for the same purpose. What am I doing wrong?
It is probably necessary to initialize not by a new table, but only to set its values.
function createTree(tree, max)
if max > 0 then
tree.data = max
tree.left = {}
tree.right = {}
createTree(tree.left, max - 1)
createTree(tree.right, max - 1)
end
end
in Lua, arguments are passed by value. Assigning to an argument does not change the original variable.
Try this:
function createTree(max)
if max == 0 then
return nil
else
return {data = max, left = createTree(max-1), right = createTree(max-1)}
end
end
It is safe to think that for the most of the cases lua passes arguments by value. But for any object other than a number (numbers aren't objects actually), the "value" is actually a pointer to the said object.
When you do something like a={1,2,3} or b="asda" the values on the right are allocated somewhere dynamically, and a and b only get addresses of those. Thus, when you pass a to the function fun(a), the pointer is copied to a new variable inside function, but the a itself is unaffected:
function fun(p)
--p stores address of the same object, but `p` is not `a`
p[1]=3--by using the address you can
p[4]=1--alter the contents of the object
p[2]=nil--this will be seen outside
q={}
p={}--here you assign address of another object to the pointer
p=q--(here too)
end
Functions are also represented by pointers to them, you can use debug library to tinker with function object (change upvalues for example), this may affect how function executes, but, once again, you can not change where external references are pointing.
Strings are immutable objects, you can pass them around, there is a library that does stuff to them, but all the functions in that library return new string. So once, again external variable b from b="asda" would not be affected if you tried to do something with "asda" string inside the function.

Lua: colon notation, 'self' and function definition vs. call

I'm getting terribly confused by the colon notation used when defining/calling Lua functions.
I thought I'd got my head round it until I saw this piece of code:
function string.PatternSafe( str )
return ( str:gsub( ".", pattern_escape_replacements ) );
end
function string.Trim( s, char )
if char then char = char:PatternSafe() else char = "%s" end
return string.match( s, "^" .. char .. "*(.-)" .. char .. "*$" ) or s
end
What's confusing me here is that string.PatternSafe() doesn't reference 'self' anywhere, yet the code seems to work.
I've also seen some scripts that use colon notation when defining the function, for example:
function foo:bar( param1 ) ... end
After several hours of googling I've still not managed to work out what precisely is happening in these two contexts. My current assumptions are as follows:
If a function is defined using colon notation, it gets an invisible 'self' parameter inserted as first parameter
If a function is called using colon notation, the object preceding ':' is inserted in to the arguments (so becomes the first parameter of the function)
If a function is called using dot notation, then even if it was defined using colon notation it will not get the object inserted as first argument/parameter
If my assumptions are correct, that raises an additional question: What is the best way to ensure that the function was called properly?
Your assumptions are all correct.
Assumption 1 from the manual:
The colon syntax is used for defining methods, that is, functions
that have an implicit extra parameter self. Thus, the statement
function t.a.b.c:f (params) body end
is syntactic sugar for
t.a.b.c.f = function (self, params) body end
Assumption 2 from the manual:
A call v:name(args) is syntactic sugar for v.name(v,args), except that v is evaluated only once.
Assumption 3 doesn't have a direct manual section since that's just normal function call syntax.
Here's the thing though. self is just the auto-magic name given in the syntax sugar used as part of the colon assignment. It isn't a necessary name. The first argument is the first argument whatever the name happens to be.
So in your example:
function string.PatternSafe( str )
return ( str:gsub( ".", pattern_escape_replacements ) );
end
the first argument is str so when the function is called as char:PatternSafe() is de-sugars (via assumption 2) to char.PatternSafe(char) which is just passing char to the function as the first argument (which, as I already said, is str).

How to return a range object from a user defined function in vba

I have this piece of code in excel:
Private Function RelCell(NmdRng as String) as Range
Set RelCell = Range(NmdRng).Cells(1,1)
End Function
it gives the runtime error "91': object variable or with block variable not set.
I really don't know what is the problem with my function.. someone does?
I don't know if this is the problem but your are only setting the range and aren't returning anything from the function.
Try declaring a range variable with a different name as the function and return that.
Actually, you should be able to return a range from a UDF as described in this MSDN Thread.
Here is the code given by the MVP:
Function GetMeRange(rStartCell As Range, lRows As Long, iColumns As Integer) As Range
Set GetMe = rStartCell.Resize(lRows, iColumns) ' note the use of Set here since we are setting an object variable
End Function
(and it works)
Tiago's comment points out a very right thing, as you want to access a named range, it should be defined first.
You can try to set a breakpoint in your UDF and see if the Range(NmdRng) is defined.
Your named range already has a cell reference attached to it, so you shouldn't need to have the .Cells(1,1) at the end of it.
Using the .Range(nmdRng) property alone will return the range object you are looking for.
Try:
Private Function RelCell(NmdRng as String) as Range
Set RelCell = Range("NmdRng")
End Function
Please rewrite your code and test it as follows :
Private Function RelCell(NmdRng as String) as Range
Dim TestRange As Range
Set TestRange=Range(NmdRng)
TestRange.Activate 'I think that error will occur here because, NmdRng is somehow invalid
Set RelCell = TestRange.Cells(1,1)
End Function