andrewducker (
andrewducker) wrote2010-02-11 03:11 pm
![[personal profile]](https://www.dreamwidth.org/img/silk/identity/user.png)
Thinking about code
If I have a method:
int DoSomething(string someStuff)
{
//Do Stuff
return 42;
}
then effectively I have an unnamed variable that gets set by the return statement, yes?
That being the case, wouldn't it be in some ways clearer to have an explicit, named, variable that gets set instead?
int DoSomething(string someStuff)
{
//Do Stuff
returnValue = 42;
}
where "returnValue" is a keyword that's used to return the value.
As it is I frequently end up with code that creates (or sets) a variable at various points through the code just so it can be returned at the end. Making this an explicit part of the language just makes sense to me.
I assume there are languages out there that do this.
int DoSomething(string someStuff)
{
//Do Stuff
return 42;
}
then effectively I have an unnamed variable that gets set by the return statement, yes?
That being the case, wouldn't it be in some ways clearer to have an explicit, named, variable that gets set instead?
int DoSomething(string someStuff)
{
//Do Stuff
returnValue = 42;
}
where "returnValue" is a keyword that's used to return the value.
As it is I frequently end up with code that creates (or sets) a variable at various points through the code just so it can be returned at the end. Making this an explicit part of the language just makes sense to me.
I assume there are languages out there that do this.
no subject
Function DoSomething(someStuff as String) as Integer
'Do Stuff
Return 42
End Function
or:
Function DoSomething(someStuff as String) as Integer
'Do Stuff
DoSomething = 42
End Function
where it lets you use the function name as a variable of the same type as your explicit named variable that will be returned.
(no subject)
(no subject)
(no subject)
(no subject)
(no subject)
(no subject)
no subject
Ah. Yes. It's silly because if you set $return halfway down your function, you can't tell at the end that you are returning something. Potentially hard to work out what is going on.
I'm not sure what you mean by an unnamed variable being set though... maybe that's a C# thing?
(no subject)
(no subject)
(no subject)
(no subject)
(no subject)
(no subject)
(no subject)
(no subject)
(no subject)
(no subject)
(no subject)
(no subject)
(no subject)
(no subject)
no subject
Now, having syntactic sugar where saying functionname = might be useful in some cases, although the fact that it looks and behaves like a normal variable, but has special behaviour based on the fact that it's called the same thing as the function, is something that would make me uncomfortable: you have to remember to update that variable name if you change the function name, or cut and paste some code from one function to another.
Also, if your function doesn't have a name (in the case of anonymous functions and/or closures), you can't do that. So you have to fall back to the far more obvious and readable return statement.
I think if you've got a number of different return values, that you'll decide according to various criteria, scattered through the code of the function, and you then finally return the value after having done all your processing, then it makes sense to declare a return variable before all of your ifs and switches etc. and then return it.
But I don't see why you'd want the overhead of doing that in all cases.
(I'm coming at this from a Perl, and occasionally Javascript, background, FWIW.)
(no subject)
(no subject)
(no subject)
(no subject)
(no subject)
(no subject)
(no subject)
(no subject)
no subject
For example (in Scheme), if we have a function:
Each time fib is called, the value returned is that of ult (set! returns an unspecified value).
If we wrote this in Common Lisp instead (where setq returns the value used to set the variable) we could write this instead:
Here, the value returned by fib is the value of the final setq, which is the value of tmp.
(no subject)
no subject
no subject
Languages with a return variable:
function monkeys() {
if (foobar) {
return = 42;
} else {
// ...
if (dagnabit) {
return = 13;
} else {
// ...
if (consarnit()) {
return = e^(-i*pi)
} else {
// ...
}
}
}
}
// fall out of function
}
Language with a return statement:
function monkeys() {
if (foobar) return 42;
// ...
if (dagnabit) return 13;
// ...
if (consarnit()) return = e^(-i*pi);
// ...
}
I find the latter preferable.
(no subject)
no subject
(no subject)
no subject
In matlab you declare the name of your result variable in the function definition:
function outvar=func_name(in1, in2)
outvar = in1 + in2;
multiple results are returned like this:
function [out1 out2]=func2(in1, in2)
out1 = in+1;
out2 = in+2;
In Eiffel, you set the result of a function by assigning to a compiler-created variable called Result.
no subject
nope. not today. Brain is goo, again.
(no subject)