Skip to content

References

TheRealMichaelWang edited this page Mar 26, 2021 · 3 revisions

References

Often times in object-oriented-languages, referencing rules are complicated and inconsistent. FastCode aims to provide a consistent, simple referencing system that also conforms to most programming practices.

What does reference even mean?

When a value is passed by reference, that means the receiving variable's value will echo the value you passed by reference, and vice versa.

For example, if we set the variable a to a reference of the variable b,

a = ref b

and assign a value to a

a = 7

that same value has also been assigned to b and every other value that references either a or b.

print(b) //outputs 7

It also works the other way around. If we assign a value to b,

b = 13

it will also be assigned to a and every other value that references either a or b.

print(a) //outputs 13

What gets passed by reference and what doesn't?

Any procedure argument or value with a reference data type (see value types for further reading) gets passed by reference automatically. Otherwise (in the case of all primitives unless specified), each value is cloned beforehand.

For example, if the variable a is passed into the procedure count,

procedure count(n) {
    while n >= 0 {
        printl(n)
        n = n - 1
    }
}
a = 10
count(a)

the variable a would be set to -1 even though it was set to 10 when we passed it into count. Keep in mind that tis is a common practice and it's done to avoid cloning values on a per-function basis.

Clone this wiki locally