In Nim, there are three main ways to pass values as arguments to functions:
- By value: the function receives a copy of the original value, so any modifications made by the function do not affect the original value.
- By reference: the function receives a reference to the original value, so any modifications made by the function affect the original value.
- By pointer: the function receives a pointer to the original value, which is similar to passing by reference in other languages.
Here's how each approach is used:
- Passing by value
When a value is passed by value, a copy of the original value is made and passed to the function, so any changes made by the function do not affect the original value. This is useful when you want to keep the original value unchanged, and use a modified copy of it instead. Here's an example:
proc double(x: int): int =
return x * 2
var a = 10
var b = double(a)
echo a # prints 10
echo b # prints 20
The function double takes an integer argument x by value, and returns its double as another integer. When the variable a is passed to the function double, a copy of its value (which is 10) is made and used as the input argument x. After the function call, a remains unchanged (i.e., it still has the value 10), while b has the value returned by the function (which is 20).
- Passing by reference
When a value is passed by reference, the function receives a reference (i.e., an alias) to the original value, so any changes made by the function affect the original value. This is useful when you want to modify the original value in place, without creating a copy of it. Here's an example:
proc incrementByRef(ref x: int) =
x += 1
var a = 10
incrementByRef(a)
echo a # prints 11
The function incrementByRef takes an integer reference ref x, which means it will modify the original value of x. When the variable a is passed to the function incrementByRef, a reference to its value (which is 10) is passed, and the function increments this value by 1. After the function call, a has the modified value 11.
- Passing by pointer
When a value is passed by pointer, the function receives a pointer to the original value, which is similar to passing by reference in other languages. This is useful when you want to modify the original value in place, but you need to work with pointer arithmetic or use a C API that requires pointers. Here's an example:
proc incrementByPtr(x: ptr int) =
inc(x[])
var a = 10
incrementByPtr(addr a)
echo a # prints 11
The function incrementByPtr takes a pointer x: ptr int, which means it expects a memory address to an integer value. When the variable a is passed to the function incrementByPtr, its address is obtained using the addr operator, and passed as the argument x. The function uses the [] operator to dereference the pointer, and increments the integer value at that address by 1. After the function call, a has the modified value 11.
Now, regarding your question about using objects and var in function arguments, here's how it works:
- Objects
In Nim, objects are passed by reference by default (similar to Python), so any modifications made by the function affect the original object. However, you can also use a reference syntax to make it explicit that the argument is a reference to an object. Here's an example:
type
Person = object
name: string
age: int
proc celebrate(person: ref Person) =
person.age += 1
var bob = Person(name: "Bob", age: 30)
celebrate(bob)
echo bob.age # prints 31
In this example, the Person object bob is created with the name field set to "Bob" and the age field set to 30. The celebrate function takes a reference to a Person object, and increments its age field by 1. When bob is passed as the argument to celebrate, a reference to its memory address is passed, so any changes made by the function affect the original object. After the function call, bob has the modified age field 31.
- Var
In Nim,
var is a keyword that can be added to the argument declaration to make it mutable inside the function, even if it was passed by value. This is useful when you need to modify the input variable without creating a copy of it, and you don't want to use a reference syntax for objects. Here's an example:
proc capFirst(var s: string) =
s[0] = s[0].toUpper
var text = "hello, world!"
capFirst(text)
echo text # prints "Hello, world!"
In this example, the capFirst function takes a string argument var s, which means it will be mutable inside the function, even though it was passed by value. When the variable text is passed to the function capFirst, a copy of its value (which is "hello, world!") is made, but it is marked as mutable using the var keyword. The function then capitalizes its first character by setting it to the uppercased version of itself. After the function call, text has the modified value "Hello, world!".
So, to summarize:
- Use objects when you want to modify the members of a complex data structure, and you want changes to propagate to the original object. Objects are passed by reference by default.
- Use
var when you want to modify a simple value type (like an integer or a string), and you don't want to create a copy of it. var makes the argument mutable inside the function, even if it was passed by value.
- Use reference syntax (
ref or ptr) when you want to modify a value type or an object, and you want changes to propagate to the original object. Reference variables make it clear that the argument is a reference to an existing object or memory location.