How do you use variables correctly in function

https://www.codecademy.com/courses/introduction-to-javascript/lessons/functions/exercises/return

hello everyone, how do you correctly declare and use variables in function?

e.g

function rectangleArea(width, height) {

rectangleArea = width * height;

return rectangleArea;

}

let area = rectangleArea(5 * 7);

console.log(area);

First, your variable name should be with another name other than the function name. and you have to use let or const to declare it, you can also use var but it’s highly recommended not to use var.

second, keep in mind that your function waiting for two arguments from you but here let area = rectangleArea(5 * 7); you are passing just one because 5 * 7 will be computed to 35 as one argument, if you want to pass two arguments you will have to separate them with a comma , .

so your code will end with something like that:

function rectangleArea(width, height) {

    let area = width * height;

    return area;

}

let area1 = rectangleArea(5, 7);

console.log(area1); // print 35

=========================

Also please, there is a tool in Codecademy forums to refactor your code so it can be more readable, you can find it next to the upload tool or just use ctrl + shift + c as a shortcut. you can edit your post and use it.

Thanks for your reply. I see the correction in your code.
Firstly, you created a variable area and assigned it width * height.

Then , you use the return Keyword to return the variable .
Then you assigned the identifier to be equals to the variable.

All these above now make sense to me but what i dont understand in your post is about two arguments in the function and why you have to use area1 . The confusion is because i ran the code above using just area and it still print 35.

Also i tried to check for the refactor but i cant find it.

many thanks

1 Like

function rectangleArea(width, height) {
This line of code makes your function waiting for two arguments in the same order, width then height. so when you call it you have to pass two arguments to it.

rectangleArea(5, 7);

we are passing arguments when calling the function and separating them with a comma so now we called the function with 5 and 7.

Your function will take the first argument 5 and put it in width, and will take 7 and put it in height and will execute the function block with these two variables.

 {

    let area = width * height;

    return area;

}

here the function block will multiply 5 * 7 and put the result in area then return it.

==========================

about area1, there is no reason for this name you can choose whatever you want but keep in mind not to use the same name as the function.

every variable you declare inside the function block is related to the function scope and you can’t use it outside the function. that’s why you are able to declare another variable with the same name area outside the function.

Thanks for the detailed explanation. much appreciated

1 Like