Can anybody explain what a return variable is in Coral in simple language and how is it different from any other local variable within a function? From my understanding the return variable returns some value to the main function but I don't know if it is correct
Any examples would be appreciated. Thank you
Related
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.
Are there any rules (or will I run into any problems) if I name the parameters of a function the same as the variable I will pass into them?
For example in Python:
def foo(param):
pass
param = 2
foo(param)
In the fairly limited programming I've done, I have not ran into any problems doing this. Will I get problems in certain languages? Is this okay to do, or is it a practice to be avoided?
The "problem" in this particular case is that the function parameter name will shadow the outer param variable; i.e. you cannot (implicitly) refer to the global param anymore because inside your function param is defined as a local variable.
But, this is really as it should be. Your function should only worry about the parameters it declares locally, not about implicit global variables. Conversely, a caller of a function should not have to worry about anything that goes on inside a function. Naming a variable the same as a parameter to a function is of no consequence to the caller, and should be of no consequence to the function itself.
So, no, there's absolutely no issue here.
I'm a beginner and I remember reading somewhere, but unfortunately do not remember where, that it is good practice to first convert parameters of a function to local variables, before working with them. Could someone explain why?
void MyFunction(type param)
{
type myVar = param;
//do stuff with myVar instead of param
}
I'm guessing if param passed into MyFunction was a pointer then it might be possible the data that it's pointing to could change while MyFunction is being executed, but are there other reasons?
There is no reason to do the copy in any modern language that passes by value.
It would have been useful in older languages, like Algol, that pass parameters by name. See this example.
It's bad;; when type myVar = param;, type's copy constructor called. It's unnecessary unless you use both myVar and param for different purposes.
edit: If type's copy cost is almost free, It can be used for convenience and readability.
There are some answers to this question here. The answers why one would copy a value that has already been copied point mostly to maintainability considerations.
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
Basically I would like to call a function that I have written, and because of the amount of results, I would like the function to output its solution into a matrix that gets passed to the program that called it.
You define the output of a function in the function declaration at the top of your script:
function [output] = myFunction(input)
All you need to do is define the output variable somewhere in your script.
The confusing part (to me) was that you need to put the output variables in both your main program and your function definition. So in your main program you have:
[out1,out2,out3] = function_name(in1,in2);
and in your function definition, you have:
function [out1,out2,out3] = function_name(in1,in2).
The variables don't have to have the same name, but they need to be oriented similarly, so that you can then pass the outputs back to the main program.