Passing Function Pointers in Solidity Function Calls

Published by Mario Oettler on

Last Updated on 12. June 2023 by Mario Oettler

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:

https://blockchain-academy.hs-mittweida.de/wp-content/uploads/2021/04/logo_bcam_rgb_gross.png

Welcome

Blockchain Academy

Continue with credential

No account yet?

Powered by Hidy

Register with Hidy


Register
Sign in

We need the following credential to register:

please select:

To create a new account, we need the following data from you:

We would appreciate the following additional information from you for the creation of your account:

Welcome

Blockchain Academy

Continue with credential

Already have an account?

Powered by Hidy

Sign in with Hidy


Register
Sign in

We need the following credential to log in:

please select: