Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

Zig Functions

Zig Functions

Functions are important tools in programming languages for organizing code and implementing logic.

Functions are important tools for organizing code; using them properly can improve code readability and reusability.

In actual programming, you can define various functions as needed to implement specific functionalities.

Zig functions are defined using the fn keyword.

Basic Syntax

In Zig, the basic syntax for defining a function is as follows:

fn 函数名(参数列表) 返回类型 {

    // 函数体

}

For example, define a simple function printHello that prints "Hello, World!":

Example

const

std

=

@import

(

"std"

)

;

pub fn main

(

)

void

{

    printHello

(

)

;

}

fn printHello

(

)

void

{

    std.

debug

.

print

(

"Hello, World!\n"

, .

{

}

)

;

}

The output after compilation is:

Hello, World!

to the parameter

Functions can accept parameters, which can be values of various types.

Parameters are listed when the function is defined and passed to the function when called, for example:

Example

const

std

=

@import

(

"std"

)

;

pub fn main

(

)

void

{

    greet

(

"Alice"

)

;

}

fn greet

(

name

:

[

]

const

u8

)

void

{

    std.

debug

.

print

(

"Hello, {s}!\n"

, .

{

name

}

)

;

// Use the {s} format specifier to print a string slice

}

The output after compilation is:

Hello, Alice!

Return Value

Functions can return values; the return type is specified in the function definition using the return keyword.

For example, define a function that calculates the sum of two integers:

Example

const

std

=

@import

(

"std"

)

;

pub fn main

(

)

void

{

   

const

result

=

add

(

3

,

5

)

;

    std.

debug

.

print

(

"3 + 5 = {}\n"

, .

{

result

}

)

;

}

fn add

(

a

:

i32, b

:

i32

)

i32

{

   

return

a

b

;

}

The output after compilation is:

3 + 5 = 8

Recursion

A function can call itself, that is, a recursive call.

For example, define a recursive function that calculates a factorial:

Example

const

std

=

@import

(

"std"

)

;

pub fn main

(

)

void

{

   

const

result

=

factorial

(

5

)

;

    std.

debug

.

print

(

"5! = {}\n"

, .

{

result

}

)

;

}

fn factorial

(

n

:

i32

)

i32

{

   

if

(

n

==

0

)

{

       

return

1

;

   

}

else

{

       

return

n

factorial

(

n

1

)

;

   

}

}

The output after compilation is:

5! = 120

function overloading

Zig does not support function overloading in the traditional sense (that is, multiple functions with the same name but different parameters).

Zig provides generic function functionality; by using compile-time constant parameters, you can achieve an effect similar to function overloading, for example:

Example

const

std

=

@import

(

"std"

)

;

pub fn main

(

)

void

{

    printValue

(

i32,

5

)

;

// Explicitly specify the type as i32

    printValue

(

[

]

const

u8,

"Hello"

)

;

// Explicitly specify the type as []const u8

}

fn printValue

(

comptime T

:

type, value

:

T

)

void

{

    std.

debug

.

print

(

"{any}\n"

, .

{

value

}

)

;

// Use {any} to handle values of different types

}

In this example, the printValue function can accept parameters of any type and print based on the parameter type.

The output after compilation is:

5

{ 72, 101, 108, 108, 111 }

Inline functions

An inline function means the function's code is directly inserted at the call site during compilation, thereby avoiding the overhead of a function call.

In Zig, you can define inline functions using the inline keyword, for example:

Example

const

std

=

@import

(

"std"

)

;

// Define an inline function

inline

fn square

(

x

:

i32

)

i32

{

   

return

x

x

;

}

pub fn main

(

)

void

{

   

const

result

=

square

(

5

)

;

// Insert the inline function code at the call site

    std.

debug

.

print

(

"Square of 5 is {}\n"

, .

{

result

}

)

;

}

In this example, the square function is defined as an inline function, and the compiler will insert its code directly at the call site.

The output after compilation is:

Square of 5 is 25