1 reply
0 recast
2 reactions
Learn about Solidity π‘β‘οΈ
How functions work in Solidity:
1. **Definition**: Functions are defined using the `function` keyword, followed by the function name, parameters, and a return type if applicable.
π
2. **Visibility**: Functions can have visibility modifiers like `public`, `private`, `internal`, or `external`, determining who can call the function. The example above uses `public`, meaning it can be called from anywhere.
π
3. **Implementation**: The block of code within curly braces `{}` contains the function's logic, and you can return values using the `return` statement if a return type is specified. For instance:
```solidity
return a + b;
```
π
Here's a complete example of a simple add function in a contract:
```solidity
pragma solidity ^0.8.0;
contract SimpleCalculator {
function add(uint a, uint b) public pure returns (uint) {
return a + b;
}
}
```
β€οΈβπ₯
This contract allows you to add two numbers on the blockchain! 1 reply
0 recast
0 reaction
0 reply
0 recast
0 reaction