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
  • 函数变量
  • 多返回函数
  • 高阶函数
  • 命名规则
  • 有效名称
  • 无效名称
  • Exercises

Was this helpful?

  1. 通过例子学V语言
  2. 例子
  3. section_3

函数

Previoussection_3Next方法

Last updated 4 years ago

Was this helpful?

函数是一个有组织、可重用的代码块,用于执行单个相关操作。 函数为应用程序提供了更好的模块性和高度的代码重用。 理想情况下,您应该考虑使用 (SOLID),它声明每个模块或功能都应该有责任 对于软件提供的功能的单个部分,以保持代码的可维护性。 像C和Go一样,函数不能重载。

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

println(sum(77, 33)) // 110

注意:类型在参数名之后。

fn full_name(first_name, last_name string) string {
    return first_name + ' ' + last_name
}

println(full_name("Vitor", "Oliveira")) // Vitor Oliveira

函数变量

函数也可以是可变的,即接受无穷多个参数。 它们不是数组,不能返回。

fn foo(test ...string) {
    for txt in test {
        println(txt)
    }
}

foo("V", "is", "the", "best", "lang" , "ever")

输出

V
is
the
best
lang
ever

多返回函数

与Go类似,V中的函数也可以返回多个不同类型的函数。

fn student(name string, age int) (string, int) {
    return name, age
}

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

输出

Tom, 15

高阶函数

V中的函数也可以接受另一个函数作为参数 需要排序、映射、筛选等。

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

命名规则

以下是命名函数时应记住的规则。

  • 名称不应包含像AlphaTest这样的大写字母`

  • 使用下划线作为分隔符,如hello_world

  • 名称不应以开头_

  • 名称应尽可能具有描述性

  • 名称不应包含__

  • 名称不应包含任何空格

有效名称

fn i_am_valid()
fn thisworkstoo()
fn print_values_through_struct()

无效名称

fn IamNotValid()
fn _print()
fn print__logs()
fn new Make Lexer()

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.

这些规则来自 V使用Snake Case,并且更喜欢它,因为它更易于阅读、书写和理解。

单一责任原则
Snake_Case