version | example_title |
---|---|
1.0.0 |
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.
fn sum(x, y int) int {
return x + y
}
println(sum(77, 33)) // 110
Note: The type comes after the argument's name.
fn full_name(first_name, last_name string) string {
return first_name + ' ' + last_name
}
println(full_name("Vitor", "Oliveira")) // Vitor Oliveira
Functions can also be variadic i.e. accept an infinite number of arguments. They are not arrays and cannot be returned.
fn foo(test ...string) {
for txt in test {
println(txt)
}
}
foo("V", "is", "the", "best", "lang" , "ever")
Output
V
is
the
best
lang
ever
Similar to Go, functions in V can also return multiple and with a different type.
fn student(name string, age int) (string, int) {
return name, age
}
name, age := student("Tom", 15)
println(name1)
println(age1)
Output
Tom, 15
Functions in V can also take in another function as a parameter which is usually needed for something like sort, map, filter, etc.
fn square(num int) int {
return num * num
}
fn run(value int, op fn(int) int) int {
return op(value)
}
println(run(10, square)) // 100
The following are the rules which should be kept in mind while naming functions.
- Name should not contain Uppercase letters like
AlphaTest
- Use underscores as separators like
hello_world
- Name should not start with
_
- Name should be descriptive as possible
- Name should not contain
__
- Name should not contain any space
These rules are from Snake_Case
. V uses Snake Case and prefers it because it is more easy to read, write and understand.
fn i_am_valid()
fn thisworkstoo()
fn print_values_through_struct()
fn IamNotValid()
fn _print()
fn print__logs()
fn new Make Lexer()
- 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.