Unit 4 · Thinking like a programmer
Why "name" in quotes prints the word name
Quotes make text. "city" is the four letters c-i-t-y, while city without quotes reads the box with that name. Leave the quotes off to use the value.
The mix-up
What people often think
“name inside the function is the word "name".”
What the tutor says
name has no quotes, so it is a box. It holds whatever value the caller passes in.
How it works
Inside a function, a parameter such as first is a box holding whatever the caller passed. "first" in quotes is just the word first, so "first" + " " + "last" always gives “first last”.
If your output shows a variable’s name instead of its value, look for quotes around it.
Side by side
function fullName(first, last) {
return "first" + " " + "last"; // "first last"
}function fullName(first, last) {
return first + " " + last; // "Ada Lovelace"
}Close cousins of this mix-up
The tutor watches for these in the same lesson.
“Putting first and last in quotes uses what was passed in.”
"first" in quotes is just the word first. Leave off the quotes to use the value the caller passed.
“Putting the box name in quotes shows what is inside it.”
Quotes make it plain text, so "city" prints the word city. Leave the quotes off to read the box.
Practise it with a tutor watching
Functions: machines with inputs and outputs
Define a function, call it with inputs, and return a value the rest of the code can use.
Free, no account needed.