Introducing functions
Functions allow you to logically wrap a set of instructions inside a code block to perform a specific operation. Often, it is necessary to provide them with a name that represents the underlying logic they encompass. Functions might take arguments as an input to perform an operation. Additionally, they might return the result of the operation performed by it. Therefore, functions offer code reusability and code readability.
A function can call another function if required to do so. In some scenarios, a function can call itself to perform a recursive operation.
V facilitates working with functions. A function in V is created using the fn
keyword. The typical syntax to create a function in V is shown here:
ACCESS-MODIFIER fn FUNCTION_NAME(ARGUMENT1_NAME ARGUMENT1_DATATYPE, ARGUMENT2_NAME ARGUMENT2_DATATYPE) RETURN_DATATYPE { OPERATIONS }
In the preceding syntax, we can identify two parts of a function:
- A method signature...