FAQ: Functions - Parameters and Arguments

That is literally what i cover in my answer? parameters (like name) are defined when you define the function, they have a local scope and only exist in the body of the function.

I have a question: what is the difference between parameter and argument? is parameter serve as variables for argument (which is a true value)? thank you for help.

1 Like
function foo (param) {
    // param is a local variable
}

console.log(foo(argument)

Above, argument could be a variable referring to an object in caller scope, or it could be a literal, such as a number, string, boolean, array, object, or a function.

Inside the function, that argument will be referenced by the variable, param, or whatever name is chosen as the parameter.

parameter  =>  local variable

argument   =>  caller supplied object or reference
1 Like

It says in the lesson:

Arguments can be passed to the function as values or variables.

Then there are two diagrams given (attached). In my understanding the second should say “arguments as variables” not as values?

img2

1 Like

It could be argued they are both, variable and value, but you are correct. The values are passed by reference. On the receiving end it will be the values that are seen, not the variables.

1 Like

I kept getting an error message when my answer to Instruction 3 was

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

When I finally gave up and looked at the answer, it looks like the only difference between my efforts and the solution was where the sayThanks('Cole"); line of code was placed in the block.
The solution placed it below the console.log line.
Why wouldn’t my solution work?

Should we ask a function to run from within, or without? This is a weird question because either answer would be correct, in a sense, with given provisos. However, this instance resolves to just one state of the question, ‘without’. We call the function from the outside.

Notice that the order in which arguments are passed and assigned follows the order that the parameters are declared.

Why? As far as I can tell, this serves no critical purpose, only excessive organization.

Hi @markholley, I see what you’re seeing. The parameters are never explicitly declared and initialized inside the function body with let, const or var. When invoked, the supplied arguments are mapped to the parameter identifiers in the function’s environmentRecord.

i’m still learning too

here’s my code, getting used to these variables

function sayThanks(name) {

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

}

//sayThanks(name)//

sayThanks(6)

console says Thank you for your purchase 6! We appreciate your business.

and is your question?

Pegando os esclarecimentos dessa pergunta, posso tirar conclusão de que se uma variável no escopo global podem ser chamadas pela funcao desde que nao haja mudancas eu devo usar sempre const para evitar que aconteca? ou é relativo depende para quais fins?

Functions and constants are declared with const to protect their integrity. We declare them this way by choice, not by obligation. We are the designers. There is no should in design, only lots of considerations.

can i add number as a paramter in the function.
like function hhh(number){
console.log(testing)
}
hhh(4)
and when i do this they it shows that the function is wrong.

What does the exercise ask for? It says to add a parameter to the function definition…

function sayThanks(name) {

}

Does it matter that we use a different variable? No. We name the parameter arbitrarily, but hopefully it will be suitable to describe what value it represents. Would we use the name, number when we are expecting a NAME? Wouldn’t make a lot of sense to the reader, now, would it?

Bear in mind also that when a parameter is present, that will mean our function is going to refer to it at least once. The function above will do something with the name variable.

console.log(`Thanks, ${name}.`)

Be sure that whatever variable you use is matched in the function body. Your code uses number in the parameter and testing in the statement. Where did that variable all of a sudden appear from? We cannot just pull things out of a hat.

1 Like

Good day, pls in the below;

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

sayThanks(‘Cole’)

how was it possible the string ‘Cole’ replaced name without defining name?

parameters (like name) get there value from the argument at function call:

sayThanks('Cole')

this means if we want to say thaks to someone else, we merely have to call the function again, but we can use a different name:

sayThanks('jjdarkies')

eliminating repetitive code.

I have a question: why isn’t it possible to call one function inside another?
I was playing around on this exercise (FUNCTIONS Parameters and Arguments) and the following code did not work as I was expecting it to. Could someone be so kind as to help me with this?

MY CODE

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

sayThanks(Cole)     // Thank you for your purchase Cole! We appreciate your business.

function calculateArea(width, height) {
  console.log(width * height);
}

calculateArea(10, 10)     // 100

sayThanks(calculateArea(10, 10))    // Thank you for your purchase undefined! We appreciate your business.
//code ends here

I was expecting the name to appear as 100 (‘Thank you for your purchase 100’). Why does it show as undefined?
Thank you very much in advance! This is my first post, but I have been learning a lot from everyone here by reading other’s questions and answers.

here:

sayThanks(Cole)   

shouldn’t 'Cole' be a string?

calculateArea function does not return anything, so we get undefined, we can see this:

// attempting to log returned result
console.log(calculateArea(10, 10))

.log() merely logs something to the console, we need to use return to pass data between functions

2 Likes

@stetim94 ,
Thank you for your help!
You are correct; ‘Cole’ was a string in my original code. I pasted the wrong version here. Thanks.

Regarding the calculateArea function, I understand it a little bit better now. I changed the code and now it is working! I can see that if we in fact use return to “bring the data to life” we can pass data between different functions. Thank you so much for your help :slight_smile:

CODE:

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

function calculateArea(width, height) {
return width * height
}

console.log(sayThanks(calculateArea(10, 10))) // Thank you for your purchase 100! We appreciate your business.

3 Likes