Funktionen und Sichtbarkeitsmodifikatoren
Verstehen Sie, wie Funktionen definiert, ihre Sichtbarkeit (public, private, internal, external) verwaltet und Funktionsparameter sowie Rückgabewerte verarbeitet werden.
Funktionen und Sichtbarkeitsmodifikatoren ist eine kostenlose Blockchain Smart Contracts with Solidity-Lektion auf CoddyKit. Dies ist Lektion 3 von 4. Du kannst die komplette Lektion unten kostenlos lesen – dann übst du sie direkt im Browser mit einem integrierten Code-Editor und einem KI-Tutor rund um die Uhr. Sie ist Teil des Blockchain Smart Contracts with Solidity-Lernpfads, und dein Fortschritt wird über Web und CoddyKit-App synchronisiert. Der Blockchain Smart Contracts with Solidity-Kurs umfasst insgesamt 4 Lektionen.
Teile dieser Lektion wurden noch nicht übersetzt und werden auf Englisch angezeigt.
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!
Häufig gestellte Fragen
Ist die Lektion „Funktionen und Sichtbarkeitsmodifikatoren“ kostenlos?
Ja — der vollständige Text von „Funktionen und Sichtbarkeitsmodifikatoren“ ist hier im Web kostenlos zu lesen. Um sie interaktiv zu üben (integrierter Code-Editor und 24/7 KI-Tutor) und den Rest des Blockchain Smart Contracts with Solidity-Kurses freizuschalten, upgrade auf CoddyKit PRO. Der Blockchain Smart Contracts with Solidity-Kurs umfasst insgesamt 4 Lektionen.
Was lerne ich in „Funktionen und Sichtbarkeitsmodifikatoren“?
Verstehen Sie, wie Funktionen definiert, ihre Sichtbarkeit (public, private, internal, external) verwaltet und Funktionsparameter sowie Rückgabewerte verarbeitet werden. Du übst Blockchain Smart Contracts with Solidity mit praktischem Code, den du direkt im Browser ausführst, und ein 24/7 KI-Tutor beantwortet deine Fragen während du die Lektion bearbeitest.
Brauche ich Erfahrung, um Blockchain Smart Contracts with Solidity zu starten?
Keine Vorkenntnisse erforderlich. Blockchain Smart Contracts with Solidity auf CoddyKit ist für Anfänger bis fortgeschrittene Lernende strukturiert, sodass du hier starten oder von Anfang an beginnen und in deinem eigenen Tempo voranschreiten kannst. Dies ist Lektion 3 von 4.
Wie lange dauert die Lektion „Funktionen und Sichtbarkeitsmodifikatoren“?
Die meisten CoddyKit-Lektionen dauern etwa 5–10 Minuten. Jede ist kompakt und interaktiv, sodass du stetig Fortschritte machst und genau dort weitermachst, wo du aufgehört hast – im Web und in der App.
Kann ich in dieser Blockchain Smart Contracts with Solidity-Lektion Code schreiben und ausführen?
Ja. Jede Blockchain Smart Contracts with Solidity-Lektion enthält einen integrierten Code-Editor, sodass du echten Code direkt in deinem Browser schreibst und ausführst und sofort KI-Feedback erhältst — ohne lokale Einrichtung erforderlich.
Alle Lektionen in diesem Kurs
- Solidity-Datentypen und Variablen
- Kontrollstrukturen und Schleifen
- Funktionen und Sichtbarkeitsmodifikatoren
- Structs und Enums