函数与可见性修饰符
了解如何定义函数、管理其可见性(public、private、internal、external),以及处理函数参数和返回值。
函数与可见性修饰符 是 CoddyKit 上的免费 Blockchain Smart Contracts with Solidity 课时。 这是第 3 节课,共 4 节。 你可以在下方免费阅读本课时的完整内容 — 然后在浏览器中使用内置代码编辑器和全天候 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!
常见问题解答
「函数与可见性修饰符」课时是免费的吗?
是的 — 「函数与可见性修饰符」的完整文本可在网页上免费阅读。要进行交互式练习(内置代码编辑器和全天候 AI 导师)并解锁 Blockchain Smart Contracts with Solidity 课程的其余内容,请升级到 CoddyKit PRO。 Blockchain Smart Contracts with Solidity 课程共包含 4 节课。
「函数与可见性修饰符」这节课中我会学到什么?
了解如何定义函数、管理其可见性(public、private、internal、external),以及处理函数参数和返回值。 你通过在浏览器中直接运行的动手代码来练习 Blockchain Smart Contracts with Solidity,全天候 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 反馈 — 无需本地设置。
此课程中的所有课时
- Solidity 数据类型与变量
- 控制结构与循环
- 函数与可见性修饰符
- 结构体与枚举