-
Parameters and ArgumentsStudy/JavaScript 2020. 4. 29. 20:53
Parameters and Arguments
So far, the functions we’ve created execute a task without an input. However, some functions can take inputs and use the inputs to perform a task. When declaring a function, we can specify its parameters. Parameters allow functions to accept input(s) and perform a task using the input(s). We use parameters as placeholders for information that will be passed to the function when it is called.
Let’s observe how to specify parameters in our function declaration:
In the diagram above, calculateArea(), computes the area of a rectangle, based on two inputs, width and height. The parameters are specified between the parenthesis as width and height, and inside the function body, they act just like regular variables. width and height act as placeholders for values that will be multiplied together.
When calling a function that has parameters, we specify the values in the parentheses that follow the function name. The values that are passed to the function when it is called are called arguments. Arguments can be passed to the function as values or variables.
In the function call above, the number 10 is passed as the width and 6 is passed as height. Notice that the order in which arguments are passed and assigned follows the order that the parameters are declared.
The variables rectWidth and rectHeight are initialized with the values for the height and width of a rectangle before being used in the function call.
By using parameters, calculateArea() can be reused to compute the area of any rectangle! Functions are a powerful tool in computer programming so let’s practice creating and calling functions with parameters.
'Study > JavaScript' 카테고리의 다른 글
What are the advantages of function expressions? 함수표현식 vs 함수 선언식 (0) 2020.04.30 Why do I need to use `return`? (0) 2020.04.30 Hoisting (0) 2020.04.29 js 로 Magic Eight Ball 만들기 (0) 2020.04.29 Control flow (0) 2020.04.29