AngelScript

Default arguments

Sometimes implementing different functions for each overload is unecessary 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 will be evaluated in the context that the function is called, so if the expression contains any identifiers you need to remember that those will be resolved in the local scope where the function is called and not in the scope where the function in declared.

      int myvar = 42;
      void Function(int a, int b = myvar) {}
      void main()
      {
        int myvar = 1;
        Function(1);    // This will use the local myvar and not the global 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; }