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
  • 可変長引数を取る関数
  • 値を複数返す関数
  • 高階関数
  • 命名のルール
  • 正しい名前
  • 正しくない名前
  • 演習

Was this helpful?

  1. コード例で学ぶV言語
  2. examples
  3. section_3

関数

Previoussection_3Nextメソッド

Last updated 5 years ago

Was this helpful?

関数とは、再利用可能な形に整えられたコードのブロックであり、単一の作業を実行します。 関数はアプリケーションのモジュラリティを高め、コードをより高度なレベルで再利用できるようにします。

理想は、SOLID原則で言うところのに従うことです。この原則は「関数は、そのソフトウェアが提供する機能のひとつの部品についてのみ責任を負うこと」というものであり、コードを今後もメンテナンスできるようにするためのものです。

CやGoと同様、Vの関数もオーバーライドは禁止されています。

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(name)
println(age)

上の出力結果:

Tom, 15

高階関数

Vの関数は、別の関数をパラメータとして受け取ることもできます。これはsortやmapやfilterといった処理でよく必要になります、

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)

  • 名前の冒頭には_を置かないこと

  • できるかぎり、意味の明快な名前を付けること

  • 名前に__を含んではならv

  • 名前に(種類を問わず)スペース文字を含んではならない

正しい名前

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

正しくない名前

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

演習

  1. 数値の2乗を求めるVプログラムを関数の形で書きましょう。

  2. 渡された数値が偶数か奇数かをチェックするVプログラムを関数の形で書きましょう。

  3. 10進数を2進数に変換するVプログラムを関数の形で書きましょう。

  4. 渡された数値が素数化どうかをチェックするVプログラムを関数の形で書きましょう。

  5. 渡された配列の要素の最大値を得るVプログラムを関数の形で書きましょう。

上のルールはが由来です。Vではsnake_caseスタイルが用いられ、また推奨されます(読みやすく、書きやすく、理解しやすいため)

単一責任の原則
snake_case