There are currently no frequently asked questions associated with this exercise – that’s where you come in! You can contribute to this section by offering your own questions, answers, or clarifications on this exercise. Ask or answer a question by clicking reply () below.
If you’ve had an “aha” moment about the concepts, formatting, syntax, or anything else with this exercise, consider sharing those insights! Teaching others and answering their questions is one of the best ways to learn and stay sharp.
Join the Discussion. Help a fellow learner on their journey.
Ask or answer a question about this exercise by clicking reply () below!
Agree with a comment or answer? Like () to up-vote the contribution!
I’m not sure I understand how to call a function with two arguments in case I only want to use one.
For example here:
function makeSandwich(“ham”, “cheese”) {
Add bread
Add ham
Add cheese
Add bread
}
Suppose I only want to use ham. How do I call the function?
Would it be something like this:
makeSandwich(“ham”) ?
Whereas if I want to use both, I’ll just do makeSandwich() which executes the function entirely.
Variables are just names we give to references. They’re like a mailing label. We have an object, but to access that object we need to give it a label so we can refer to it later.
Functions are blocks of code. Since their reference is assignable we can write an expression and assign it to a variable. That is the name the function will be known by.
We can assign any object or expression to a variable. It doesn’t hold the data, it merely points to where it is stored. The language looks after the details. All we need is to know the name to reference it.
How come the lesson example didn’t define the function like this with a return at the end? Is this just a peak under the hood that the lesson didn’t explain?
You are correct that there is a discrepancy between the two. I believe that the Functions For Reusability Exercise should have included Add bread twice. You could create a post under the Bug Reporting category to bring this to Codecademy’s attention.
It is just a minor discrepancy between the two exercises, I wouldn’t look too much into it. Instead, focus on the main point of the exercise, which is how functions can make code more reusable and achieve greater flexibility.
hai can you please explain those symbols meanings like why did you write += in each line. its meaning
and in each line end you wrote + ', '; its meaning
i just dont understand those symbols and placing them like that and in the end return sandwich its meaning
thanks
+= is the augmentation operator for concatenation of strings (as well as addition of numbers). We are simply extending the existing string (sandwich) by adding an ingredient followed by a comma and space (', '). At the end we top it off with bread.
We could have written it otherwise as one long concatenation expression but instead we spread it over several lines.
There is also a new form of string expression in ES6, template literal which we would write like this…
I wanted to ask
We have to declare ingredient1 and ingredient2 as ham and cheese, as peanut butter and jelly , as patty and pickle everytime we have to make a thing , right?