Help:Lua/Reference

Hello world example
local p = {}

-- Documentation for hello world function p.hello return "Hello, world!" end

return p

First line

 * local: This keyword declares a variable with local scope. In Lua, local variables are accessible only within the block where they are declared, as opposed to global variables which are accessible from anywhere in the script. Using `local` is a good practice for preventing naming conflicts and can improve script performance.


 * p = {}: This initializes p as an empty table. In Lua, a table is a versatile data structure that can hold arrays, dictionaries (key-value pairs), and functions. In the context of MediaWiki's Scribunto extension, p is used to store the functions that the module will export.

Documentation/Comment

 * The  in Lua denotes a line comment. You can write anything after this on the same line and it will not be interpreted.

Function Definition
function p.hello return "Hello, world!" end


 * function p.hello: This line defines a new function named `hello` as a member of the p table. The function keyword is used to start the definition of a function. In Lua, functions are first-class values and can be stored in variables and passed as arguments like any other value.
 * return "Hello, world!": The return keyword is used to specify the value that the function should output when it is called. In this case, the function hello returns the string Hello, world!.


 * end: This keyword signifies the end of the function definition. In Lua, if conditions and functions need to be terminated with end.

Return Statement
return p: At the end of the script, p, which now contains the hello function, is returned. In the context of a Scribunto Lua module, this return statement makes the hello function available to other pages on the wiki that invoke this module. When another page uses, it calls the hello function from this module, which in turn returns the string Hello, world!.