This is a read-only snapshot of the ComputerCraft forums, taken in April 2020.
Geko4515's profile picture

Function returns to another function

Started by Geko4515, 31 August 2013 - 05:13 AM
Geko4515 #1
Posted 31 August 2013 - 07:13 AM
Topic: Function returns to another function


Hello, I am working on something like the following:


cords {x=5, y=1, z=1}

function getCords(cord)
	--Returns x,y,z or if string specified just that cord
	if type(cord) == "string" then string.lower(cord)
		else cord = nil end
	if cord == nil then
		return cords.x, cords.y, cords.z
	elseif cord == "x,z" then
		return cords.x, cords.z
	elseif cord == "x,y" then
		return cords.x, cords.y
	elseif cord == "y,z" then
		return cords.y, cords.z
	elseif cord == "x" then
		return cords.x
	elseif cord == "y" then
		return cords.y
	elseif cord == "z" then
		return cords.z
	end
end

when I call the function with a print I get the following:
print(getCords("x,z")) output: 51
seems okay but expected a space in between.

Now my problem is when I do function call inside another function
newy = 10
check = checkCords(getCords("x,z"),newy)
I print the parameters passed in and get: 5 10

Any assistance is greatly appreciated.
Bubba #2
Posted 31 August 2013 - 08:51 AM
Split into new topic.
floppyjack #3
Posted 31 August 2013 - 09:12 AM
What you need to do is the following:

newy = 10
tCords = { getCords("x,z") }   -- This puts the return values of getCords("x,z") in a table
table.insert(tCords, newy)	  -- This adds newy to the table
checkCords(unpack(tCords)) -- This unpacks the table and calls checkCords with the result

unpack(table) returns a comma-seperated list of the contents of the table.
kreezxil #4
Posted 31 August 2013 - 09:18 AM
I think i just learned something new here. Thanks floppyjack!
floppyjack #5
Posted 31 August 2013 - 09:53 AM
I think i just learned something new here. Thanks floppyjack!
No problem! :)/>
GopherAtl #6
Posted 31 August 2013 - 09:58 AM
to clarify what was actually happening in the original code, value lists returned by functions can only be added to the end of other value lists.

explanation in code:

function f()
  return 1,2
end

print(f(),3) -- this will only print 13 - the value list returned by f() will be truncated, only the first value will be kept
print(0,f()) -- this will print 012, as the whole list can be added to the end of a list
Geko4515 #7
Posted 31 August 2013 - 02:40 PM
Thanks GopherAtl and floppyjack, That clarifies a bunch.

I ended up just doing temp variables rather than a table

local x, z = getCord("x,z")
check = checkCord(x, z, newy)