Help with functions

If I had a function that went something like…

function doStuff(c,d)
     a = c + d
     b = c * d
     return a, b
end

Is there a way in lua for me to call doStuff() and have Codea use just a or just b instead of both a and b?

function doStuff(c,d,i) --i tells function what to return
     a = c + d
     b = c * d
     if i=1 then return a 
     elseif i==2 then return b 
     else return a,b end
end

Thanks. I thought that Lua would have a built in method to handle this stuff, but this works perfectly.

Lua is a very simple language. That means you have to do some stuff yourself, but it is extremely flexible - just see what tables can do!

Or, how about passing parameters back and forth from the function using a table of named keys?

function doStuff(t)
    t.aVariable = t.aVariable + (t.anotherVariable or 0) --you can test whether the table contains a variable and supply a default if it does not by using "variable or default"
    t.myString = t.myString..t.anotherString
    return t
end
...
local t = doStuff{aVariable = 5, myString = "hello ", anotherString = "world"} 
-- if a function just takes a single table or string, you can omit the outer layer of () brackets

print(t.myString, t.aVariable)

As you’ll know what the return values are and whcih one you’d want (using @Ignatz’s idea) you can just use the common practice of using a global _ to ignore the values you don’t want eg.

  a,_ = doStuff(c,d)
  _,b = doStuff(c,d)
 

You only need the _ in the second case, and it doesn’t have to be global:

local a = doStuff(c,d)
local _,b = doStuff(c,d)

Or just do a,b=doStuff(c,d) and use a or b or both depending on which one you want.

admirable, many solutions to just one question