Function Type
Declaration
Functions are declared using the fn
keyword, and are optionally preceded with the pub
keyword to make it public.
Parameters
Parameters are defined in the form name: type
, and multiple arguments are separated by commas.
When calling a function, values must be passed in that correspond to each parameter.
In this case, each argument passed in must match the type of the corresponding parameter. In order to pass in the arguments out of order, or just to be more clear about the name of each argument, you can precede each argument with the name of the parameter and a colon.
All arguments in a function call must either be positional or named -- they cannot be mixed.
Variadic Parameters
Parameters can be declared as variadic like so:
Functions may only have one variadic parameter, and it must be the last parameter in the parameter list. Attempting to define a variadic parameter in a position other than the last one will result in an error.
Default Values
Parameters may be defined with default values, which must be a literal value.
In this case, passing in a value for the s
parameter is optional. If no value is provided when calling the function, the pre-defined default value will be used. Default values do not need to be the last parameter in the parameter list, however using the default value of a parameter that is not last in the list requires the use of named arguments.
Return Values
Return values are defined with an arrow and the return value's type after the parameter list.