V for Go programmers —Functions

Kevin Da Silva
3 min readApr 24, 2023

As we are following the order in “Introducing Go” applied to Vlang, today’s chapter is chapter six, “Functions” which for a functional programmer like it’s probably the most exciting chapter.

Functions

Functions in V work as the following:
First, we define the body of the function

fn my_function() {

}

Notice that we use the snake case convention in V as opposed to the Go camel case or Pascal case when working with module functions.

If our function has parameters we can define them by typing their identifiers and their corresponding types:

fn my_function(x int, y bool) {

}

If the types of our params are the same we can do like Go and express the following:

fn my_function(x, y int) {

}

In case we need to return something we have to inform the return type after the closing parenthesis character

fn my_function(x, y int) int {
return x+y
}

And in case I need a variable amount of parameters, V also supports variadic functions

fn my_function(args ...int) {
for arg in args {
println(arg)
}
}

Or if I need a closure:

fn my_function() fn(int, int) int {
return fn(x, y int) int {
return x + y
}

}

I can even curry functions:

fn curry_sum(x int) fn(int) int {
return fn [x] (y int) int {
return x+y
}
}

Notice that I had to use [x] to specify the inner function that I want to use variables declared in the outer scope.

V has support for recursion too and it’s very straightforward:

fn recursion_it(times int) int {
if times > 0 {
return times + recursion_it(times-1)
}

return 0
}

A recursive function that sums the parameter it receives while counting down the number of times it should be called.

Pointers

As Go V also has a very similar approach to pointers, but it has a few differences that we will cover below, but for now have a taste of how it works:

fn main() {
mut x := 5
zero(mut &x)
println(x) //should be 0
}

fn zero(mut xptr &int) {
xptr = 0
}

The main difference is that in order to pass a point as a parameter I don’t use “*” in the zero function instead I use “&”, and the second detail is that to work with pointers I have to declare the value I’m working on as mut when I declare x, call zero in main and declare a pointer as a parameter, otherwise it would output an error because constants cant be changed.

It changes a bit, but it’s very similar to what Go has.

Exercises

  • Write a function that takes an integer and halves it and returns true if it was even or false if it was odd. For example, half(1) should return (0, false) and half(2) should return (1, true).
  • Write a function with one variadic parameter that finds the greatest number in a list of numbers.
  • Write a make_odd_generator function that generates odd numbers.

<- CHAPTER 5

And we did it, one more chapter done!

We successfully learned how functions work in V and kept going this series, hope to see you in the next article!

--

--

Kevin Da Silva

I'm a back-end developer, functional programming lover, fascinated by computer science and languages. From the south part of Brazil to the world