function with multiple default arguments

Questions about the LÖVE API, installing LÖVE and other support related questions go here.
Forum rules
Before you make a thread asking for help, read this.
Post Reply
User avatar
HanaIndiana
Prole
Posts: 12
Joined: Fri Jul 10, 2015 12:48 am
Location: Northern CA
Contact:

function with multiple default arguments

Post by HanaIndiana »

Not sure why I can't find this question anywhere. I feel like I'm missing something obvious.
Assume there is a function like this:

Code: Select all

function myFunc(a,b,c)
    b = b or 10
    c = c or -1
    print (a,b,c)
end
Since both 'b' and 'c' have defaults, I'm guessing there isn't a way to set 'c', but leave b as default?
Because myFunc(10,20) would set a and b, and leave c as default. Or can you use some type of explicit assignment, like this... myFunc(10,c=20) ?
User avatar
Nixola
Inner party member
Posts: 1949
Joined: Tue Dec 06, 2011 7:11 pm
Location: Italy

Re: multiple default arguments

Post by Nixola »

You'd have to do myFunc(10, false, 20) or myFunc(10, nil, 20).
lf = love.filesystem
ls = love.sound
la = love.audio
lp = love.physics
lt = love.thread
li = love.image
lg = love.graphics
User avatar
HanaIndiana
Prole
Posts: 12
Joined: Fri Jul 10, 2015 12:48 am
Location: Northern CA
Contact:

Re: multiple default arguments

Post by HanaIndiana »

I knew I was missing something simple. Much thanks!!
Nixola wrote: Sat Aug 05, 2017 5:57 pm You'd have to do myFunc(10, false, 20) or myFunc(10, nil, 20).
davisdude
Party member
Posts: 1154
Joined: Sun Apr 28, 2013 3:29 am
Location: North Carolina

Re: function with multiple default arguments

Post by davisdude »

In this case, nil would be preferred over false because nil represents the absence of a variable, while false could be an argument.
GitHub | MLib - Math and shape intersections library | Walt - Animation library | Brady - Camera library with parallax scrolling | Vim-love-docs - Help files and syntax coloring for Vim
User avatar
zorg
Party member
Posts: 3468
Joined: Thu Dec 13, 2012 2:55 pm
Location: Absurdistan, Hungary
Contact:

Re: function with multiple default arguments

Post by zorg »

davisdude wrote: Sat Aug 05, 2017 11:31 pm In this case, nil would be preferred over false because nil represents the absence of a variable, while false could be an argument.
Yes, but since they're using "or", that doesn't differentiate between them it only cares about whether it's truth-y or false-y.
Me and my stuff :3True Neutral Aspirant. Why, yes, i do indeed enjoy sarcastically correcting others when they make the most blatant of spelling mistakes. No bullying or trolling the innocent tho.
Post Reply

Who is online

Users browsing this forum: Amazon [Bot], Bing [Bot] and 6 guests