# Lua defining a function with optional parameters (SOLVED)

**URL:** <https://forum.defold.com/t/lua-defining-a-function-with-optional-parameters-solved/3340>\
**Category:** Questions\
**Created:** [November 8, 2016, 4:28am UTC](https://forum.defold.com/t/lua-defining-a-function-with-optional-parameters-solved/3340 "2016-11-08T04:28:30Z")\
**Posts on this page:** 1\
**Showing post:** 4

<div class="post-metadata">

**Author:** ![britzl](https://forum-defold.b-cdn.net/user_avatar/forum.defold.com/britzl/32/23_2.png) [@britzl](https://forum.defold.com/u/britzl)\
**Post date:** [November 8, 2016, 5:41am UTC](https://forum.defold.com/t/lua-defining-a-function-with-optional-parameters-solved/3340/4 "2016-11-08T05:41:19Z")

</div>

Like @Ragnar_Svensson writes there is no mechanism in the language itself to indicate that a function parameter is optional, but there are plenty of examples in the Lua standard API where optional parameters exist and where the behaviour of a function depends on the number of arguments provided:

```
table.concat (table [, sep [, i [, j]]])
table.insert (table, [pos,] value)
os.date ([format [, time]])

```

…and many more.

In your code you can assign default values using the `or` logical operator like in Ragnar’s example. A lot of Lua libraries use [LDoc](https://stevedonovan.github.io/ldoc/manual/doc.md.html) to write and automatically generate documentation, and optional arguments are documented like this:

```
--- Three dashes indicate the beginning of a function or field documented
-- using the LDoc format
-- @param var1 The first argument
-- @param[opt=6] var2 The second argument
-- @param[opt=0] var3 The third argument
-- @return Max The maximum value of var1 and var2
-- @return Min The minimum value of var2 and var3
function foobar(var1, var2, var3)
	var2 = var2 or 6
	var3 = var3 or 0
	return math.max(var1, var2), math.min(var2, var3)
end

```

And the resulting generated docs would look like:

 ![](https://forum-defold-uploads.b-cdn.net/original/2X/2/21b50c39bd0c0c34540611dddafff973465159f2.png)

The above example is just a subset of what can be done using LDoc.

---

_[View the full topic](https://forum.defold.com/t/lua-defining-a-function-with-optional-parameters-solved/3340)._
