Conteúdo do curso
JavaScript Course for Beginners: Fundamentals and Practice
Sobre a Aula

Parameters and Function Arguments

Welcome to the second lesson of Module 2! In this stage, you will explore the power of parameters and function arguments in JavaScript. These are tools that allow you to customize the behavior of your functions and provide specific data for each call.

Function Parameters

Parameters are like placeholders in a function where we can receive input data. They function as variables that store the values passed to the function.

Analogously, imagine a milkshake machine with an empty container. This container represents the function’s parameter, where you can put different flavors of ice cream.

Function Arguments

Arguments are the actual values passed to the function’s parameters when it is called. They provide the necessary data for the function to perform its tasks.

Analogously, let’s imagine that you added a scoop of strawberry ice cream and a scoop of chocolate ice cream to the milkshake machine’s container.

These ice cream scoops represent the function’s arguments, which are the actual values that will be used in the function’s calculations or processing.

Let’s see a practical example. Suppose we have a function called “sum” that takes two parameters: “a” and “b”. When calling the function with the arguments 3 and 5, the sum of these values will be returned.

Example:

function sum(a, b) {
  return a + b;
}

const result = sum(3, 5);
console.log(result); // Output: 8

Challenge:

  1. Create a JavaScript script that prompts the user for two numbers.
  2. Create a function that receives these numbers as parameters and calculates their sum, difference, product, and quotient.
  3. Invoke the function and display the results on the screen.

Now that you understand the importance of function parameters and arguments, let’s move on to the next lesson: Variable Scope.

Entrar na conversa
Rolar para cima