return greetings
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;
contract SchoolGreetings {
address public principal;
event GreetingReceived(string greeting);
constructor(address _principal) {
principal = _principal;
}
// 向校長打招呼並獲取回應
function greetPrincipal() public returns (string memory) {
// 假設校長地址不為空
require(principal != address(0), "Principal address is not set");
// 模擬向校長打招呼的過程
emit GreetingReceived("Hello Principal!");
// 返回校長的回應
return "Hello, how can I help you?";
}
}
SchoolGreetings
,其中有一個函式 greetPrincipal
用於向校長打招呼,並獲取他的回應。return
返回一個包含校長回應的字符串。return
關鍵字時,我們可以想像成是和校長打招呼後,得到了一個回應。在 Solidity 中,return
用於從函式中返回數據給調用者。