FAQ: Functions - How Parameters & Arguments Work

This community-built FAQ covers the “How Parameters & Arguments Work” exercise from the lesson “Functions”.

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

Learn C++

FAQs on the exercise How Parameters & Arguments Work

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!

So in this slide, we learn how parameters and arguments generally work and as I’m reading the code, it returns sandwich at the end. My question here is why sandwich is returned instead of make_sandwich? If you were to use void, wouldn’t it be something like this:
void make_sandwich(std::string ing1, std::string ing2) {
etc code…
}
Here there is a void in front of the make_sandwhich but you do not need to use a return statement because it’s already voided. So why do we return sandwich in the string statement rather than make_sandwich?

This is the code I’m referring to:

Thank you for any help!

If I am misunderstanding your point of confusion, perhaps you can elaborate on your reasoning a bit more.

funcDef

make_sandwich is the name of the function.

std::string function_name(parameters) { ...
In the function definition, std::string before the function name specifies that this function will be returning a string.
Within the function body, the string could be returned directly e.g.

return "Hello!\nYay!!!";

or it could be assigned to some variable within the function before it is returned e.g.

std::string some_variable = "Hello!\nYay!!!";
return some_variable;

As long as the returned value is of the same type as specified in the function declaration (header), the return is valid. As long as some_variable is a syntactically valid name, we haven’t been constrained to use only a special name.

In main, we call the function make_sandwich and pass it two arguments. After the statements in the body of the function are executed and a string is returned, the returned string is then printed by std::cout

If we changed the function’s return type to void and deleted the return statement, then the function won’t return anything. We could add a std::cout statement inside the function so that even if the function isn’t returning anything, at least it is doing something meaningful by displaying a string on the screen. If return type is specified as void and the std::cout statement inside the function is omitted, then the function won’t do anything useful.

void make_sandwich(std::string ing1, std::string ing2) {
    std::string sandwich = "";
    sandwich += "bread\n";
    sandwich += ing1 + "\n";
    sandwich += ing2 + "\n";
    sandwich += "bread\n";
    std::cout << sandwich;
}

int main() {
    make_sandwich("peanut butter", "jelly");
}