FAQ: Functions - Functions for Flexibility

This community-built FAQ covers the “Functions for Flexibility” exercise from the lesson “Functions”.

Paths and Courses
This exercise can be found in the following Codecademy content:

Learn How To Code

FAQs on the exercise Functions for Flexibility

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 (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 (reply) below!

Agree with a comment or answer? Like (like) to up-vote the contribution!

Need broader help or resources? Head here.

Looking for motivation to keep learning? Join our wider discussions.

Learn more about how to use this guide.

Found a bug? Report it!

Have a question about your account or billing? Reach out to our customer support team!

None of the above? Find out where to ask other questions here!

Hi!

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.

Is that correct?

Thanks in advance!

1 Like

First we write the parameters as variables, not values…

function makeSandwich(ingredient1, ingredient2) {

and then pass the actual values as the call arguments…

makeSandwich('ham', 'cheese')

Our function will assemble these ingredients between two slices of bread, and return the final product.

sandwich = 'bread' + ', ';
sandwich += ingredient1 + ', ';
sandwich += ingredient2 + ', ';
sandwich += 'bread';
return sandwich;

The return value will be a string that looks like this…

'bread, ham, cheese, bread'
1 Like

Hi! Thanks for the reply.
I have another doubt now. If I write it like this, i.e. with no arguments:

function makeSandwich() {
sandwich = ‘bread’ + ‘,’
sandwich += ‘bread’
return sandwich
}

Why do I still have to write this part:

result = makeSandwich();

Even if the function has no arguments?
Can’t I just call the function like this:

makeSandwich()

1 Like

You could, but then the return value will vanish. By assigning it to a receiving variable we retain the value for further use, such as printing.

whats the difference between variables and functions

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.

variable_name = 'assigned value'
1 Like

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?

One supposes so, yes. return takes a bit of explanation that would be more than needed for this exercise.

Why do we list add bread twice when defining the function for flexibility but we didn’t list it twice when writing the function for reusability?

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.

Welcome to the forums!

So when the function is defined, it should list add bread twice, but when it is called, it will only display add bread one time?

Add bread
Add burger patty
Add pickles

should result in only one bread being added.

Add bread
Add burger patty
Add pickles
Add bread

should result in two bread being added.


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.

Thank you for clarifying!

1 Like

@mtf

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…

    return `bread, ${ingredient1}, ${ingredient2}, bread`;
1 Like

ingredient1 and 2 are also strings right doesn’t they require apostrophe’s around them

They are variables that point to string values, so no quotes.