Updated variables/result.md

This commit is contained in:
Dominik Picheta 2014-05-29 21:22:19 +01:00
commit 08a8ca6126

View file

@ -1,15 +1,15 @@
# 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`
The `result` variable is a special variable that serves as an implicit return variable. 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 following code starts as `0`, before being initialized to `number`:
``` nimrod
proc expr(number, power: int): int =
proc `**`(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.
A possible gotcha is declaring a variable called result and expecting it to have the same semantics.
``` nimrod
proc unexpected(): int =
@ -17,4 +17,4 @@ proc unexpected(): int =
result += 5
echo unexpected() # Prints 0, not 10
```
```