Parameters and Arguments

The function morningChores does exactly the same thing every time. But often you want a function to do almost the same thing, only with a different value each time: water the carrots, then the potatoes. A function gets such values through parameters:

local function water(crop)
    print("Watering the " .. crop)
end

water("carrots")
water("potatoes")

This prints:

Watering the carrots
Watering the potatoes

In the definition, crop in the parentheses is a parameter: a local variable of the function that it can use in its body. In the call, the value in the parentheses, "carrots", is an argument. When you call the function, Lua stores the argument in the parameter and then runs the body. On the first call, crop is "carrots"; on the second, it's "potatoes".

The two words are easy to mix up, so remember: a parameter is a name in the definition, an argument is a value in the call. An argument can also be a variable or a calculation, such as water(crops[1]). You've been passing arguments since the first lesson: "Hello" in print("Hello") is one.

A function can have several parameters, separated by commas. The arguments go into them in order: the first argument into the first parameter, the second into the second, and so on:

local function plant(crop, count)
    print("Planting " .. count .. " " .. crop)
end

plant("carrots", 12)
plant(12, "carrots")

This prints:

Planting 12 carrots
Planting carrots 12

The second call has its arguments swapped, so 12 went into crop and "carrots" into count. Lua doesn't know what the values mean, only their order.

Many languages check that each call passes exactly as many arguments as the function has parameters. Lua doesn't. A missing argument is simply nil, and extra ones are thrown away:

local function plant(crop, count)
    print(crop, count)
end

plant("wheat")
plant("corn", 5, "extra")

This prints:

wheat	nil
corn	5

That can be handy, as you'll see in the exercise on default values. But a forgotten argument isn't an error in the call. The error shows up later, inside the function, where it uses the nil:

local function plant(crop, count)
    print("Planting " .. count .. " " .. crop)
end

plant("wheat")

The program ends with an error:

main.lua:2: attempt to concatenate a nil value (local 'count')
stack traceback:
	main.lua:2: in local 'plant'
	main.lua:5: in main chunk

The error is on line 2, in the function, but the mistake is in the call on line 5. The lines below stack traceback: show how the program got there. Read them from the bottom: the main program (main chunk) called plant on line 5, and line 2 of plant failed. When an error message names a parameter, such as local 'count', check the calls of the function: does each one pass all the arguments, in the right order?

Instructions

Emma plants her crops in rows, and you'll draw them with letters. Write a local function drawRow with two parameters, plant and count. It prints the text plant on one line, count times: in a for loop, print it with io.write, and after the loop, end the line with print().

Below the function, call drawRow("C", 5) for a row of five carrots and drawRow("P", 3) for a row of three potatoes. The program prints:

CCCCC
PPP

An argument can also be a variable. Emma's strawberry bed is a triangle: each row has one plant more than the row before. Below the two calls, write a for loop in which i goes from 1 to 4 and which calls drawRow("S", i). In each iteration of the loop, the function gets a different argument, so the program draws:

S
SS
SSS
SSSS

Start programming for free

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

Or sign up with:

2/11