0

just wanted to know that in Solidity we can give function parameters without any name by just giving type, but how can we use them inside a function:

pragma solidity >=0.6.0 <0.7.1;

contract demo{
    
function myFunction1(int, int8, int) public{
        // now ho to use these received parameters
    }
}

This question is already asked here but no reasonable answer is given. Thanks in advance.

1
  • how can we use them inside a function - you're obviously going to write code inside that function, so start by giving a name to each one of those unnamed parameters. In other words, the answer to "how to use unnamed parameters" is "by naming them"!!! Sep 16 '20 at 16:33
2

You can't access them with Solidity. You may be able to use inline assembly to do it, but there is no point really, and you should just give them a name if you want to use them in the function.

There are a few use cases for unnamed parameters. They can be useful if you have a function that overrides another function, but you only need certain parameters from that function. They are also often used for return parameters. For example:

interface A {
  function doSomething (uint256 a, uint256 b) external returns (uint256);
}

contract B is A {
  function doSomething(uint256, uint256 b) override external returns (uint256) {
    return b;
  }
}

Since it's not accessing a, you can omit the name in the override function. It also returns directly in the function, so there is no need to name the return parameter, like:

contract B is A {
  function doSomething(uint256, uint256 b) override external returns (uint256 c) {
    c = b;
  }
}

This can be useful or not, depending on your use case. Removing the parameter altogether would result in a different function signature, and in this case a compiler error since the function does not implement the interface properly.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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