Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why would this function in Lua be called with one extra argument?

Tags:

lua

In file linalg.lua I have this function declaration:

function dot(A,B)
   return sum(mult(A,B),2); -- sum along second dimension
end

And then in another file I have these calls:

require 'linalg'

-- First fundamental Coeffecients of the surface (E,F,G)
local E = dot(Xu,Xu,2)
local F = dot(Xu,Xv,2)
local G = dot(Xv,Xv,2)

local m = cross(Xu,Xv,2)
local p = sqrt( dot(m,m,2) )
local n = div(m,concath(p, p, p))

-- Second fundamental Coeffecients of the surface (L,M,N)
local L = dot(Xuu,n,2)
local M = dot(Xuv,n,2)
local N = dot(Xvv,n,2)

What I don't understand is:

Why the dotfunction is called with three arguments (being 2 always the last of them) if the function is declared with two arguments? Is it some Lua idiom?

The code runs fine inside a system where it gives correct results, and now I have the task to translate it to Python/Numpy.

like image 425
heltonbiker Avatar asked Dec 21 '25 04:12

heltonbiker


2 Answers

Quote from http://www.lua.org/pil/5.html

Parameters work exactly as local variables, initialized with the actual arguments given in the function call. You can call a function with a number of arguments different from its number of parameters. Lua adjusts the number of arguments to the number of parameters, as it does in a multiple assignment: Extra arguments are thrown away; extra parameters get nil.

So simply extra arguments are ignored and missing arguments are nil. Yes. It is a part of how the language works and is perfectly fine to be used.

like image 124
Rochet2 Avatar answered Dec 24 '25 10:12

Rochet2


I have ended up testing myself (don't usually use Lua, but have it installed) and it seems that it IGNORES extra arguments:

For example, in the snippet below, function is declared with two arguments, but called with three, and still works, since third argument is simply discarded, it seems:

function sum(a,b)
   return a + b;
end

local a = 1
local b = 2
local c = 100

local d = sum(a,b,c)

print(d)
> 3
like image 45
heltonbiker Avatar answered Dec 24 '25 12:12

heltonbiker



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!