Some of it pertains to basic programming concepts (functions, arguments, returning or altering values), but how all of that is put together here is language-specific.
A function doesn’t have to receive or return any arguments. Whether it receives any arguments, it can access anything in global scope. Without declaring a variable ‘local’ explicitly, things usually are in global scope.
It can take a number of arguments, and “PrintStuff” takes one (we don’t know what, and PrintStuff doesn’t really care) we’ll call ‘stuff’. PrintStuff doesn’t know what it is either.
When it’s called with “Hello World”, ‘stuff’ becomes “Hello World”, then ‘PrintStuff’ passes it on to ‘print’ (still as ‘stuff’ - ‘print’ doesn’t know what it is yet either, it’s just something named ‘stuff’ - ‘print’ won’t necessarily know it by that name though, that depends on what argument[s] ‘print’ has named in its function definition…and so on down the line.)
Functions are a convenient way of passing messages back and forth, if you will, between bits of code. I’m sure much of this is not technically correct – just trying to help with a general concept of ‘functions’ as programming tools, not worry too much about scope or instantiation of variables (or functions for that matter.)
They’re ‘sub-routines’ if you prefer to think of them that way, a way to organize your code. The one thing to keep in mind is that, whatever you do to the named ‘stuff’ within the function stays within the function. (You can give tables as arguments to functions, however, and alter the contents/elements of the tables.) If I pass a function a hard-coded “10”, it can’t change what I’ve passed it to “11” of course – but it can add 1 to the variable ‘stuff’ that is assigned the value 10 when the function is invoked.
If you wanted to change the variable(s) you pass in to a function, you can return the desired result(s) with the ‘return’ statement and assign the value of the function (what it returns) to the variable(s) you want changed:
local stuff=10
AddStuff(stuff)
print(stuff)
-- stuff is still 10
stuff=AddStuff(stuff)
print(stuff)
-- stuff should be 11 now
function AddStuff(stuff)
stuff=stuff+1
return stuff
end
I hope that helps some.