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!
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?!
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.
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.
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.”);
}
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.
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.
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
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’);