I want to build a function for some code that I use often in my MQL4 programming that outputs to a file data resulting from optimisation runs.
I already have the working code but I am having a difficulty with passing parameters.
Some of the function parameters, that I need to access inside the function and so these need to be passed as parameters, are global, user defined variables - eg as: input int Moving_Average_period = .... ;.
The next time I use the function, this variable may not be required but another(s) will
This variable reference needs to appear in a FileWrite() statement eg:
FileWrite( h, Counter, Moving_Average_period, StopLoss, .......... );
any ideas please - thanks in advance.
Declare the function with an Array(s) in the call-signature:
Simply put,
void aFunctionWithValuesInARRAYs( int const anArrayOfINTs[],
double const anArrayOfDOUBLEs[],
datetime const anArrayOfDATETIMEs[]
) {
// ------------------------ ^
// PROCESS DATA AS YOU NEED +-- pre-loading and updating values
// ------------------------ in each anArrayOf*s[] is a very
// Q.E.D. flexible external-responsibility
...
}
Related
In WebhookScript, I can store a function in a variable with:
sub = function(a, b) {
return a - b
}
I'd like to store a function in a Global Variable so that I can use it in multiple Custom Actions. But if I've saved the above function as $sub$ then
sub2 = var('$sub$')
subX = sub(1,2)
causes an error:
Trying to invoke a non-function 'string' # line...
And
function subX(a,b){
var('$sub$')
}
when sub only contains return a - b, doesn't work either.
Obviously I need to convert the string to a function but I'm not sure whether that's possible.
I know this is a bit of an obscure language but if anyone knows how this can be done in similar languages like JavaScript and PHP, I'm happy to test out any guesses...
The solution here is to remove the function section and just enter the script, which inherits the execution scope so if my global variable $script$ is:
return 'hello ' + a
Then I can execute the function with:
a = 'world'
value = exec(var('$script$'))
echo(value)
(credit to Webhook.Site's support team for explaining this)
Is it possible to define a VHDL function with no parameters?
I'm trying to compute the number of bits in a record type, but to do so it is necessary to create an instance of that type. Therefore, since I can't just define const BITS = t_rec.a'length + t_rec.b'length, I would like to define a function function BITS() return natural that instantiates a record rec : t_rec and returns rec.a'length + rec.b'length. However, the compiler fails with unexpected '(') at the function declaration, before it even reaches the definition.
I would just include an unused, dummy parameter, but I suspect that the lint tools would complain.
Defining subtypes for each record field in advance of the record would be too verbose.
Complete example:
package pack is function BITS() return natural; end package pack; package body pack is function BITS() return natural is begin return 0; end function; end package body pack;
Error message:
pack.vhd:1:30: empty interface list not allowed
You should leave out the parentheses when defining a function with no inputs, eg:
function BITS return natural is
variable rec : rec_t;
begin
return rec.a'length + rec.b'length;
end function;
You should not include the parentheses when you call the function, either. eg:
report integer'image( BITS );
not
report integer'image( BITS() );
https://www.edaplayground.com/x/5fMB
is there a way to get the function call stack with pycparser ?
for example if i have code like this:
static int Func_1(int *val)
{
unsigned int data_1 = 0;
int result;
result = Func_2(val,
data_1
);
result = Func_3(val,
result
);
return result;
}
so with FuncDefVisitor i can retrieve Func_1 and with FuncCallVisitor i could retrieve Func_2 and Func_3
but how can i retrieve the information that e.g. Func_2 is called from within Func_1 ?
You can write a visitor that finds FuncDef nodes and runs another visitor on them that finds all FuncCall nodes. For example:
class FuncDefVisitor(c_ast.NodeVisitor):
def visit_FuncDef(self, node):
# Here node is a FuncDef node for the function definition.
v = FuncCallVisitor()
v.visit(node)
class FuncCallVisitor(c_ast.NodeVisitor):
def visit_FuncCall(self, node):
# do something
In visit_FuncDef, we create a new call visitor and run it on node, which means it only runs within the AST node for the function definition. It will find Func_2 and Func_3 in your code, but not func calls in other functions.
That said, note that you will only get the static call graph from this. In C functions can call others via function pointers and that's not something you can know statically in the general case.
I was able to store functions into a table. But now I have no idea of how to invoke them. The final table will have about 100 calls, so if possible, I'd like to invoke them as if in a foreach loop. Thanks!
Here is how the table was defined:
game_level_hints = game_level_hints or {}
game_level_hints.levels = {}
game_level_hints.levels["level0"] = function()
return
{
[on_scene("scene0")] =
{
talk("hint0"),
talk("hint1"),
talk("hint2")
},
[on_scene("scene1")] =
{
talk("hint0"),
talk("hint1"),
talk("hint2")
}
}
end
Aaand the function definitions:
function on_scene(sceneId)
-- some code
return sceneId
end
function talk(areaId)
-- some code
return areaId
end
EDIT:
I modified the functions so they'll have a little more context. Basically, they return strings now. And what I was hoping to happen is that at then end of invoking the functions, I'll have a table (ideally the levels table) containing all these strings.
Short answer: to call a function (reference) stored in an array, you just add (parameters), as you'd normally do:
local function func(a,b,c) return a,b,c end
local a = {myfunc = func}
print(a.myfunc(3,4,5)) -- prints 3,4,5
In fact, you can simplify this to
local a = {myfunc = function(a,b,c) return a,b,c end}
print(a.myfunc(3,4,5)) -- prints 3,4,5
Long answer: You don't describe what your expected results are, but what you wrote is likely not to do what you expect it to do. Take this fragment:
game_level_hints.levels["level0"] = function()
return
{
[on_scene("scene0")] =
{
talk("hint0"),
}
}
end
[This paragraph no longer applies after the question has been updated] You reference on_scene and talk functions, but you don't "store" those functions in the table (since you explicitly referenced them in your question, I presume the question is about these functions). You actually call these functions and store the values they return (they both return nil), so when this fragment is executed, you get "table index is nil" error as you are trying to store nil using nil as the index.
If you want to call the function you stored in game_level_hints.levels["level0"], you just do game_level_hints.levels["level0"]()
Using what you guys answered and commented, I was able to come up with the following code as a solution:
asd = game_level_hints.levels["level0"]()
Now, asd contains the area strings I need. Although ideally, I intended to be able to access the data like:
asd[1][1]
accessing it like:
asd["scene0"][1]
to retrieve the area data would suffice. I'll just have to work around the keys.
Thanks, guys.
It's not really clear what you're trying to do. Inside your anonymous function, you're returning a table that uses on_scene's return value as keys. But your on_scene doesn't return anything. Same thing for talk.
I'm going to assume that you wanted on_scene and talk to get called when invoking each levels in your game_level_hints table.
If so, this is how you can do it:
local maxlevel = 99
for i = 0, maxlevel do
game_level_hints.levels["level" .. i] = function()
on_scene("scene" .. i)
talk("hint" .. i)
end
end
-- ...
for levelname, levelfunc in pairs(game_level_hints.levels) do
levelfunc()
end
I would like to override a function to inspect its parameter values but the call it and pass the original parameters as normal. Is this possible? I am using Corona SDK by www.coronalabs.com
My code at present, which does not work, is:
-- getting a refrence to the original function so i can replace it with my overriding function
local newcircle = display.newCircle
-- my override
display.newCircle = function(...)
-- attempt to pass the parameters to this function on to the real function
local t = {...}
newcircle(unpack(t))
end
-- calling the overridden function as if it were normal
display.newCircle( display.newGroup(), "image.png" )
In your new display.newCircle implementation, you are using the undefined t table, and the deprecated arg automatic table.
Try this :
-- my override
display.newCircle = function(...)
local t = {...} -- you need to collect arguments to a table
-- dumb use of override
print(t[1])
-- attempt to pass the parameters to this function on to the real function
newcircle(unpack(t))
end