Why does my code print only once?

It is super-easy to fix; just do it return nil , but why doesn't my code work without this line?

 function x(bool) if bool then return "!" end end print(x(true), x(false), x(false)) 

What is even more confusing is that it always prints nil as many times as I cause x(false) subtract 1.

I can not imagine how this happens.

+5
source share
1 answer

The manual says:

If control reaches the end of a function without encountering a return , the function returns without results.

Note that returning a result is not different from returning nil .


In this call:

 print(x(true), x(false), x(false)) 

both x(false) do not return anything, however everything except the last element is always corrected for only one result.

Usually we see a function call that returns one or more results, remaining only with the first. Here the result is not filled with the nil character.

+6
source

Source: https://habr.com/ru/post/1233370/


All Articles