Passing Function Pointers in Solidity Function Calls

Published by Mario Oettler on

Solidity allows you to call a function and pass a function pointer as parameter.

Since this technique leads to hardly readable code, don’t use it if possible. We only show it that you can read and understand it if you stumble upon it.

As of Solidity 0.8.15 this works only with external and internal functions (private and public don’t work).

Example

pragma solidity 0.8.20;

contract FunctionPointerTest{

    function f1(uint256 x) internal pure returns(uint256){
        return x;
    }

    function f2(function(uint256) internal pure returns(uint256)a ) internal pure returns(uint256){
        return a(27);
    }
    
    function f3() public pure returns(uint256){
        return f2(f1);
    }
}
Categories:

if()