generic image
Processing...
  • Games
  • Catalog
  • Develop
  • Robux
  • Search in Players
  • Search in Games
  • Search in Catalog
  • Search in Groups
  • Search in Library
  • Log In
  • Sign Up
  • Games
  • Catalog
  • Develop
  • Robux
   
ROBLOX Forum » Game Creation and Development » Scripters
Home Search
 

Re: So.. If functions are variables then

Previous Thread :: Next Thread 
xPRESIDENTx is not online. xPRESIDENTx
Joined: 10 Jun 2011
Total Posts: 815
31 Dec 2013 01:34 PM

function GetNumber(a)
if a == 1 then
return (function()
print(a);
end)
elseif a == 2 then
return (function()
print(a);
end)
end
end

d = GetNumber(1)
d()

Prints 1. This has alot of potential in my mind for like maybe a substitute for properties? just return different functions depending on the variable a.
Report Abuse
lombardo2 is not online. lombardo2
Joined: 30 Nov 2008
Total Posts: 1604
31 Dec 2013 01:51 PM
Fucntions are not variables. In Lua, functions are first class citizens, meaning you can pass them as arguments, return them from other functions and assign them to variables, but functions are not variables, they are different concepts.
Report Abuse
xPRESIDENTx is not online. xPRESIDENTx
Joined: 10 Jun 2011
Total Posts: 815
31 Dec 2013 02:04 PM
You can use them as variables, kinda

k = function

I was told that they were and it doesnt matter they work the same way.
Report Abuse
RyanDolan123 is not online. RyanDolan123
Joined: 05 Mar 2009
Total Posts: 17919
31 Dec 2013 02:04 PM
Functions are variables. They're stored as a different TYPE of variable, and they're the only ones you can call.


You can initialize functions just like other variables

local funct = function()
print("hi")
end

funct()


and they can be used just like any other variable, and also be called as you would want to do for the function
Report Abuse
xPRESIDENTx is not online. xPRESIDENTx
Joined: 10 Jun 2011
Total Posts: 815
31 Dec 2013 02:10 PM
Yes, I just thought this had so much potential for like virtual properties.
Report Abuse
morash is not online. morash
Joined: 22 May 2010
Total Posts: 5834
31 Dec 2013 02:17 PM
You can actually concatenate functions onto a string, but it gives some weird code, which might be considered by some to show that Lua functions are variables. Putting cat = function() print("Hi") end print(cat) into the command bar demonstrates that functions can be set a variables and shown as variables.
Report Abuse
Oysi is not online. Oysi
Joined: 06 Jul 2009
Total Posts: 9058
31 Dec 2013 02:32 PM
"Functions are variables. They're stored as a different TYPE of variable, and they're the only ones you can call."

No. No. And more no.


@Everyone
Functions aren't variables. Just the same as 2 isn't a variable. However, variables can be functions, or hold a function, if you will. The code [ function a() end ] is translated to (or treated as) [ a = function() end ], so they aren't special monsters of doom. They are simple and wonderful little creatures. A special thing about functions (or function calls, I should say) is that they can be both a statement and an expression.


@OP
Yes, returning a function can add certain features, although I don't see a situations where you want to return different things. You generally want your stuff to be consistent, and you want to know what you're working with. Instead of having one function to check your values, and return different functions depending on that, you should rather just call those different functions from the start.

It seems to me like you think of the code a bit too little programmer-y. You think of certain mixes of certain things doing certain things, when what you should be thinking of is what certain individual things do. Then you can mix them together and do whatever you want, as well as have a completely clear understanding of things. This actually applies to a lot of people, probably like half of Scripters or something.
Report Abuse
xPRESIDENTx is not online. xPRESIDENTx
Joined: 10 Jun 2011
Total Posts: 815
31 Dec 2013 06:11 PM
@os well I know a lot about Lau so I try to expand my knowledge by doing what you said, using tools in ways almost never thought of. I can think of some practicable uses of this such as a global funcyion that controls the whole game.
Report Abuse
Previous Thread :: Next Thread 
Page 1 of 1
 
 
ROBLOX Forum » Game Creation and Development » Scripters
   
 
   
  • About Us
  • Jobs
  • Blog
  • Parents
  • Help
  • Terms
  • Privacy

©2017 Roblox Corporation. Roblox, the Roblox logo, Robux, Bloxy, and Powering Imagination are among our registered and unregistered trademarks in the U.S. and other countries.



Progress
Starting Roblox...
Connecting to Players...
R R

Roblox is now loading. Get ready to play!

R R

You're moments away from getting into the game!

Click here for help

Check Remember my choice and click Launch Application in the dialog box above to join games faster in the future!

Gameplay sponsored by:
Loading 0% - Starting game...
Get more with Builders Club! Join Builders Club
Choose Your Avatar
I have an account
generic image