“regresa lua” Código de respuesta

regresa lua

--if return is confusing like it was for me let me tell you
--how it became easy for me.
--see this
  
function Name()
return "Ali"
end

local name = Name()
print(name)
--it should print Ali
--that how return in lua works you Call a function as a Veriable
--that Veriable will Catch the return know that in lua functions
--yield a thread that means line 8 will yield until the Name() function has
--finished executing thats what i call that function dead / we cant
--resume it anymore. i dont wanna get into coroutines just know a thread /
--function is dead when arriving to end / return or an error line.
      1
--here is a very usefull example:
      
function MagnetudeCalculator(objvector3, targetvector3)
    local magnitude, success, exeption
        
    if objvector3 == nil then -- we check if objvector3 is nil
    	magnitude, success, exeption = nil, false, "Vector3 Expected Not Nil!"
       return magnitude, success, exeption   -- this will return nil, false, "Vector3 Ex..."
    end
        
    if targetvector3 == nil then -- we check if targetvector3 is nil
    	magnitude, success, exeption = nil, false, "Vector3 Expected Not Nil!"
        return magnitude, success, exeption   -- this will return nil, false, "Vector3 Ex..."
    end
        
    magnitude, success, exeption = (objvector3 - targetvector3).Magnitude, true, "No Errors"
    return magnitude, success, exeption 
end

-- here we Call the function from diffrent ways to get diffrent results
local magnetude, success, exeption = MagnetudeCalculator(Vector3, Vector3)
print(magnetude, success, exeption) -- some numbers, true, No Er...

local magnetude, success, exeption = MagnetudeCalculator(Vector3)
print(magnetude, success, exeption) -- nil, false, Vector3 Ex...

local magnetude, success, exeption = MagnetudeCalculator(nil, Vector3)
print(magnetude, success, exeption) -- nil, false, Vector3 Ex...

-- just like you guys see return is very usefull to use
-- you could also use return just like that to end a function like:
function Fun()
  
	if 1+1 == 2 then
    
    	return -- the function ends / function is dead and cannot be resumed
    
    end
  
end

Fun()-- the script has gone back to Fun() and will come back when Fun() ends or when the function reaches return
print("Fun Has Ended!")-- this will print when Fun has reached end or return

-- the function wont reach end because its already returned so this whole 
-- prosses could be very confusing just know that if u use return
-- nothing behind the function resumes but it will go back to the caller
-- see all this proccess like a very fast marker the marker will
-- mark everything but not functions until the function gets called
-- then the marker will go through and executes the function
-- and when return is used the marker will go back to where
-- the function was called that could be anywhere in the script
-- or mabey a diffrent script if you are not using the same
-- script for the call it doesnt matter return will always find a way
-- just be carefull what you send because im not a profissional
-- but there may be limits to return veriables check the official
-- lua website for this ill attach a link down below
-- and know break is not the same as return break
-- is used to break out of a loop an continue the function
-- / script but when a return is used in a loop the loop and
-- function will be dead that means the function will not be
-- executed further instead it will be returned to the sender

function LoopBreak()
  
  	while true do
    
		local i = 0

    	if i == 0 then
      
      	break
      	
      	end
  	end
  
  	print("Broke") -- this should print out
end

function LoopReturn()
  
  	while true do
    
		local i = 0

    	if i == 0 then
      
      	Return
      	
      	end
  	end
  
  	print("Broke") -- tthis will never be printed
end

LoopBreak()

-- lastely what i mean by dead i mean that the function can always
-- be called again but it will never be resumed in case of coroutines
-- coroutines are hard to master and more in the advanced way of
-- lua programming they have thier own rules and complications
-- like always coroutines are also pretty usefull
-- just to tell u guys a brief about coroutines
-- coroutines can be yielded, resumed, and the most benifits from
-- using coroutines they dont yield a thread but make thier own thread

-- all credits goes to ME!
Botter3029

Función return lua

function factorial(x)
  if x == 1 then
    return 1
  end
  return x * factorial(x-1)
end
Vitus

romper en lua

    local i = 1
    while a[i] do
      if a[i] == v then break end
      i = i + 1
    end
Naughty Nightingale

Respuestas similares a “regresa lua”

Preguntas similares a “regresa lua”

Explore las respuestas de código populares por idioma

Explorar otros lenguajes de código