Default Values

You know from the exercise on parameters that a missing argument is nil. Often there's a value that fits most calls, and you only want to pass a different one now and then. Emma usually buys one bag of seeds, for example. For such cases, the parameter gets a default value. Lua has no special way of writing it, but you know everything you need from the lesson on conditions: x or default.

local function buySeeds(crop, bags)
    bags = bags or 1
    print("Seeds: " .. crop .. ", bags: " .. bags)
end

buySeeds("carrot", 3)
buySeeds("pumpkin")

This prints:

Seeds: carrot, bags: 3
Seeds: pumpkin, bags: 1

The first line of the body fills in the default value. When the call passes bags, bags or 1 is that number. When it doesn't, bags is nil, and nil or 1 is 1. A call can leave out arguments only at the end, so parameters with a default value go last.

Many other languages write the default value right in the parentheses: function buySeeds(crop, bags = 1). Lua doesn't have that, and such a line is a syntax error: ')' expected near '='.

There's one trap. or replaces not only nil, but also false. So when the default value is true, the caller can't pass false:

local function feed(animal, hungry)
    hungry = hungry or true
    if hungry then
        print("Feeding " .. animal)
    else
        print(animal .. " isn't hungry")
    end
end

feed("Bella")
feed("Rex", false)

This prints:

Feeding Bella
Feeding Rex

Rex isn't hungry, but false or true is true. It's the same trap as with a and b or c in the lesson on conditions. For a parameter that can be false, check for nil instead:

local function feed(animal, hungry)
    if hungry == nil then
        hungry = true
    end
    if hungry then
        print("Feeding " .. animal)
    else
        print(animal .. " isn't hungry")
    end
end

feed("Bella")
feed("Rex", false)

This prints:

Feeding Bella
Rex isn't hungry

Now only a missing argument (nil) gets the default value, and false stays false. For numbers and texts, x or default is all you need, because they're never false. Even 0 and "" stay as they are, since in Lua they count as true.

Instructions

The seed shop gives Emma a discount now and then. The program prints 15 and then ends with the error attempt to perform arithmetic on a nil value (local 'discount'), because the second call has no discount. Give discount the default value 0. The program then prints 15 and 12.

Most of the time, Emma buys a single bag. Give bags the default value 1. Then, at the end of the program, print seedCost(4), the price of one bag of strawberry seeds. The program prints:

15
12
4

The function waterNeeded returns how many buckets of water Emma needs for her plots: 2 per plot on a sunny day, 1 per plot otherwise. Most days are sunny, so sunny should default to true. At the end of the program, print waterNeeded(5), waterNeeded(5, true) and waterNeeded(5, false), each on its own line. The last one prints 10, even though the day isn't sunny. Fix the default value, so that the three lines are 10, 10 and 5.

Start programming for free

By signing up, you agree to the Terms of Service and Privacy Policy.

Or sign up with:

5/11