More work
This commit is contained in:
parent
4a619004b9
commit
2ea62f358e
9 changed files with 119 additions and 1 deletions
20
src/variables/result.md
Normal file
20
src/variables/result.md
Normal file
|
|
@ -0,0 +1,20 @@
|
|||
# Result
|
||||
|
||||
The `result` variable is a special variable that serves as an implicit return. This is useful because in most cases, the control flow semantics of the `return` statement are unneeded. `result` is initialized with the default value for a given return type, so the the following code it starts as `0`, before being initialized to `number`
|
||||
|
||||
``` nimrod
|
||||
proc expr(number, power: int): int =
|
||||
result = number
|
||||
for i in 1..power:
|
||||
result *= number
|
||||
```
|
||||
|
||||
A common gotcha is declaring a variable called result and expecting it to have the same semantics.
|
||||
|
||||
``` nimrod
|
||||
proc unexpected(): int =
|
||||
var result = 5
|
||||
result += 5
|
||||
|
||||
echo unexpected() # Prints 0, not 10
|
||||
```
|
||||
Loading…
Add table
Add a link
Reference in a new issue