This section focuses on understanding the import functionality in Solidity, a crucial aspect for building Solana programs.
illustration
b
Solidity Functions Explained

In this lesson, you'll:

  • Learn how to declare functions within and outside Solidity contracts.
  • Understand the different visibility types for functions.
  • Grasp the concepts of state mutability and function overloading.

Solidity allows function creation both within and outside contracts. Functions can have different visibility types (public, private, internal, external), and can be marked as view, pure, payable, or non-payable, indicating how they interact with the blockchain. Function overloading is supported, allowing functions with the same name but different parameters.

Function Declaration:

Functions in Solidity can be declared inside contracts (accessing contract storage and other functions) or outside contracts. They can have any number of arguments, which can be named or unnamed. Named return values can be used, and if all return values are named, an explicit return statement isn't necessary.

Example:

function get_initial_bound() returns (uint256 value) {
 value = 102;
}

contract foo {
 uint256 bound = get_initial_bound();

 function set_bound(uint256 _bound) public {
 bound = _bound;
 }

 function get_with_bound(uint256 value) public view returns (uint256) {
 return (value < bound) ? value : bound;
 }
}

Function Visibility:

Functions in Solidity have visibility specifiers:

  • Public: Accessible inside and outside the contract.
  • Private: Only accessible within the contract.
  • Internal: Accessible within the contract and any inheriting contract.
  • External: Only accessible by other contracts or directly by RPC.

Functions defined outside a contract can't have visibility specifiers.

Arguments Passing and Return Values:

Function arguments can be passed by position or by name. For named arguments, the order doesn't matter. Functions can return multiple values, assignable using destructuring statements.

Example:

contract foo {
 function bar1(uint32 x, bool y) public returns (address, bytes32) {
 return (address(3), hex"01020304");
 }

 function bar2(uint32 x, bool y) public returns (bool) {
 return !y;
 }

 function test() public {
 (address f1, bytes32 f2) = bar1(102, false);
 bool f3 = bar2(255, true);
 }
}

State Mutability:

  • View and Pure Functions: These do not modify contract state. view functions can read data, while pure functions cannot even read data.
  • Payable and Non-payable Functions: Payable functions can handle transactions, while non-payable functions cannot.

Example:

contract Transaction {
 function transferEther() public payable {
 // handle Ether transfer
 }

 function readData() public view {
 //Read data from the blockchain
 }
}

Function Overloading:

Functions can have the same name but different argument types or numbers. Overloading is identified by the argument types, not by return types.

Example:

contract Calculator {
 function add(int a, int b) public returns (int) {
 return a + b;
 }

 function add(int a, int b, int c) public returns (int) {
 return a + b + c;
 }
}

In the ABI, overloaded functions have mangled names to distinguish them.

You now have a solid foundation in understanding Solidity functions for Solana programming. Next, we'll delve into constructors in Solidity.