LogoLogo
  • V by Example
  • en
    • examples
      • section_3
        • Functions
        • Methods
        • Arrays
        • Struct
      • section_1
        • Primitives
        • Keywords
        • Variables
        • Formatted Print
        • Strings
        • Comments
      • section_4
        • Files
        • JSON
        • Testing
        • Array Functions
      • section_2
        • Operators
        • Looping Constructs
        • If statement
        • Match
  • V por exemplos
    • Contribuindo
    • examples
      • section_1
        • Comentários
        • Primitivos
        • Palavras-chave
      • section_2
        • Operadores
  • V mit einem Beispiel
    • examples
      • section_3
        • Methods
        • Struct
        • Functions
        • Arrays
      • section_4
        • Testing
        • Array Functions
        • JSON
        • Files
      • section_2
        • Looping Constructs
        • If statement
        • Match
        • Operators
      • section_1
        • Primitive Datentypen
        • Schlüsselwörter
        • Formatted Print
        • Strings
        • Kommentare
        • Variables
    • CONTRIBUTING_de
  • V dengan Contoh
    • examples
      • section1
        • Variabel
  • 通过例子学V语言
    • 例子
      • section_3
        • 函数
        • 方法
        • 数组
        • 结构体
      • section_1
        • 基本类型
        • 关键字
        • 变量
        • print系列函数
        • 字符
        • 注释
      • section_4
        • 文件
        • JSON操作
        • 调试
        • 数组函数
      • section_2
        • 运算符
        • 循环结构
        • If表达式
        • 匹配
  • コード例で学ぶV言語
    • examples
      • section_3
        • 関数
        • メソッド
        • 配列
        • 構造体
      • section_1
        • プリミティブ型
        • キーワード
        • 変数
        • 書式付き出力
        • 文字列
        • コメント
      • section_4
        • ファイル操作
        • JSON操作
        • テスト
        • 配列の関数
      • section_2
        • 演算子
        • ループ
        • If文
        • マッチ
  • Changelog
  • Contributing
  • Documentation Style Guide
Powered by GitBook
On this page
  • Variadic Functions
  • Multi-Return Functions
  • High Order Functions
  • Exercises

Was this helpful?

  1. en
  2. examples
  3. section_3

Functions

Previoussection_3NextMethods

Last updated 3 years ago

Was this helpful?

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 (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))

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"))

Variadic Functions

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

Multi-Return Functions

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
}

name1, age1 := student("Tom", 15)
println(name1)
println(age1)

Output

Tom, 15

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.

fn square(num int) int {
    return num * num
}

fn run(value int, op fn(int) int) int {
    return op(value)
}

println(run(10, square))

Output

100

Exercises

  1. Write a V program to find the square of any number using the function.

  2. Write a V program to check a given number is even or odd using the function.

  3. Write a V program to convert decimal number to binary number using the function.

  4. Write a V program to check whether a number is a prime number or not using the function.

  5. Write a V program to get the largest element of an array using the function.

single responsibility principle