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

[Ask A Pro] Can I redifine what a function does later on in the code?

Started by JustIan12, 27 October 2016 - 10:57 AM
JustIan12 #1
Posted 27 October 2016 - 12:57 PM
So basically

local function C()
    -- C Boot

    term.clear()
    term.setCursorPos(1, 1)
    term.write("Stage C")
    turtle.turnRight()
    turtle.place()
    turtle.turnLeft()
    turtle.back()
    turtle.turnRight()
    turtle.forward()
    turtle.turnLeft()


    -- Functions
    local function x()
	    turtle.turnLeft()
	    turtle.back()
	    turtle.turnRight()
    end

    local function moveBackAndPlace(n)
	    for i = 1, n do
		    turtle.back()
		    turtle.place()
	    end
    x()
    end

    -- Arguement List

    a = {5,4,3,3,1,2,1,2,1,1,1,1,1}

    for i,v in ipairs(a) do
	    moveBackAndPlace(v)
    end
end

I want to redo the code to do a second layer but I need to change the Argument list can I redefine the function so that It does not effect earlier code.
KingofGamesYami #2
Posted 27 October 2016 - 01:05 PM
You can also pass tables as arguments to functions. I would also recommend moving your function declarations out of your C function, as it will be less resource-intensive.


-- Functions
local function x()
    turtle.turnLeft()
    turtle.back()
    turtle.turnRight()
end

local function moveBackAndPlace(n)
    for i = 1, n do
        turtle.back()
        turtle.place()
    end
    x()
end

local function C( a )
    -- C Boot

    term.clear()
    term.setCursorPos(1, 1)
    term.write("Stage C")
    turtle.turnRight()
    turtle.place()
    turtle.turnLeft()
    turtle.back()
    turtle.turnRight()
    turtle.forward()
    turtle.turnLeft()

    for i,v in ipairs(a) do
        moveBackAndPlace(v)
    end
end

C( {5,4,3,3,1,2,1,2,1,1,1,1,1} )
JustIan12 #3
Posted 28 October 2016 - 08:28 AM
Ahh thanks :P/>