Updated April 10, 2023
Introduction to Lua return
The Lua return statement is used to return result from a function or to finish a function. The return statement is a built in statement or keyword in the Lua programming. The return statement is a special statement which is used inside a method or function to send the functions’s result back to the function caller. The return statement contain return keyword followed by return value which is optional. The function can return any data type value which are supporting in Lua and also can return multiple results. In it there is an implicit return at the end, which means if a function is to be end naturally without returning any value then no need to have return statement. Note that the return statement should be the last statement of a block or function, but before an end statement of a function as per the syntactic reason.
Syntax of Lua modulo operator:
function fun_name( ):
statements
.
.
return [expression]
Parameter:
- expression: This is an optional, that specifies the return value of the function.
Working of return Statement in Lua Programming
- The return statement used to return the result of a function to the caller.
- The return statement used as the last statement of a function which consist of return keyword and return value as “return 50” here the function return the 50 value as an output to the caller.
Examples
Given below are the examples mentioned:
Example #1
Example of return statement in lua programming to show the usage of return statement.
Code:
-- Lua program to show return statement
function add( x, y )
-- returning sum of a and b
return x + y
end
function sub( x, y )
-- returning subtraction of a and b
return x - y
end
function divide( x, y )
-- returning division of a and b
return x / y
end
function multiply( x, y )
-- returning multiplication of a and b
return x * y
end
-- calling functions
res = add( 20, 30 )
print( "Result of add function is :", res)
res = sub( 20, 30 )
print( "Result of subtraction function is :", res)
res = divide( 20, 30 )
print( "Result of division function is :", res)
res = multiply( 20, 30 )
print( "Result of multiplication function is :", res)
Output:
As in the above program functions are created to perform the basic calculation and return the result, further the function are called and stored the return result into a variables as we can see in the above output.
Example #2
Example of return statement in lua programming to show the table data types return.
Code:
-- Lua program to show return statement for table
function create_table( )
city = { "Bangalore", "Delhi", "Mumbai", "Hyderabad" }
-- returning city table
return city
end
-- calling function
r_city = create_table( )
print( "The city table is : ", r_city)
print( "The type of city table is ", type(r_city) )
print( "The length of the city table is : ", #r_city )
-- still table is a key-value association.
print( "Table value at index 1 is : ", r_city[1] )
print( "Table value at index 2 is : ", r_city[2] )
print( "Table value at index 3 is : ", r_city[3] )
Output:
As above, the create_table( ) function is created, inside this function the city table is created which contain the name of the cities and which will have the implicit key. So, the create_table( ) function will return this city table whenever this function is called. Further the create_table( ) function is calling and which return the value as table that is city table itself and return value functionality and specification are same as table not change anything.
Example #3
Example of return statement in lua programming to show the multiple values are return as individual and as an array.
Code:
-- Lua program to show return statement for multiple values
function ret_mul( )
name = "John"
age = 24
return name, age
end
function ret_mulAsList()
name = "John"
age = 24
-- return table as array
return {name, age}
end
-- calling function
r_name, r_age = ret_mul()
print( "The return values are ", r_name, " and ", r_age)
r_name, r_age = ret_mulAsList()
print( "The return values as list are ", r_name, " and ", r_age)
r_value = ret_mulAsList()
print( "The return values as list are ", r_value)
Output:
As above, the two function are created ret_mul( ) and ret_mulAsList( ), where the ret_mul( ) function is returning multiple values individually and the ret_mulAsList( ) function is returning multiple values as an array which is again a table. Further both function are calling whose return result is storing into a variable and displaying them, as we can see in the above output.
Conclusion
The Lua return keyword is a built in keyword in the Lua programming, which is used to return the result from a function. There is an implicit return at the end of the Lua functions. As per syntactic reason the return statement should be the last statement of a block or function, before the end keyword.
Recommended Articles
We hope that this EDUCBA information on “Lua return” was beneficial to you. You can view EDUCBA’s recommended articles for more information.