Function arguments that aren't required

Godot Version

4.3

Question

Can anyone point me in the right direction-

I have a function, that takes 3 arguments… but i would like to completely avoid giving it the third one for example and then the function sort of assumes things on its own.

Like:

func add3(a,b,c) -> float:
   if c == null:
      return a+b
   else:
      return a+b+c

so this shouldn’t break:

var result = add3(2,5)

Is this possible?

func add3(a,b,c = null) -> float:

This will make the third argument optional.

2 Likes

cool… that was…simple. thanks!

Worth mentioning as well that it doesn’t have to be a “null”, it can be any other value that will be treated as “default” when this argument is not provided explicitly. So e.g.
func add3(a,b,c = 420) -> float:
Will also work fine, it will always add 420 if you don’t provide the 3rd argument explicitly.

2 Likes

ah yes very good! thanks!

1 Like