We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand.
A function is a group of statements that together perform a task. You can divide up your code into separate functions. How you divide up your code among different functions is up to you, but logically the division usually unique, is so each function performs a specific task.
The Lua language provides numerous built-in methods that your program can call. For example, method print() to print the argument passed as input in console.
A function is known with various names like a method or a sub-routine or a procedure etc.
Defining a Function
The general form of a method definition in Lua programming language is as follows −
A method definition in Lua programming language consists of a method header and a method body. Here are all the parts of a method −
-
Optional Function Scope − You can use keyword local to limit the scope of the function or ignore the scope section, which will make it a global function.
-
Function Name − This is the actual name of the function. The function name and the parameter list together constitute the function signature.
-
Arguments − An argument is like a placeholder. When a function is invoked, you pass a value to the argument. This value is referred to as the actual parameter or argument. The parameter list refers to the type, order, and number of the arguments of a method. Arguments are optional; that is, a method may contain no argument.
-
Function Body − The method body contains a collection of statements that define what the method does.
-
Return − In Lua, it is possible to return multiple values by following the return keyword with the comma separated return values.
Example
Following is the source code for a function called max(). This function takes two parameters num1 and num2 and returns the maximum between the two −
Function Arguments
If a function is to use arguments, it must declare the variables that accept the values of the arguments. These variables are called the formal parameters of the function.
The formal parameters behave like other local variables inside the function and are created upon entry into the function and destroyed upon exit.
Calling a Function
While creating a Lua function, you give a definition of what the function has to do. To use a method, you will have to call that function to perform the defined task.
When a program calls a function, program control is transferred to the called function. A called function performs the defined task and when its return statement is executed or when its function's end is reached, it returns program control back to the main program.
To call a method, you simply need to pass the required parameters along with the method name and if the method returns a value, then you can store the returned value. For example −
Live Demo
When we run the above code, we will get the following output.
Assigning and Passing Functions
In Lua, we can assign the function to variables and also can pass them as parameters of another function. Here is a simple example for assigning and passing a function as parameter in Lua.
Live Demo
When we run the above code, we will get the following output.
Function with Variable Argument
It is possible to create functions with variable arguments in Lua using '...' as its parameter. We can get a grasp of this by seeing an example in which the function will return the average and it can take variable arguments.
Live Demo
When we run the above code, we will get the following output.
Posted by2 months ago
Hi everyone, I'm not a lua programmer, but I was playing with clink and I wanted to make a 'fuzzy' autocompletion match generator. So here it is.
EDIT: Here you have a Repl.it with the core functionality
RE-EDIT: New version based on /u/curtisf's suggestions
It's not really fuzzy, but it's working (code at the bottom). From a given input (e.g.:) 'rcwi', it matches any file which has those initials in its first 'words', like 'run_cmd_with_initials.bat', 'run-cmd-with-initials-and-more.exe', 'run cmd with ini.exe', etc. The separators currently supported are '%.', '%s', '_', '-' and 'CamelCase'.
get_initials
) and concat
functions. So please, point out all the mistakes or improvable things (thanks!). FYI: the lua version installed in my clink is 5.2, I don't know if I can upgrade it.
EDIT: Done! That was an easy one, only needed 1 line:
text = text:gsub('(%u)', ' %1')
Thanks again.
The full code:
100% Upvoted