3

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.

7
  • lua.org/pil/5.2.html Commented May 30, 2014 at 17:58
  • sorry, lua-users.org/wiki/FunctionsTutorial Commented May 30, 2014 at 18:00
  • @hjpotter92 from what I have read in your link, it's not clear if I can declare a function with two parameters and call it with 3 (by the way, I have never seen this in any language without an error message of some sort). Commented May 30, 2014 at 18:02
  • yes, you have seen it in several languages, but didn't recognize it directly. For eg. print a, b, c, ... in python. The extra arguments are ignored when you're calling a function. Less number of arguments won't raise an error either. They will be assigned a nil value. Commented May 30, 2014 at 18:10
  • @hjpotter92 just to clarify (sorry for the insistence), as I understand, you can make this only if you have def function(args) and then you unpack args, or check its length. That doesn't seem to be the case, function dot (A,B) states that you should call it with two arguments exactely. It's possible that former programmer thought the 2 parameter was needed when actually it's not, but then I wonder if that would generate an error, or the program would run properly, in the Lua case. Commented May 30, 2014 at 18:15

2 Answers 2

3

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.

Sign up to request clarification or add additional context in comments.

Comments

2

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 

1 Comment

It's not discarded. It's on the stack but inaccessible using built-in functionality. echo "x=1 y=2 z=3 function f(a,b) end f(x,y,z,4)" | luac -l -l -v -

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.