Log In  

Okay, when viewing code for a bunch of games, there seems to be variables that are put into the parentheses after a function's name. What are the purpose of these?
My guess is that they're local variables, but I have no idea.

P#111299 2022-05-04 21:06

I can't tell if your question is about the basics or about extra variables being passed, so I'll just give a full explanation. They are local, but they also receive the values passed when the function is called.

To demonstrate:

function example_function(a,b,c)
   -- code here has access to local variables a,b,c
   -- which may or may not be nil to start
  return
end

-- this call causes a,b, and c to be nil at the start
-- of the function's code
example_function()

-- this call causes a to be 2, b to be 4 and c to be 6
-- at the start of the function's code, with 7 being part of
-- the implicit table ... instead
example_function(2, 4, 6, 7)

-- this call causes a to be 2, b to be 5, and c to be nil
-- at the start of the function's code
example_function(2, 5)

If a function needs to check whether one of the variables in the parentheses received a value, it can take advantage of the fact that nil is considered false and all other values are considered true.

function example_function(a, b, c)
  if a then
  -- code for when a was given a value
  else
  -- code for when a wasn't supplied or was specified as 
  -- either nil or false
  end
end

The variables can also be given default values by taking advantage of how boolean operators are parsed.

function example_function(a, b, c)
  a = a or 4  -- if a is nil, it will become 4
end

I think I've seen mention of listing extra variables to avoid using extra tokens for the local declarations, but at some point the local keyword stopped adding to the token count. There might be some other way that using parameters as locals saves tokens but I don't know how.

P#111302 2022-05-04 21:48

You mean like this?

function some_function(a, b) <-- these variables here?
    -- do stuff
end

If so, those are the function's parameters. And yeah, they're basically local variables which only exist inside the function. They get values assigned to them when you call the function with arguments. So, for instance:

-- this is the function definition
function some_function(a, b)
    print(a)
    print(b)
end

-- and the parameters, a and b, get values when you call the function like this
some_function(42, 'hello') -- a gets the value 42, b gets 'hello'

If you copy and paste that into a cart with nothing else and run it, you should see this:

42
hello

When you call a function you're 'binding' the parameters. So in the above example the parameter (or variable if you prefer) a is bound to the integer value 42. And the parameter b is bound to the string value 'hello'. When the function finishes running the parameters become unbound and you can call the function again with new values.

some_function(43, 'goodbye') -- now a=43 and b='goodbye'

In most programming languages if you define a function with two parameters then you have to call it with two arguments otherwise you'll get an error and your program will crash. Lua is a more forgiving in this respect. You can actually call a function with either more or fewer arguments than you've defined parameters.

So if we do this, for instance:

some_function(200)

Then the parameter a is bound to the value 200 but the parameter b is unbound. (This isn't quite true. What actually happens is b gets bound to nil but you can think about it as being unbound.)

If you do this:

some_function(1, 2, 3)

The a is bound to 1 and b is bound to 2. The value 3 is simply ignored: you won't have access to that 3 inside the function.

There are uses for passing fewer arguments than you have parameters but if you're just starting out then I wouldn't worry about those too much. Just make sure that you're always passing the same number of arguments (values) as you've defined parameters (variables) and you should be okay.

And of course you can define more variables inside the function if you need them.

function some_other_function(a, b)
   local c = a + b
   print(c)
end

Hope that helps.

P#111300 2022-05-04 22:14

Both help, thank you very much for that!

P#111549 2022-05-09 12:04

[Please log in to post a comment]

Follow Lexaloffle:          
Generated 2024-03-28 09:12:43 | 0.007s | Q:16