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

Function Help

Started by civilwargeeky, 08 September 2012 - 12:59 AM
civilwargeeky #1
Posted 08 September 2012 - 02:59 AM
I don't really want an answer a specific problem, but I don't know where else to put this. So I recently got into programming in lua (after programming in TI-BASIC [TI-84 Calculator]), and I don't understand how to use functions. On the calculator I would just execute another program within the program, using global variables in all. I realize that in lua you can use the io.read as a variable and set specific parameters in the function name (I think), but I don't really understand the relationship between the function, read, and the rest of the program. I guess I would like someone to clarify.
Lettuce #2
Posted 08 September 2012 - 03:23 AM
Functions in and of themselves are not related to io.read(). In fact, io.read() IS a function call, as characterized by (). "function" lets you set a function to run some functions, which makes no sense. Here's a sample:

function goForward()
turtle.forward()
end
That essentially renames turtle.forward(), but they can run multiple functions without having to type them all out using function. You will use them frequently. io.read() lets you set variables to user input. I use it often. This next bit of code, another sample, repeats a user's input.

quote = io.read()
print "You said:"
print(quote)
Again, absolutely useless, but you can see how they work. it can be quite powerful. ALSO: Protip, Users speak only strings. If a user types a number, like 5, Lua interprets "5." So it can't be used as a number in that state, only a string. Numbers are converted to strings automatically when printed, but strings must be manually set to a number like so:

anumber = tonumber(io.read())

Hopefully that clears things up. Enjoy the mod!
civilwargeeky #3
Posted 08 September 2012 - 03:38 AM
Thanks! That actually helped a lot. One more thing I would like to know is when you have something like

function = lol(thing)
stuff()
otherstuff()
end
How would you get the function to do something based on thing? And what exactly does change in the function?
Lettuce #4
Posted 08 September 2012 - 03:49 AM
Hmm… I've never tried that. I usually put arguments in the function, like so:

function lol()
   if thing == "ha ha" then
	  stuff()
	  otherstuff()
   end
end

And what do you aim to change in the function? Can you rephrase that? I don't follow. Change… a variable? That is:

x = x+1
You can also subtract(-), multiply(*), divide(/) or use an exponent(^).
Hope that helps.

*edit
P.S. Do not say function = lol() it is just function lol().
civilwargeeky #5
Posted 08 September 2012 - 04:05 AM
I guess I want to know if I should really care about anything that you can put in the (). I've seen some examples where they put (str) or ( str ) there and I don't really get that.
And how with print() you would put things in like print("Thing") or print(var). And setCursorPos() is setCursorPos(1,1). I was just curious how what you put in the parentheses could affect the rest of the function. If I'm still not clear enough then don't worry about it. I understand what I need to know.
Lettuce #6
Posted 08 September 2012 - 04:17 AM
I understand. Again, I have never put things in those parenthesis to experiment, but I know you can. I believe it is tArgs. Try looking it up on the ComputerCraft wiki. It's right here on computercraft.info. Or click "News" then wiki. That aside, if you want to know how to loop a function based on a variable, which IS within my realm of knowledge, I can give you that. That's usually what tArgs is used for anyway.

function goForward()
var = tonumber(read())
   for i = 1,var do
   turtle.forward()
   end
end
Luanub #7
Posted 08 September 2012 - 04:24 AM
It depends on how you want to use the arguments.

So doing this

function example(...) -- will take an unlimited number of arguments and store them in a table.
for x=1, #tArgs do
print(tArgs[x]) -- will print all the args sent to the function
end
end

If you want to set a specific number of args just list in the ()'s what var names you want to capture the arguments in.

function example( arg1, arg2, arg3)
print(arg1)
print(arg2)
print(arg3)
end

Here is what should be a working function for a turtle movement using arguments.

function fwd( num )
if not num then num = 1 end
num = tonumber(num)
for x=1, num do
  turtle.forward()
end
end

fwd(8) -- will move the turtle forward 8 spaces.
civilwargeeky #8
Posted 08 September 2012 - 04:28 AM
Thanks guys. You were very helpful. That was exactly what I was looking for.