Cover photo

②1: Get Message

Functions

関数

.

.

A function is re-usable code!

関数は再利用可能なコードです。

.

One important aspect of functions is the ability to return an output. Let's take a look at this closely.

関数の重要な側面の1つは、出力を返す能力です。これについて詳しく見てみましょう。

.

function getNumber() {
    return 4;
}

.

In this case, the number 4 is always returned when we call getNumber. Calling the function would look like this:

この場合、getNumberを呼び出すたびに常に数字4が返されます。関数を呼び出すには次のようになります。

.

const num = getNumber();

.

The value 4 would now be stored in our variable num!

これで、num変数に4が格納されます!

.

Explore the syntax of functions more in details.

関数の構文をもっと詳しく調べてみましょう。

.

.

Your Goal: Return a string from getMessage

目標:getMessageから文字列を返す

.

In the getMessage function return a string. It can be any string that you choose!

getMessage関数から文字列を返してください。好きな文字列でかまいません!

.

Once you have done this, click "Run Tests" or hit the enter key to move forward!

これを行ったら、「Run Tests」をクリックするか、Enterキーを押して進みましょう!

.

.

.

Function Syntax 関数の構文 . Let's break down function syntax a bit! 関数の構文を少し解説しましょう! . Calling a Function 関数の呼び出し . . You will see the term call used with functions. What does it mean? Call means to execute the function. Often when you call a function, you'll pass specific input values. 関数と一緒に使用される「call」という用語を見かけることがあるでしょう。それは何を意味しますか?Callは関数を実行することを意味します。関数を呼び出すときは、特定の入力値を渡すことがよくあります。 . You may also see the word invoke. These terms mean the same thing, you can call a function or you can invoke a function. 「invoke」という言葉も見かけるかもしれません。これらの用語は同じ意味です。関数を呼び出すことも、関数を実行することもできます。 . What does calling a function look like? どのように関数を呼び出すのでしょうか? . const output = addOne(2); . Here addOne is our function. We're using parenthesis after addOne to call the function. We are passing in an input value 2 into our function addOne. ここで、addOneは私たちの関数です。addOneの後に括弧を使用して関数を呼び出しています。関数addOneに入力値2を渡しています。 . Functions don't need to have input values, consider our getMessage function: 関数には入力値が必要なくてもかまいません。getMessage関数を考えてみましょう。 . const message = getMessage(); . In this case, we're just using parenthesis to call the function. We are not passing any input values to this function. この場合、関数を呼び出すために括弧を使用しています。この関数には入力値を渡していません。 . . Function Declaration 関数の定義 . In order to call a function, we must first define it! 関数を呼び出すにはまず定義する必要があります! . function addOne(input) { return input + 1; } . In this example we are creating a function called addOne which takes one input called input. We are returning the input plus one. この例では、一つの入力を取るaddOneという名前の関数を作成しています。入力に1を加えた値を返しています。 . function getMessage() { return "Hello World!"; } . This function getMessage does not take an input. We are simply returning a string, saying "Hello World!". このgetMessage関数は入力を取りません。単に文字列「Hello World!」を返しています。 . . Return Statement Returnステートメント . . The return keyword within a function will return the desired output of your function. This value will be returned to where the function is called. 関数内のreturnキーワードは、関数の望む出力を返します。この値は、関数が呼び出された場所に返されます。 . For example, if you have the following statement: 例えば、次のステートメントがある場合: . const a = addOne(1); . The variable a is now assigned to the return value of the addOne function invoked with an input of 1, which evaluates to 2. 変数aは、入力値1で呼び出されたaddOne関数の返り値に割り当てられ、評価結果は2になります。 . You may see the term declare a function. This is synonymous with defining a function. It means we are creating a function. Once it's been created, we'll be able to call it elsewhere in our program! 関数を宣言するという用語を見かけるかもしれません。これは関数を定義するという用語と同義です。それは、関数を作成していることを意味しています。作成された後は、プログラムのどこかで呼び出すことができるようになります! . . . NEXT⇒⇒ https://mirror.xyz/0x1E768F62Ab8678af5c977Ee880eC8012e1ce3c4F/XEYtpiFbuGKYQc_aaopr1GWx_eZW9nyZ2WqxHcmTjWM