My use case is this: a function called 'time' that will return how long it takes to run any function you give it.
So the time function needs to know all the parameters to pass into the function when it calls it.
I know how to pass a function into another function, but how can I pass all its parameters, without knowing in advance how many and what type they are, so they can be used when calling the function?
For example, if I pass in an array of all the parameters I need to send, is there some Dart way to call a function by expanding an array into a list of parameters? Or perhaps there's another way to capture and pass a function call, including all parameters, as one executable object?
I'm also interested in knowing if there's a more Dartful way to accomplish what I'm trying to do re: timing function calls.
I believe using a List of parameters with the apply method is the most common way and practical of doing this and I have seen something similar used to pass parameters for JS interop. As far as I know, there isn't a way to expand an array into a list of parameters like you can for javascript. You could of course create your own object to pass arguments, but I think it would add unnecessary complexity and end up being more difficult.
Example of passing parameters to function in dart:js here.
Related
I'm currently trying to create a vectorSUM function in google scripts, which would sum up all vectors. I've looked into using the arguments method, but would much rather prefer using a rest parameter like the built in SUM function uses. I keep getting the error "missing formal parameter" this is my code. How would I go about using Optional parameters, as well as rest parameters in a custom function?
function vectorAdd(vector1, [vector2, ...]) {}
The built in SUM function uses
(value1, [value2, ...])
How can I achieve this?
True or False: When a function is called, the calling program suspends until
the function completes.
True or False: When you call a function with a list as a parameter, you could
change the original calling program’s list from within the function
True or False: When you call a function with a dictionary as a parameter, you could change the original calling program’s dictionary from within the
function.
Im very fuzzy on these questions and what they mean, could someone help explain these?
1.True
the original program will stop and going to run the function. It will back to the place where it stop and start to execute the next line command after function done.
2.False
call by value. it wont change the original variable's value.
3.True
call by pointer or call by reference.
Sometime yes and sometimes no. It depends whether you are calling it synchronously or asynchronously, see this answer for the distinction: Asynchronous vs synchronous execution, what does it really mean?
In most cases yes, because you are just passing by reference, i.e you are passing the location in memory of the param you are passing. However, you can also pass by value, i.e passing a copy of the object. See this question for more detail: What's the difference between passing by reference vs. passing by value?
same answer as your previous question. Whether you are passing an integer, string, array, list, dictionary, it doesn't matter, it all depends on whether you are passing by reference or by value. Which one of these happens by default depends on which programming language you use. You can determine which one is happening pretty easily with some experimentation: Define a function with a dictionary variable, add a key/value pair call another function with the dict as a param, and modify it in the called function, then print it out in the calling function once the called function has returned. If it has been modified, you know you are passing by reference. If it has the original key/value you set in the caller and is not modified, you know you are passing by value.
So I've been Googling function arguments and I would like to understand arguments better.
I am new to as3, to summarize arguments with my current knowledge, I would say they are like temporary variables? I don't fully get why you add parameters which are names that can be any value? Then you like call these parameters later and their order magically replace these parameters, but why? I'm missing some understanding here to fully grasp their use. Why make parameters in a function and then add the values later? If I'm even saying that right.
function name( applepie, sugar, healthyfood)
name( 1,2,3)
What was the point?
Also I haven't found a syntax book that describes what every symbol does yet that I can just search like () and it describes it, I heard some just use Google, but the results I got weren't very fruitful. Hence why I'm here asking. Personally I don't want to continue on until I fully grasps the use of (). I also tried Adobe website search but that didn't work out well either, was a good amount of searches trust me....
A function is a piece of code that can be reused many times in different contexts. You pass arguments to a function to tell the function something about the context in which it is being called; as a trivial example, when you call the print() function you must specify what you want the function to print. In your example name(applepie, sugar, healthyfood) the function should use the value supplied in place of each argument somewhere in its body, because the function doesn't know what values it will be passed, in the body of the function definition you use the names you chose (which should be descriptive) to refer to the values which will be passed in later and which will presumably be different each time it is called.
The parentheses are used for delimiting different semantic elements, in this case they are telling the interpreter where the argument list starts and stops.
I work with a BASIC programming language and have found it useful to write functions that rely on their output as a parameter. Such as
inOut = someFunction(inOut)
I'd like to call this a recursive function. but it doesn't seem right because it is not calling itself. Can someone tell me what the name of this type of function is and if the parameter/return has a special name?
Thanks!!
This is an ordinary function as any other. The thing you show is called reassingment. You can rename inOut on the left with newinOut and it will not change anything... there is absolutely nothing special about the function, it's a naming pattern, that's all.
In many languages (including VB, but not sure about classic BASIC) there's something called passing parameter by reference. It's not exactly what you posted, but rather simple
someFunction(inOut)
parameter is passed into the function, changed there and the change persists outside the function
I'm asking myself which can be the better way to handle parameters. Given an objet like this:
objet
property1
property2
lets say a function needs to receive it, at least property1 and property2 , so what is better, this?
function(param/property1, param/property2)
or this?
function(entire_object)
I can guess that for example passing the entire object isn't good for the performance while using params says exactly what function needs... and at the other side, we can say passing an object can be more dynamic. I don't know, let's discuss!
I can guess that for example passing the entire object isn't good for the performance
This is probably wrong. You can pass the object as a reference/pointer.
Passing an object as a reference/pointer can be considered as "passing an interface".
Comparing between "passing an interface" and "passing individual parameters", the former is likely to be a more stable interface. Lets say, your function implementation needs to change and you need an additional value. You can always pick up more information from the interface without any change to the function signature. This would not be true if you were passing individual parameters.
A disadvantage of passing an interface - From caller perspective, if the individual parameters are more readily available, it can be cumbersome to create an object just to pass to a function.