All pastes #947188 Raw Edit

return in a nutshell

public text v1 · immutable
·published 2008-03-18 08:27 UTC
rendered paste body
"return" in a nutshell.... 

The return statement is used to return values from a function. 

Example: 

function number() 
return 7 
end 

print(number()) 

'7' will print in the output. 

------------------------------------------------------------------------------------------ 

More Examples: 

Variables can be used to return values, 

function string() 
local s = "Message" 
return s 
end 

print(string()) 

'Message' will print in the output. 

------------------------------------------------------------------------------------------ 

When assigning a function to a variable, the variable becomes what the function returns. 

function number() 
return 11 
end 

n = number() 

print(n) 

'11' will print in the output. 

------------------------------------------------------------------------------------------ 

'return' acts as output. 

function add(value) 
v = value + 10 
return v 
end 

n = add(5) 

print(n) 

'15' will print in the output. 

------------------------------------------------------------------------------------------ 

Another output example: 

function add(value) 
return value + 10 
end 

n = add(6) 

print(n) 

'16' will print in the output. 

------------------------------------------------------------------------------------------ 

'return'ing nothing returns nil 

function nothing() 
return 
end 

n = nothing() 

print(n) 

'nil' will print in the output. 

------------------------------------------------------------------------------------------ 

'return'ing nothing is often used to escape a function 

---------- 
When not escaping: 

function DontEscape(value) 
if value == 0 then 
print("it was 0") 
end 
print("the end") 
end 

---------- 
DontEscape(1) 

'the end' will print in the output. 
---------- 
DontEscape(0) 

'it was 0' will print in the output. 
'the end' will print in the output. 
---------- 

When escaping: 

function Escape(value) 
if value == 0 then 
print("it was 0") 
return 
end 
print("the end") 
end 

---------- 
Escape(1) 

'the end' will print in the output. 
---------- 
Escape(0) 

'it was 0' will print in the output. 

------------------------------------------------------------------------------------------ 

'return' can be used to return multiple values. 

function NotMultiple() 
return 10 
return 20 
end 

print(NotMultiple()) 

'10' will print in the output. 

---------- 

function Multiple() 
return 10,20 
end 

print(Multiple()) 

'10 20' will print in the output. 

------------------------------------------------------------------------------------------ 

Multiple variables can be set with multiple returns. 

---------- 
Normal: 

a,b,c = 5,6,7 

print(a) 
print(b) 
print(c) 

'5' will print in the output. 
'6' will print in the output. 
'7' will print in the output. 

---------- 

function multiple() 
return 15,16,17 
end 

a,b,c = multiple() 

print(a) 
print(b) 
print(c) 

'15' will print in the output. 
'16' will print in the output. 
'17' will print in the output. 
------------------------------------------------------------------------------------------