Back to Course

Introduction to JavaScript

0% Complete
0/0 Steps
  1. JavaScript: Basics
    9Topics
    |
    10 Quizzes
  2. JavaScript: Variables
    9Topics
    |
    10 Quizzes
  3. JavaScript: Conditional Statements
    10Topics
    |
    11 Quizzes
  4. JavaScript: Functions
    10Topics
    |
    11 Quizzes
  5. JavaScript: Scope
    6Topics
    |
    7 Quizzes
  6. JavaScript: Arrays
    11Topics
    |
    12 Quizzes
  7. JavaScript: Loops
    8Topics
    |
    9 Quizzes
  8. JavaScript: High Order Functions
    3Topics
    |
    4 Quizzes
  9. JavaScript: Iterators
    3Topics
    |
    3 Quizzes
Lesson 4, Topic 4
In Progress

JavaScript: Parameters and Arguments

Yasin Cakal 5 Nov 2021
Lesson Progress
0% Complete

So far, the functions we’ve written have performed a task without receiving any input. Some functions, on the other hand, can take inputs and use them to perform a task. We can specify parameters for a function when declaring it. Parameters allow functions to accept one or more inputs and perform a task with them. Parameters serve as placeholders for data that will be passed to the function when it is called.

Let’s look at how parameters are specified in our function declaration:

function squarePerimeter(legnth){
  console.log( legnth*4);
}

squarePerimeter() computes the perimeter of square based on one input that is length.  The parameter is specified as length  between the parenthesis, and it behaves like a regular variable inside the function body. The dimension length serves as a placeholder for the value that will be multiplied by 4.

When we call a function with 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. Values or variables can be passed to the function as arguments.

function squarePerimeter(legnth){
  console.log( legnth*4);
}

squarePerimeter(10);

//Output: 40

In the function call (line 5) above, the number 10 is passed as the length. The function now will take it and compute the answer by multiplying it by 4. You can compute the perimeter of as many squares as you want without having to write the function code block again and again.

Now as we have learned about how we set parameters and pass arguments to the function. Let’s take an example of a function with more than one parameter.

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

calculateVolume(5,11,6);

//Output:  330

Notice that the order in which arguments are passed and assigned is the same as the order in which the parameters are declared. In the above example the assignment of arguments to the parameters is as length = 5 , width=11 and height=6.

calculateVolume() can be reused to compute the volume of any rectangle by passing different values for arguments!

Do practice creating and calling functions with parameters because functions are a powerful tool in computer programming.

Responses

Leave a Reply

%d bloggers like this: