13

I didn't find a way to have optional arguments in the solidity docs so i'm wondering if anyone has found a working around to this ?

All i can think of are internal functions with " empty " params but that's going to be ugly very fast.

As anyone found a working around to this ?

solinooby
  • 261
  • 1
  • 2
  • 6
  • Just to clarify, what are you trying to do? Optional based on what conditions? Do you mean being able to feed in a parameter into a function which isn't used within the function? – Physes Jul 03 '16 at 21:12
  • no, i mean function x(params1,--optionalparams2,--optionalparams3,-optionalparams4) where --optionalparams is as the name says optional – solinooby Jul 06 '16 at 18:57
  • @solinooby Did you find an answer to your question bellow? If yes, please mark as answer and vote up. thanks, – Muhammad Altabba Jan 15 '18 at 11:01

3 Answers3

8

Unfortunately solidity does not have the ability to add variable/ optional params. Strange enough there is away to add anonymous params, i.e. with no name.

Roland Kofler
  • 11,638
  • 3
  • 44
  • 83
5

As mentioned by @roland-kofler and @muhammad-altabba , Solidity does not provide for optional parameters. Though such functionality can be achieved by using function overloading : http://solidity.readthedocs.io/en/v0.4.21/contracts.html#function-overloading
or a hack could be sending 0x0 as the argument when not present and accounting for that within the function.
I hope this is helpful

Adibas03
  • 388
  • 2
  • 7
4

Till now, this is no optional parameters in Solidity.

However, they plan to include this. You can check the discussion at GitHub: https://github.com/ethereum/solidity/issues/232

Muhammad Altabba
  • 2,157
  • 1
  • 15
  • 31