0Pricing
Blockchain Smart Contracts with Solidity · 강의

함수와 가시성 지정자

함수 정의, 가시성(public, private, internal, external) 관리, 함수 매개변수와 반환값 처리를 이해합니다.

함수와 가시성 지정자은(는) CoddyKit의 무료 Blockchain Smart Contracts with Solidity 강의입니다. 이것은 4개 중 3번째 강의입니다. 아래에서 전체 강의를 무료로 읽을 수 있으며, 내장 코드 에디터와 24/7 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/7 AI 튜터), CoddyKit PRO로 업그레이드하면 Blockchain Smart Contracts with Solidity 강의 전체를 잠금 해제할 수 있습니다. Blockchain Smart Contracts with Solidity 강의에는 총 4개의 강의가 포함되어 있습니다.

“함수와 가시성 지정자”에서 뭘 배우나요?

함수 정의, 가시성(public, private, internal, external) 관리, 함수 매개변수와 반환값 처리를 이해합니다. 브라우저에서 직접 실행하는 실습 코드로 Blockchain Smart Contracts with Solidity을(를) 배우며, 24/7 AI 튜터가 강의를 진행하면서 질문에 답변해줍니다.

Blockchain Smart Contracts with Solidity을(를) 시작하는 데 경험이 필요한가요?

사전 경험은 필요하지 않습니다. CoddyKit의 Blockchain Smart Contracts with Solidity은(는) 초급자부터 고급 학습자까지를 위해 구성되어 있으므로, 여기서 시작하거나 처음부터 시작할 수 있으며 자신의 속도대로 진행할 수 있습니다. 이것은 4개 중 3번째 강의입니다.

“함수와 가시성 지정자” 강의는 얼마나 걸리나요?

대부분의 CoddyKit 강의는 약 5~10분이 소요됩니다. 각 강의는 간결하고 인터랙티브하여 꾸준한 진행이 가능하며, 웹과 앱에서 중단한 부분부터 바로 시작할 수 있습니다.

이 Blockchain Smart Contracts with Solidity 강의에서 코드를 작성하고 실행할 수 있나요?

네. 모든 Blockchain Smart Contracts with Solidity 강의에는 내장 코드 에디터가 포함되어 있으므로, 브라우저에서 바로 실제 코드를 작성하고 실행한 후 즉시 AI 피드백을 받을 수 있습니다 — 로컬 설정이 필요 없습니다.

이 강의의 모든 강의

  1. Solidity 데이터 형식과 변수
  2. 제어 구조와 반복문
  3. 함수와 가시성 지정자
  4. 구조체와 열거형
← Blockchain Smart Contracts with Solidity(으)로 돌아가기