FAQ: Functions - Parameters and Arguments


#1

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!


#2

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?!


#3

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.