From the point of view of solidity code the use of named return values is choosed when:
1) it increases by far the readability. For instance any function defined as
function foo( ... ) returns (bool success)
is immediately understood. The alternative:
function foo( ... ) returns (bool) // true if successful
is by far less readable.
2) BY FAR THE MOST IMPORTANT when you plan to use local variables in order to optimize gas in calculations which implies state variable.
If you need to have this function:
function foo( ... ) returns (uint) {
return(globalPubliCVariable*globalPubliCVariable*globalPubliCVariable + 2*globalPubliCVariable + 1);
}
it is widely known that you can have big advantages in terms of gas cost using a local temporary variable:
function foo( ... ) returns (uint) {
uint res;
res = globalPublicVariable;
res = res*res*res+ 2*res + 1;
return(res);
}
but you can have further advantages in term of gas cost simply using named return values as temporary local variable:
function foo( ... ) returns (uint res) {
res = globalPublicVariable;
res = res*res*res+ 2*res + 1;
}
where the difference is that you did not create any local variable for intermediate results. (It can be demonstrated that, this way, you minimize the stack deep).
In all the other cases you can use the named return or the unnamed without differentiations.
———————————
EDIT #1 to address various comments
to better explain second point, you need to know that when a function is called, the caller push on the stack the parameters, the space for the return values, the return address (not in this order). At this level it is exactly the same to have named or unnamed return values. When the control to the called function is passed, if the called function has declared some local variables, they are pushed on the stack as well. This means that if you DO NOT have such local variables your stack is smaller than that in the case you have them. Given this, sometimes you can avoid to declare new local variables used to store intermediate results USING temporary named returns value (they already exist and shall be written when a return is encountered only... if they are unnamed. If they, on the contrary, are named... they can be used in that called routine as local variables and the last value written in them before returning shall be the return value readed by the caller. So: you can write and read whatever you want before return. Without using additional stack space. Of course you must be sure that, when returning, you shall have written the proper return values in it. Just before returning.
.
EDIT #2 - important but wrong comment
“I think you've missed MY point. Optimizing stack size can be done regardless of whether your local variable is declared in the header of the function or in the body of the function. For every example of a function which declares the return variable in the header, one can implement an equivalent function which declares the return variable in the body. These two implementations differ only in the syntax, NOT in their compiled code, hence not in their runtime behavior. – goodvibration”
This is absolutely wrong. In any (ANY) situation the stack deep during the execution of any called function is:
RLSD = sizeof(function parameters) + sizeof(return address) + sizeof(return values) + sizeof(local temporary variables in the called function)
It is IMPOSSIBLE to have it lower than the RLSD of the case where the latter is zeroed. Full stop.
The allocation of temporary variables is not a syntax sugar: it is a deep structural choice. And you can (sometimes) avoid it REUSING the space reserved for return values (it is there ANYWAY) as a temporary buffer in the called function execution. And you can do this choice, if and only, your return values are “named”.