| AngelScript |

Default arguments

Sometimes implementing different functions for each overload is unnecessary when the difference can be provided with a default value to a parameter. This is where default arguments come in handy.

By defining default arguments in the declaration of the function, the script doesn't have to provide these values specifically when calling the function as the compiler will automatically fill in the default arguments.

      void Function(int a, int b = 1, string c = "")
      {
        // Inside the function the arguments work normally
      }

      void main()
      {
        // Default arguments doesn't have to be informed
        // The following three calls produce the exact same result
        Function(0);
        Function(0,1);
        Function(0,1,"");
      }

When defining a default argument to one of the parameters, all subsequent parameters must have a default argument too.

The default argument expression can include references to variables or call functions, but only if the variables or functions are visible in the global scope.

      int myvar = 42;
      void Function(int a, int b = myvar) {}
      void main()
      {
        int myvar = 1;
        Function(1);    // This will use the global myvar and not the local myvar
      }

The special 'void' expression can be used as default argument to make an optional output parameter.

      void func(int &out output = void) { output = 42; }