[Home] [Downloads] [Search] [Help/forum]


Register forum user name Search FAQ

Gammon Forum

[Folder]  Entire forum
-> [Folder]  MUSHclient
. -> [Folder]  Lua
. . -> [Subject]  A function with no arguments can be called with them?

A function with no arguments can be called with them?

It is now over 60 days since the last post. This thread is closed.     [Refresh] Refresh page


Posted by Cadari   (25 posts)  [Biography] bio
Date Wed 29 Aug 2007 06:03 PM (UTC)
Message
Hello. A very basic question.

function balance_equilibrium_notify()
    ...
end

balance_equilibrium_notify(gained_balance_eq)

A function with no parameters is called with a one? Is that a trick of Lua of some kind? :) gained_balance_eq is boolean if it's of any importance.
[Go to top] top

Posted by Shaun Biggs   USA  (644 posts)  [Biography] bio
Date Reply #1 on Wed 29 Aug 2007 06:28 PM (UTC)
Message
I'm not quite sure what exactly the question is here... Yes, you can call a function that does not take any parameters with a parameter in it... It just doesn't do particularly much of anything.

Lua 5.1.2  Copyright (C) 1994-2007 Lua.org, PUC-Rio
> function test()
>> print( "whee!" )
>> end
> test( true )
whee!
> test( "foo" )
whee!
>

It is much easier to fight for one's ideals than to live up to them.
[Go to top] top

Posted by Cadari   (25 posts)  [Biography] bio
Date Reply #2 on Wed 29 Aug 2007 06:56 PM (UTC)
Message
I don't know a single thing about Lua, so was wandering, if it is some kind of work-around or something alike. Like not calling the mentioned function if the parameter is nil.

Thank you!
[Go to top] top

Posted by Shaun Biggs   USA  (644 posts)  [Biography] bio
Date Reply #3 on Wed 29 Aug 2007 07:11 PM (UTC)
Message

Lua 5.1.2  Copyright (C) 1994-2007 Lua.org, PUC-Rio
> function test( foo, bar )
>> print( foo, bar )
>> end
> test( 1, 2 )
1       2
> test( 1 )
1       nil
> test()
nil     nil
>

Is this what you mean? You can check to see if a parameter has been passed before running the function.

Personally, with the example you gave, I would just test before calling the function.

if gained_balance_eq ~= nil then
  balance_equilibrium_notify(gained_balance_eq)
end

It is much easier to fight for one's ideals than to live up to them.
[Go to top] top

Posted by Cadari   (25 posts)  [Biography] bio
Date Reply #4 on Wed 29 Aug 2007 09:15 PM (UTC)

Amended on Wed 29 Aug 2007 09:17 PM (UTC) by Cadari

Message
No, sorry, I'm not that good with explaining my thoughts in English.

The code I posted is not mine. I just stumbled upon it and thought there are two possibilities:

1. It's just a minor mistake, nothing to worry about if Lua allows it.

2. It's an extremely complex Lua-only trick and this code works in some completely unexpected way :)

Now, as you told me it's just #1, I have no questions. Thanks, again.
[Go to top] top

Posted by Nick Gammon   Australia  (22,975 posts)  [Biography] bio   Forum Administrator
Date Reply #5 on Wed 29 Aug 2007 09:56 PM (UTC)
Message
Quote:

A function with no parameters is called with a one? Is that a trick of Lua of some kind?


The Lua manual clearly illustrates that functions can be called with more or less arguments than they "expect". If you supply too many arguments, they are ignored. If you supply "too few" then the extra ones are set to nil.

I cannot see a huge amount of point in writing code like your example, as calling that function with an argument, which it will not handle, seems redundant and is probably a simple coding error. However it is not a Lua syntax error.

Functions can also return multiple results, a "trick" which is used quite often.

- Nick Gammon

www.gammon.com.au, www.mushclient.com
[Go to top] top

The dates and times for posts above are shown in Universal Co-ordinated Time (UTC).

To show them in your local time you can join the forum, and then set the 'time correction' field in your profile to the number of hours difference between your location and UTC time.


16,031 views.

It is now over 60 days since the last post. This thread is closed.     [Refresh] Refresh page

Go to topic:           Search the forum


[Go to top] top

Quick links: MUSHclient. MUSHclient help. Forum shortcuts. Posting templates. Lua modules. Lua documentation.

Information and images on this site are licensed under the Creative Commons Attribution 3.0 Australia License unless stated otherwise.

[Home]


Written by Nick Gammon - 5K   profile for Nick Gammon on Stack Exchange, a network of free, community-driven Q&A sites   Marriage equality

Comments to: Gammon Software support
[RH click to get RSS URL] Forum RSS feed ( https://gammon.com.au/rss/forum.xml )

[Best viewed with any browser - 2K]    [Hosted at HostDash]