Functions
A function is a block of organized, reusable code that is used to perform a single, related action. Functions provide better modularity for your application and a high degree of code reusing.
Ideally, you should consider using the single responsibility principle (SOLID) which states that every module or function should have responsibility for a single part of the functionality provided by the software to keep your code maintainable.
Like C and Go, functions cannot be overloaded.
Note: The type comes after the argument's name.
Variadic Functions
Functions can also be variadic i.e. accept an infinite number of arguments. They are not arrays and cannot be returned.
Output
Multi-Return Functions
Similar to Go, functions in V can also return multiple and with a different type.
Output
High Order Functions
Functions in V can also take in another function as a parameter which is usually needed for something like sort, map, filter, etc.
Output
Exercises
Write a V program to find the square of any number using the function.
Write a V program to check a given number is even or odd using the function.
Write a V program to convert decimal number to binary number using the function.
Write a V program to check whether a number is a prime number or not using the function.
Write a V program to get the largest element of an array using the function.
Last updated