Skip to content

References

Sami edited this page Nov 2, 2025 · 3 revisions

References in Kong

Kong supports pass-by-reference for function parameters, allowing functions to directly modify the original variables. Any changes made to a referenced parameter within the function body affect the variable outside the function's scope.

Parameters can be passed as references using the '&' keyword in the function.

Here's an exemple:

Funk swap(Int &a, Int &b) -> Int {
    Int temp = a;

    a = b;
    b = temp;
    Return 0;
}

Funk main() -> Int {
    Int a = 6;
    Int b = 7;

    print(a, b); 
    swap(a, b);
    print(a, b);
    Return 0;
}

Result:

(6, 7)
(7, 6)

References in vektors, arrays, tuples and strukts.

By default, elements inside vektors, arrays, tuples and strukts are references. That means every change you make to an element in a function is applied globally.

For instance:

Funk change(Int<4> arr) -> Int {
    arr[1] = 67;
    Return 0;
}

Funk main() -> Int {
    Int<4> arr = <0, 0, 0, 0>;

    print(arr);
    change(arr);
    print(arr);
    Return 0;
}

Result:

[0,0,0,0]
[0,67,0,0]

You can prevent this from happening by storing konst values in your vektor / array / tuple / strukt:

Funk change(Int<4> arr) -> Int {
    arr[1] = 67;
    Return 0;
}

Funk main() -> Int {
    Int Konst <4> arr = <0, 0, 0, 0>;

    print(arr);
    change(arr);
    print(arr);
    Return 0;
}

Result:

[0,0,0,0]
[0,0,0,0]

Clone this wiki locally