FAQ: Functions - Parameters and Arguments

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

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

Web Development

Introduction To JavaScript

FAQs on the exercise Parameters and Arguments

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
in the lesson it says that when we use a parameter, it can be used as a variable in the function body
but when I was practicing I find out that even without a parameter I can use a variable from outside of the function body! look at this

`const name = ‘Cole’;

function sayThanks()
{
console.log('Thank you for your purchase '+ name + ‘! We appreciate your business.’);
}

sayThanks();`

so when we can use a variable in the function body without using parameter, what’s the purpose of them?!

1 Like

A function should ideally be a freestanding and independent block of code that works on/with values we pass in as arguments and return a result or outcome to the caller. The caller supplies the arguments, the function takes them as parameters, makes use of them in some form of evaluation and passes back the finished value.

Variables in global scope may be called upon by the function, so long as there are no changes made to the variable. There are lots of things we can do, but some we should avoid for the sake of our program’s overall reliability.

Consider,

let a = 6, b = 7
function add_a_b() {
    return a + b;
}
console.log(add_a_b());    // 13

Above, if we wish to add two numbers in our function and return their sum, a and b MUST EXIST somewhere in global scope. The function cannot work without those variables present. That is dependence, and something that can cause problems.

function add_a_b(a, b) {
    return a + b;
}
console.log(add_a_b(6, 7));    // 13
console.log(add_a_b(20, 22));    // 42

Above, a and b are local variables that are independent of the program, and thus our function is independent. It takes two arguments and returns a sum, without regard what the two arguments are. The outcome will always be based upon what is given in the parameters.

12 Likes

The colors of the letters in the graphics make it REALLY hard to see. It’d be great if those colors were a few shades darker.

3 Likes

May we assume you mean this one?

functions_graphic

I think this is something we can both agree upon… The contrast does not meet the threshold of accessibility spelled out in WCAG, etc.

Perhaps @oduffy can look at this and refer it to the CXP team for further review?

2 Likes

Yes! The contrast is not strong enough, making it harder for the user to make out what’s actually written while digesting new content. It looks pretty color-wise, but it’s not user-friendly.

2 Likes

I need some explanations if possible. While I was practicing it kept giving me the error message when I tried to use the name Cole with out the ’ ’ in the parameters and function lesson after I tried to call the function.

Can someone explain why I need the ’ ’ in the () of a function call? It didnt go into detail why this is the case.

Exp:
function sayThanks(name){
console.log("Thank you for your purchase "+ name + “! We appreciate your business.”);
}

sayThanks(‘Cole’);

5 Likes

without the apostrophes, Cole would be a undefined variable, with apostrophes its a string.

7 Likes

Thank you! Im still learning and trying to wrap my head around everything.

5 Likes

Hi! I have a doubt.

In this function:

function sayThanks(name) {
console.log('Thank you for your purchase '+ name + ‘! We appreciate your business.’);

Since the parameter name is a variable, why do I use string concatenation instead of interpolation?

6 Likes

It’s just a matter of preference. Feel free to use interpolation (in some exercises this might lead to certain problems because of strict tests).

3 Likes

Glad you asked this, my first thoughts where exactly this

So, at least in JavaScript, I don’t have to declare or assign anything to the variable before using it all willy-nilly as an argument/parameter?

1 Like

These are all declarations…

var a;
let b;
const c;

If we log them they will all turn up, undefined.

Since JS is loosely typed, there is no restriction whatever we wish to define them as. Only once we assign a value or object to the variables are they defined.

Often we may wish to keep the information about a variable available in a higher scope, such as outside of a function or a loop. Declaring them in that scope permits us to define them again and again at various points throughout the program.

2 Likes

It’s just that the lesson was doing something like this:

function multiplyNumbers (numberOne, numberTwo)
{
  answer = numberOne * numberTwo;
  return answer;
}

multiplyNumbers(3, 6);

… and that seemed fine, even though there are no vars or consts or lets to be seen.

The problem with defining variables without a declaration keyword is that the variable pollutes global scope since that is where it will be declared.

After the line multiplyNumbers(3, 6) try logging answer.

console.log(answer)    //  18

When variables are declared in function or block scope they cannot leak into a higher scope. This is important for any number of reasons which your reading will reveal.

Technically your function above needs no variable since the expression can be returned directly…

return numberOne * numberTwo;

Then assign it to a variable in caller scope…

let answer = multiplyNumbers(3, 6);
console.log(answer)    //  18

I’m very familiar with function scope from my introductory studies with C# using the Unity Engine. This is why I was confused by the Codecademy lesson not declaring variables in ANY given scope, (local OR global), before using them.

Leaving the matter of variable scope entirely behind us, it all just seemed odd to me.

How can we pass parameter to function without declaring them?

1 Like

parameters are defined along with the function. Then the parameters act as placeholders until the function is called. Remember, functions are only execute when called, so parameters don’t need a value until the function is actually called

2 Likes

Hello!
Related to this, in the piece of code below, there is no declaration of the variable name and it works anyways. Is it not necessary to declare the variable the follwoing way outside or inside the function?: const name;

The function of the exercices is this one:

function sayThanks(name) {
console.log('Thank you for your purchase '+ name + ‘! We appreciate your business.’);
}
sayThanks (‘Cole’);

1 Like