lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]


> Is there a difference between using
>   return(0);
> and
>   lua_pushnil(L);
>    return(1);
> in C functions?

Sometimes. It is the same as the difference between

  return

and

  return nil

in Lua functions.

If the function is called in a multiple-return context, it makes a 
difference, but it is not very easy to see this difference from Lua. On 
the other hand, it is quite possible to see the difference if the function 
is called from C; I would say that the bug is then in the C call, rather 
than the function returning (or not) the value, but that's a matter of 
opinion.

Here is an example of where it would make a difference:

function howmany(...)
  return table.getn(arg)
end

function return_nil() return nil end
function return_nothing() return end
  -- the return could be left out of return nothing,
  -- I just put it in to be explicit

> =howmany(return_nil())
1
> =howmany(return_nothing())
0
> -- force the context to be single-value return
> =howmany((return_nothing()))
1