0Pricing
Blockchain Smart Contracts with Solidity · レッスン

関数と可視性修飾子

関数の定義方法、可視性(public、private、internal、external)の管理、関数の引数と戻り値の扱いを理解します。

「関数と可視性修飾子」はCoddyKit上の無料Blockchain Smart Contracts with Solidityレッスンです。 これはレッスン3/4です。 下記で完全なレッスンを無料で読むことができます。その後、ブラウザ内の組み込みコードエディタと24時間対応のAIチューターでハンズオン演習できます。 これはBlockchain Smart Contracts with Solidity学習パスの一部であり、ウェブとCoddyKitアプリ全体で進捗が同期されます。 Blockchain Smart Contracts with Solidityコースには全4レッスンが含まれています。

このレッスンの一部はまだ翻訳されておらず、英語で表示されています。

Functions: Your Contract's Actions

Welcome to Lesson 3! Today, we'll dive into functions in Solidity. Think of functions as the actions or operations your smart contract can perform.

They allow you to organize your code, make it reusable, and define how users (or other contracts) can interact with your contract's data and logic.

Defining a Simple Function

Every function needs a name and a body where its code lives. Here's the basic structure:

  • Keyword function
  • Function name (e.g., greet)
  • Parentheses () for parameters
  • Curly braces {} for the code block

Let's see a very simple example.

Basic Function Example

This contract has a function named getName. It doesn't take any inputs and simply returns a string. We'll add visibility soon!

Try running it to see how a function can return a value.

pragma solidity ^0.8.0;

contract MyFirstFunctions {
    function getName() pure returns (string memory) {
        return "Coddy";
    }
}

Controlling Access: Visibility

Solidity functions, like variables, have visibility modifiers. These define who can call a function and how.

Understanding visibility is crucial for security and proper contract design. There are four types: public, external, internal, and private.

Public Functions: Open for All

A public function is the most accessible. It can be called:

  • From outside the contract (by users or other contracts).
  • From inside the contract (by other functions within the same contract).

If you don't specify a visibility, functions are public by default. It's good practice to always specify it explicitly.

Public Function in Action

Here, setValue and getValue are both public. You can call them directly from an external account or another contract.

Run this example and try calling both functions after deploying.

pragma solidity ^0.8.0;

contract PublicExample {
    uint public myValue;

    function setValue(uint _newValue) public {
        myValue = _newValue;
    }

    function getValue() public view returns (uint) {
        return myValue;
    }
}

External Functions: External Calls Only

external functions can only be called from outside the contract. They cannot be called internally by other functions within the same contract.

This is often used for functions that are entry points for external interactions. It's also more gas-efficient for receiving large arrays of data.

External Function Example

Notice how onlyExternal can only be called from outside. If another function inside ExternalExample tried to call it, it would result in a compilation error.

Run this to see the external interaction.

pragma solidity ^0.8.0;

contract ExternalExample {
    uint public count = 0;

    function incrementExternal() external {
        count++;
    }

    // This function can't call incrementExternal() directly.
    // Try adding 'incrementExternal();' inside this function to see the error.
    function getCount() public view returns (uint) {
        return count;
    }
}

Internal & Private: For Internal Use

internal functions can only be called from within the current contract or by contracts that inherit from it.

private functions are the most restricted. They can only be called from within the contract they are defined in. Even inheriting contracts cannot call them.

These are great for helper functions that shouldn't be exposed externally.

Parameters & Return Values

Functions can accept parameters (inputs) and return values (outputs).

  • Parameters are defined inside the parentheses ().
  • Return types are specified using the returns (type) keyword.

This allows functions to be dynamic and process different data.

Function Visibility Check

Which of the following function visibility modifiers means a function can ONLY be called from outside the contract, but NOT from another function within the same contract?

Recap: Functions & Visibility

Great job! You've learned the fundamentals of Solidity functions.

  • Functions organize contract logic.
  • public: Callable from anywhere.
  • external: Callable only from outside.
  • internal: Callable from within & by derived contracts.
  • private: Callable only from within the defining contract.
  • Functions can take parameters and return values.

Next, we'll explore more complex data structures like mappings and dynamic arrays!

よくある質問

「関数と可視性修飾子」レッスンは無料ですか?

はい。「関数と可視性修飾子」の完全なテキストはこのウェブで無料で読めます。インタラクティブに演習し(組み込みコードエディタと24時間対応のAIチューター)、Blockchain Smart Contracts with Solidityコースの残りをアンロックするには、CoddyKit PROにアップグレードしてください。 Blockchain Smart Contracts with Solidityコースには全4レッスンが含まれています。

「関数と可視性修飾子」で何を学びますか?

関数の定義方法、可視性(public、private、internal、external)の管理、関数の引数と戻り値の扱いを理解します。 ブラウザで直接実行するハンズオンコードでBlockchain Smart Contracts with Solidityを演習し、24時間対応のAIチューターがレッスンを進める中での質問に答えます。

Blockchain Smart Contracts with Solidityを始めるのに経験は必要ですか?

事前経験は必要ありません。CoddyKitのBlockchain Smart Contracts with Solidityは初級者から上級者向けに構成されているため、ここから始めるか最初から始めて、自分のペースで進むことができます。 これはレッスン3/4です。

「関数と可視性修飾子」レッスンにはどのくらい時間がかかりますか?

ほとんどのCoddyKitレッスンは約5~10分かかります。各レッスンはコンパクトでインタラクティブなので、着実に進歩し、ウェブとアプリ全体で正確に前回の場所から再開できます。

このBlockchain Smart Contracts with Solidityレッスンでコードを書いて実行できますか?

はい。すべてのBlockchain Smart Contracts with Solidityレッスンに組み込みコードエディタが含まれているため、ブラウザでリアルコードを書いて実行し、即座のAIフィードバックを取得できます。ローカル設定は不要です。

このコースのすべてのレッスン

  1. Solidityのデータ型と変数
  2. 制御構文とループ
  3. 関数と可視性修飾子
  4. 構造体と列挙型
← Blockchain Smart Contracts with Solidityに戻る