robertoschler
over 1 year ago  Karma: 50
Do default values in Solidity work the same as they do in other programming languages?

A question regarding Solidity and what looks like "default values" for function call parameters. For example:

function someFunc(address theAddress = msg.sender)

Does this behave like default parameter values in other languages, whereby the caller of the function can override the default value by supplying an explicity value during the call?:

someFunc("akdfjejekfjdak");

Or in Solidity, is that parameter now "constant" if you try to provide an explicit value during the call, it will be flagged as error?

default-parameters
solidity
functions
robertoschler
over 1 year ago  Karma: 50
Do default values in Solidity work the same as they do in other programming languages?

A question regarding Solidity and what looks like "default values" for function call parameters. For example:

function someFunc(address theAddress = msg.sender)

Does this behave like default parameter values in other languages, whereby the caller of the function can override the default value by supplying an explicity value during the call?:

someFunc("akdfjejekfjdak");

Or in Solidity, is that parameter now "constant" if you try to provide an explicit value during the call, it will be flagged as error?

default-parameters
solidity
functions

2 ANSWERS
luke2
over 1 year ago Karma: 1864

Short answer: no, it's not the same as other languages and it's not setting a default value for optional parameters (although that's a EIP that's being considered)

  function set(uint _value = 123) public {
    value = _value;
  }

Trying to do this would give you a compiler error (including your example), try it on ethfiddle: https://ethfiddle.com/63RLPgpp_G

Short answer: no, it's not the same as other languages and it's not setting a default value for optional parameters (although that's a EIP that's being considered)

  function set(uint _value = 123) public {
    value = _value;
  }

Trying to do this would give you a compiler error (including your example), try it on ethfiddle: https://ethfiddle.com/63RLPgpp_G

Earn tokens by posting and answering questions about blockchain!
Karma to eth
YOUR ANSWER