|
Oktatás * Programozás 1 * Szkriptnyelvek Teaching * Programming 1 (BI) Félévek Linkek * kalendárium |
Nim2 /
increment a variableSimilar to Python. nim> var x = 0 nim> ++x Error: undeclared identifier: '++' nim> x++ Error: expression expected, but found '[EOF]'
var x = 0 x += 1 echo x # 1 x += 2 echo x # 3 inc(x) echo x # 4 x = 10 echo x # 10 x -= 1 echo x # 9 x -= 4 echo x # 5 dec(x) echo x # 4 More examples: var a = 0 inc(a) echo a # 1 a.inc() echo a # 2 a.inc echo a # 3 a.inc(5) echo a # 8 How does inc() work?By default, arguments are passed by value (like in C), i.e. a copy is made about the argument.
In Nim how you pass an argument to a procedure (by value or by reference) is indicated on the formal parameter list of the procedure. In C, you decide it on the caller's side. Either you pass a variable or its memory address. ExerciseImprove var x = 0 x.my_inc() echo x # 1 x.my_inc(4) # !!! new thing !!! echo x # 5 my_inc(x) echo x # 6 x.my_inc echo x # 7 |
![]() Blogjaim, hobbi projektjeim * The Ubuntu Incident [ edit ] |
||||