Log in to vote
0

difference between end and return?

Asked by 7 years ago

I cannot rap my head around that.

1 answer

Log in to vote
2
Answered by 7 years ago

The end keyword ends a block, such as a function or an if statement. The return keyword, however, stops execution of a function and returns the value you provide after the keyword. If you don't provide any value, it will return nil.

function test(s)
    if s == 2 then
        return "hello"
    elseif s == 3 then
        return
    elseif s == 4 then
        return 50
    end
    return "yolo"
end

print( test(2) )
--> hello

print( test(3) )
--> nil

print( test(4) )
--> 50

print( test() )
--> "yolo"

print( test('yo dawg') )
--> "yolo"
Ad

Answer this question