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

Using turtle.forward() etc. as variables or change the name of function

Started by Assyrianos, 11 June 2015 - 11:41 PM
Assyrianos #1
Posted 12 June 2015 - 01:41 AM
Hey guys, i'm very new with programming and even FTB thats why my question maybe sounds a little bit dumb., Im trying to change the name "turtle.forward()" to any variable for example to "a". I need that for special pictures and make the programming easier.

Let me just show you guys what i mean:


local a = turtle.forward()
local b = turtle.turnRight()
local c = turtle.turnLeft()
local d = turtle.back()

a()
b()
a()
c() --the order of them doesnt really matter, i need to know, how i can set that up at all

What i dont understand, is what the CC-Wiki said, i did it exactly the same way before, but that spitted more Error out…

Thanks for your help guys!
Dog #2
Posted 12 June 2015 - 01:56 AM
What you're doing is calling the functions and thus setting a, b, c, & d to the output returned by the functions you're trying to set them equal to. Removing the brackets will allow you to set your variables the way you wish, like so

local a = turtle.forward
local b = turtle.turnRight
local c = turtle.turnLeft
local d = turtle.back

a()
b()
c()
d()
flaghacker #3
Posted 12 June 2015 - 06:14 AM
But renaming functions isn't really a good practise. It may save some typing, but it makes the code harder to read in most cases.
Edited on 12 June 2015 - 04:15 AM
Bomb Bloke #4
Posted 12 June 2015 - 06:28 AM
It… depends. I don't like the readability aspect of it, not one bit. I also hate people writing functions when they don't have to (whereas some coders seem to like using function names as if they were comments, and so define them where there's absolutely no other point in doing so).

There is a potential benefit though, at least, in this case: Calling a() is faster than calling turtle.forward(), because the latter requires a table lookup whereas the former effectively has the table lookup result pre-cached.

However, that's a micro-optimisation, and you could argue that there's a time and a place for it. That is, to an extent, a matter of taste.

Certainly, though, if you're going to assign the function to a different variable, there's no excuse not to use a descriptive name for that variable.
Assyrianos #5
Posted 12 June 2015 - 08:13 PM
What you're doing is calling the functions and thus setting a, b, c, & d to the output returned by the functions you're trying to set them equal to. Removing the brackets will allow you to set your variables the way you wish, like so

local a = turtle.forward
local b = turtle.turnRight
local c = turtle.turnLeft
local d = turtle.back

a()
b()
c()
d()

Thank you Dog, so the brackets make the function start? Sorry if im not understanding it right, maybe it might be a language problem. But anyway, did what i wrote make any sence? I dont need it for my program, but i would like to know, if that could be in any way useful.


But renaming functions isn't really a good practise. It may save some typing, but it makes the code harder to read in most cases.
Ye flaghacker, i'm trying to get more used to the longer text, for me, the renaming was usefull, because i could simply write my code without any loops etc. just to make the writing easier when im thinking about using more commands. I already understood, that this way of writing is not the "right" or the optimal one, but thats how i want to see, it, from a very primitive program to a better and better program :-)
Thanks
Assyrianos

It… depends. I don't like the readability aspect of it, not one bit. I also hate people writing functions when they don't have to (whereas some coders seem to like using function names as if they were comments, and so define them where there's absolutely no other point in doing so).

There is a potential benefit though, at least, in this case: Calling a() is faster than calling turtle.forward(), because the latter requires a table lookup whereas the former effectively has the table lookup result pre-cached.

However, that's a micro-optimisation, and you could argue that there's a time and a place for it. That is, to an extent, a matter of taste.

Certainly, though, if you're going to assign the function to a different variable, there's no excuse not to use a descriptive name for that variable.

True, esp when it gets longer. In this case, this program is more a kind of learning process for me, im just gimping around with diffrent ways to the same goal. I'll change from a,b,c,d to something like up, down, left, right..


Thanks for the advice and fast answers guys!
Cheers
Assyrianos
Dog #6
Posted 12 June 2015 - 09:21 PM
Thank you Dog, so the brackets make the function start? Sorry if im not understanding it right, maybe it might be a language problem. But anyway, did what i wrote make any sence? I dont need it for my program, but i would like to know, if that could be in any way useful.
Essentially yes - adding the brackets makes the function start. For example, turtle.down points to the function and turtle.down() calls the function (executes it). So when you wish to reassign a function name (such as turtle.down) you want to assign your new variable to the function pointer (turtle.down), not actually call the function (turtle.down())