Clicky

[JavaScript] What is a dummy argument?

JavaScript
JavaScript
This article can be read in about 7 minutes.

※記事中に広告情報を含みます。

スキルを手に入れた時、人は強くなれる。
Youtubeでスキルアップを始める 電子書籍でスキルアップを始める
\ワードプレスのスキルアップはこちら!/ WordPress入門読本

What are formal arguments?

Formal arguments are variables that receive values ​​passed to functions and methods in programming. Formal arguments are used within functions and are associated with actual arguments.

This article explains the basic concept of formal arguments and how to use them in detail.

The basic role of formal arguments

Formal arguments define variables that are used within a function when it is called.

When actual arguments are passed to a function, their values ​​are assigned to the formal arguments and used to process the internals function.

Formal argument example

Below is an example of a basic function using formal arguments in JavaScript.

Create a simple function that adds two numbers.

function add(x, y) {
return x + y; // Adds x and y and returns the result
}

var result = add(3, 5);
console.log(result); // Output: 8

In this example, x and y are formal arguments, and actual arguments (3 and 5) are assigned to them when they are called.

For example, imagine a recipe for baking sweets.

A recipe has a column for “ingredients,” for example, like this:

Flour _ grams

Sugar _ grams

These blanks (_) are what we call “formal arguments” in programming.

function makeSweets(flour, sugar) { // <- These flour and sugar are formal arguments
return flour + sugar;

The blanks in the recipe are where you enter specific amounts when you actually make the sweets.

For example:

Flour 200 grams

Sugar 100 grams

In the same way, you enter specific values ​​when you actually use a function.

makeSweets(200, 100); // <- These 200 and 100 are the actual values ​​(actual arguments).

In other words,

The blanks (formal arguments) in the recipe are placeholders (places to put specific amounts later).

When you actually cook (when you call the function), you put specific numerical values ​​(actual arguments) in those blanks.

If you think of formal arguments in this way, you can understand them as “boxes that will contain specific values ​​later.”

About multiple formal arguments

Functions can have multiple formal arguments and can handle any number of arguments.

In the following example, a function is created that receives a name and a greeting.

function greet(name, greeting) {
return greeting + ', ' + name + '!';
}

console.log(greet('山田', 'こんにちは')); // 出力: こんにちは, 山田!

Using default arguments

In JavaScript, it is also possible to set default values ​​for formal arguments.

This ensures that the function will work properly even if no arguments are passed.

function multiply(x, y = 1) {
return x * y;
}

console.log(multiply(5)); // 出力: 5 (5 * 1)
console.log(multiply(5, 2)); // 出力: 10 (5 * 2)

Error handling and formal arguments

Validating the values ​​passed to a function is important. Ensuring that they are of the correct data type and that invalid values ​​such as null or undefined are not passed is key to making sure the function works correctly.

function safeAdd(x, y) {
if (typeof x !== 'number' || typeof y !== 'number') {
throw new Error('Both arguments must be numeric.');
}
return x + y;
}

try {
console.log(safeAdd(3, 5)); // Output: 8
console.log(safeAdd('3', 5)); // Throws an error
} catch (error) {
console.error(error.message);
}

Summary

Formal parameters are a key element in allowing functions to flexibly handle a variety of input amounts. Understanding formal parameters is the first step to a deeper understanding of function design and operation.

In this article, we have covered the basic concept of formal parameters, use cases, the benefits of multiple formal parameters, default parameters, and the importance of error handling. Let’s use formal parameters in programming to create more efficient functions.