Jump to content




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


  • You cannot reply to this topic
5 replies to this topic

#1 Assyrianos

  • New Members
  • 2 posts

Posted 12 June 2015 - 12: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!

#2 Dog

  • Members
  • 1,179 posts
  • LocationEarth orbit

Posted 12 June 2015 - 12: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()


#3 flaghacker

  • Members
  • 655 posts

Posted 12 June 2015 - 05: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 by flaghacker, 12 June 2015 - 05:15 AM.


#4 Bomb Bloke

    Hobbyist Coder

  • Moderators
  • 7,099 posts
  • LocationTasmania (AU)

Posted 12 June 2015 - 05: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.

#5 Assyrianos

  • New Members
  • 2 posts

Posted 12 June 2015 - 07:13 PM

View PostDog, on 12 June 2015 - 12:56 AM, said:

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.


View Postflaghacker, on 12 June 2015 - 05:14 AM, said:

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

View PostBomb Bloke, on 12 June 2015 - 05:28 AM, said:

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

#6 Dog

  • Members
  • 1,179 posts
  • LocationEarth orbit

Posted 12 June 2015 - 08:21 PM

View PostAssyrianos, on 12 June 2015 - 07:13 PM, said:

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())





3 user(s) are reading this topic

0 members, 3 guests, 0 anonymous users