Functions in V are declared with the
A function is called using a function name and a list of arguments:
Function parameters are written in the Go style; first comes the name, then the type:
In V, function parameters cannot have default values. Just like in Go and C, functions cannot be overloaded. This simplifies the code and improves its maintainability and readability.
You can use Trailing struct literal arguments to emulate default values and named arguments.
As you may have noticed, the return type is specified after the parameter list. If the function does not return a value, then the return type is omitted:
V supports multiple return values:
When calling such a function, you must assign each return value to a variable. The number of variables must match the number of return values.
If you want to ignore one or more return values, you can use
Functions can be used before their declaration:
sub are declared after
main, but can still be called from
This is true for all declarations in V and eliminates the need for header files or thinking about the order of files and declarations.
Variable number of arguments
You can mark the last parameter of a function with
to indicate that the function can take a variable number of arguments:
In such a case, you can call the function with any number of arguments:
...a array unpacking, you can pass an array as a function argument:
Inside the function itself, such an argument will be represented as an array:
Immutable function arguments by default
In V function arguments are immutable by default, and mutable args have to be marked on call.
This is to prevent accidental modification of arguments, which can lead to bugs.
It is possible to modify function arguments by declaring them with the keyword
Note that you have to add
nums when calling this function.
This makes it clear that the function being called will modify the value.
It is preferable to return values instead of modifying arguments,
user = register(user) instead of
Modifying arguments should only be done in performance-critical parts of your application to reduce allocations and copying.
For this reason, V does not allow the modification of arguments with primitive types (e.g. integers). Only more complex types such as arrays and maps may be modified.
Default argument value
Like Go, V does not support default argument values. However, this limitation can be bypassed using Trailing struct literals.
V does not support calling functions with named parameters. However, this limitation can be bypassed using Trailing struct literals.
Parameter evaluation order
The evaluation order of the parameters of function calls is NOT guaranteed. Take, for example, the following program:
V currently does not guarantee that it will print 100, 200, 300 in that order.
The only guarantee is that 600 (from the body of
f), will be printed after all of them.
That may change in V 1.0.
Functions can have generic parameters, which are specified using square brackets after the function name:
See Generics for more information.
Functions can be declared as inline using the
This will cause the function to be inlined at the call site. This can improve performance, but also increase the size of the binary.
To prevent the inlining of a function, use the