7
\$\begingroup\$

I'm golfing in Lua, which does support such thing as multiple return values like this:

function multret()
    return 1, 'hello'
end

local a,b = multret()
-- a is 1, b is 'hello'
local c = multret()
-- c is 1
print(multret())
-- print "1<tab>hello"
local tab = {multret()}
-- create table (array) containing all returned results (equal to {1, 'hello'})

Now, my problem exactly: there is library function string.gsub whose result I directly return from my function. This lead to two values being returned from my submission: resulting string (answer required) and amount of replacements done.

Is it fine to return garbage values after required results? I think so, because assigning them to value (like in my example with variable c) will lead to same result, just want to be sure. In other words, would my multret function be a valid answer to challenge asking to "create function which return 1"?

\$\endgroup\$
  • 3
    \$\begingroup\$ I think it will depend on the challenge. Usually, the use case is to print the exact result of the func call, and in this case it won't be a good answer => you'll have to return only what's needed. But I don't have every single case in my mind right now so there might be cases where it is okay to return something unrelated to the answer. \$\endgroup\$ – V. Courtois Aug 9 at 11:35
  • \$\begingroup\$ I'm not familiar with Lua, is it correct that the amount of outputs is basically determined from the "outside" (i.e. when you call the function you have to decide how many outputs you want to get)? If so I think MATLAB has a similar feature. \$\endgroup\$ – flawr Aug 9 at 12:05
  • \$\begingroup\$ @flawr Yes, with pretty much default option to pass them all. \$\endgroup\$ – val Aug 9 at 12:48
  • \$\begingroup\$ @flawr I've extended question with more examples when result amount is kept unchanged. \$\endgroup\$ – val Aug 9 at 12:53
  • 3
    \$\begingroup\$ Pretty sure we have a consensus on this under default I/O that disallows this. But there's never any harm in asking, on a per challenge basis, whether your output format is permissible. \$\endgroup\$ – Shaggy Aug 9 at 22:08
  • 3
    \$\begingroup\$ These kinds of functions, when used as a value, have the value of the first returned thing, e.g. here. I'd imagine then that for challenges, the first returned value could be considered its return value \$\endgroup\$ – Conor O'Brien Aug 10 at 23:12

You must log in to answer this question.

Browse other questions tagged .