|
Oktatás * Programozás 1 * Szkriptnyelvek Teaching * Programming 1 (BI) Félévek Linkek * kalendárium |
Nim2 /
parameter passingTerminologyConsider this code snippet: func twice(n: int): int = 2 * n let value = twice(7) # 14
Many people use "parameter" and "argument" interchangeably, but if we want to be strict, there's a difference between them. Formal parameters are immutableBy default, formal parameters are immutable. And this is good. (Kotlin does the same.) func twice(n: int): int = # n = 10 # ERROR! return 2 * n Small values (like Large objects (for example, a big static array) may be passed by const reference under the hood. We still get the immutability guarantee (can't modify it inside the function), but without the cost of actually copying the whole array. The compiler decides whether to physically copy or pass a We can simply think of it the following way: whatever is passed to the procedure, it's immutable inside the procedure. ShadowingAlso known as variable shadowing or parameter shadowing. proc something(n: int) = var n = n n = 10 echo n # 10 # -------------------- # var x = 5 something(x) echo x # 5 What happens here?
Pass by reference# vvv proc double_it(n: var int) = n *= 2 # ---------- # var x = 8 echo(x) # 8 double_it(x) echo(x) # 16 !!!
|
![]() Blogjaim, hobbi projektjeim * The Ubuntu Incident [ edit ] |