- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Return statement in Lua Programming
There are certain cases where we want to return a value from a given function so that we can use it later. These return values make use of a return keyword which in turn allows a function to return values.
There is an implicit return at the end of any function, so you do not need to use one if your function ends naturally, without returning any value.
It should be noted that the return statement is optional; if not specified, the function returns nil.
Also, only one return statement is allowed in a function.
Syntax
return expression/value
Now let’s consider an example where we would like to return a value from a function and then use that value and print it.
Example
Consider the example shown below −
function bar() return 2 * 10 end x = bar() print(x)
Output
20
The above example is the most common use-case of return statements. Let’s consider one more example of return statement in Lua.
Example
Consider the example shown below −
local v = 16 function foo() if v == 16 then return end print("foo") end print("Completed")
Output
Completed
- Related Articles
- Break statement in Lua Programming
- Return statement in Dart Programming
- Comments in Lua Programming
- table.pack() function in Lua programming
- table.unpack() function in Lua programming
- Lexical Conventions in Lua Programming
- math.ceil() function in Lua programming
- math.floor() function in Lua programming
- math.max() function in Lua programming
- math.modf() function in Lua programming
- select() function in Lua programming
- Semicolon Conventions in Lua Programming
- Sort function in Lua programming
- string.byte() function in Lua programming
- string.char() function in Lua programming
