0
//SPDX-License-Identifier: MIT

pragma solidity ^0.8.0;

contract funTesting  {

   


    function add(uint x, uint y) public pure returns(uint) {
        uint sum;
      
        x = x+10;

        sum = x + y;
        sum = sum * getValueToAdd(2);
        return sum;
    }

    function getValueToAdd(uint num) public pure returns(uint) {

        uint value = num*10;
        return value;
    }

}


//Free function
//this is the function that i want to call in my funTesting contract
function dummy(uint a) pure returns(uint) {
    uint _a = a;
    return _a;
}

4 Answers 4

0

Since solc 0.7.1 Solidity has "free functions" https://docs.soliditylang.org/en/v0.8.15/contracts.html#functions. Free functions have an implicit internal modifier, so they couldn't be used outside a contract.

You could access them like any other function in the contract.

//SPDX-License-Identifier: ISC
pragma solidity ^0.7.1;

function bar(uint256 a, uint256 b) pure returns (uint256) {
    return a + b;
}

contract AA {
    uint256 public a;

    constructor() {
        a = 1234;
    }

    function foo() public view returns (uint256) {
        return bar(a, 1000);
    }
}
0

Solidity doesn't have free functions, so your question does not make any sense.

0

You cannot create a free function in solidity without/outside a contract{}. But you can create 2 contracts and then separate the functions you want to separate. So your code should look like something like this:

//SPDX-License-Identifier: MIT

pragma solidity ^0.8.0;

contract funTesting {

function add(uint x, uint y) public pure returns(uint) {
    uint sum;
  
    x = x+10;

    sum = x + y;
    sum = sum * getValueToAdd(2);
    return sum;
}

function getValueToAdd(uint num) public pure returns(uint) {

    uint value = num*10;
    return value;
}
} //Free function //this is the function that i want to call in my funTesting contract 

contract Dummy {

 function dummy(uint a) public pure returns(uint) {
     uint _a = a; return _a; 
     }
     
}
 

And then to call those functions there are two methods actually (1) Direct Call & (2) Super Call. There is a great video on Youtube explaining direct & super call, you definitely should check that out: https://youtu.be/lqRYnIejWMk

0

Yes, Solidity doesn't have free functions. Did you mean how to call functions on other contracts?

contract ContractA {
    function callBFromA(
        address contractB,
        uint256 a,
        uint256 b
    ) external pure returns (uint256) {
        // This will work too IContractB(contractB).sumOnB(a, b);
        return ContractB(contractB).sumOnB(a, b);
    }
}

interface IContractB {
    function sumOnB(uint256 a, uint256 b) external pure returns (uint256);
}

contract ContractB is IContractB {
    function sumOnB(uint256 a, uint256 b) override external pure returns (uint256) {
        return a + b;
    }
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.