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

Function Arguments...

Started by Xhap, 18 July 2016 - 11:39 PM
Xhap #1
Posted 19 July 2016 - 01:39 AM
Hate to bug you :l But I'm having a bit of an issue with arguments and I'm wondering if you could clear that up <3… I'm making my program much more simplified saving me about 3 lines every time I want to repeat something using arguments however, my curiosity is getting the better of me and I want to see if I can have an if statement that says ("if args = nil then args = 1") However the error is that a Then Is expected T_T … help

function forward( args )
if args = nil … = 1
for i = 1, … do
turtle.forward()
print("going forward!")
end
end
end
end

function back( … )
if … = nil then … = 1
for i = 1, … do
turtle.back()
print("going back!")
end
end
end

back()

forward()
Bomb Bloke #2
Posted 19 July 2016 - 03:25 AM
Use = for assignments, == for comparisons.

local function forward( amount )
	if not amount then amount = 1 end  -- If amount is nil then give it a value.
	
	for i = 1, amount do
		turtle.forward()
		print("going forward!")
	end
end

local function back( amount )
	for i = 1, amount or 1 do  -- "amount or 1" resolves as "amount" if it's non-nil or 1 if it is.
		turtle.back()
		print("going back!")
	end
end

back()

forward()