Module:Basic example

Lát'ọwọ́ Wikipedia, ìwé ìmọ̀ ọ̀fẹ́

Usage[àtúnṣe àmìọ̀rọ̀]

This module is used to demonstrate basic scribbling. It is used by Template:Basic scribbling example. The module takes one or two numerical inputs and outputs a string, with or without numbers, based on their relationship. An optional named parameter adds an extra string.

{{#invoke:Basic example|Name2|2}} produces Lonely

{{#invoke:Basic example|Name2|3|1}} produces 2

{{#invoke:Basic example|Name2|1|3}} produces Be positive!

{{#invoke:Basic example|Name2|4|1|lucky=yeah}} produces 3 is my lucky number.

Use the template for general input, as it is simpler.

Refer to the notes in the code for details, using edit for a better view.

{{#invoke:Basic example|function_name}}


-- This is a note.

local p = {} -- Use unexplained code as is.

function lucky(a, b) -- One can define custom functions for use. Here we define a function 'lucky' that has two inputs a and b. The names are of your choice.
	if b == 'yeah' then -- Condition: if b is the string 'yeah'. Strings require quotes. Remember to include 'then'.
		return a .. ' is my lucky number.' -- Outputs 'a is my lucky number.' if the above condition is met. The string concatenation operator is denoted by 2 dots.
	else -- If no conditions are met, i.e. if b is anything else, output specified on the next line.  'else' should not have 'then'.
		return a -- Simply output a.
	end -- The 'if' section should end with 'end'.
end -- As should 'function'.

function p.Name2(frame) -- This section is the core of the module. 'Name2' is a name of your choice. The same name needs to be referred to when the module is used.
	-- The next five lines are mostly for convenience only and can be used as is for your module. The output conditions start on line 20.
	local pf = frame:getParent().args -- This line allows template parameters to be used in this code easily. The equal sign is used to define variables. 'pf' can be replaced with a word of your choice.
	local f = frame.args -- This line allows parameters from {{#invoke:}} to be used easily. 'f' can be replaced with a word of your choice.
	local M = f[1] or pf[1] -- f[1] and pf[1], which we just defined, refer to the first parameter. This line shortens them as 'M' for convenience. You could use the original variable names.
	local m = f[2] or pf[2] -- Second shortened as 'm'.
	local l = f.lucky or pf.lucky -- A named parameter 'lucky' is shortend as l. Note that the syntax is different from unnamed parameters.
	if m == nil then -- If the second parameter is not used.
		return 'Lonely' -- Outputs the string 'Lonely' if the first condition is met.
	elseif M > m then -- If the first condition is not met, this line tests a second condition: if M is greater than m.
		return lucky(M - m, l) -- If the condition is met, the difference is calculated and passed to the the self defined function along with l. The output depends on whether l is set to 'yeah'.
	else
		return 'Be positive!'
	end
end

return p