Recent Changes - Search:

Oktatás

* Programozás 1
  + feladatsor
  + GitHub oldal

* Szkriptnyelvek
  + feladatsor
  + quick link

Teaching

* Programming 1 (BI)
  ◇ exercises
  ◇ quick link

teaching assets


Félévek

* 2025/26/2
* archívum


Linkek

* kalendárium
   - munkaszüneti napok '20
* tételsorok
* jegyzetek
* szakdolgozat / PhD
* ösztöndíjak
* certificates
* C lang.
* C++
* C#
* Clojure
* D lang.
* Java
* Nim
* Nim2
* Scala


[ edit | logout ]
[ sandbox | passwd ]

parameter passing

Terminology

Consider this code snippet:

func twice(n: int): int =
  2 * n

let value = twice(7)  # 14
  • Parameter (or formal parameter): the n in the function definition func twice(n: int): int
  • Argument (or actual parameter): the 7 you pass at the call site twice(7)

Many people use "parameter" and "argument" interchangeably, but if we want to be strict, there's a difference between them.

Formal parameters are immutable

By 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 int, char) are passed by value.

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 const pointer, choosing whatever is more efficient for the type's size.

We can simply think of it the following way: whatever is passed to the procedure, it's immutable inside the procedure.

Shadowing

Also 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?

  • x is passed by value. The procedure gets the value and assigns it to n. n on the formal parameter list is an immutable variable. There's no way to change x on the call site.
  • Inside the function, we copy the value of n into a local, mutable variable. However, we don't want to come up with a new name for this mutable copy. And here comes the shadowing: Nim allows us to create a local, mutable variable with the same name. The new n (which is a new variable) will shadow the n on the formal parameter list.
  • When you set n's value to 10, you modify the local, mutable copy. After the line "var n = n", you cannot refer to the original n on the formal parameter list since it was shadowed.
  • This way, you get mutability inside the procedure without affecting the caller (variable x) at all.
  • This is an idiom in Nim, don't be afraid to use it.

Pass by reference

#                 vvv
proc double_it(n: var int) =
  n *= 2

# ---------- #

var x = 8

echo(x)          # 8

double_it(x)

echo(x)          # 16   !!!
  • On the call site we have the variable x whose value is 8. It's a mutable variable.
  • We pass it to the procedure by reference. Notice the var keyword on the formal parameter list. n becomes an alias for the x variable on the call site. Now, when you modify the value of n, you actually modify x.
  • Think of n as an "alias". It's not a copy, not a pointer you dereference — it truly is another name for the same variable.
  • This way, a procedure can modify its argument on the call site.
Cloud City

  

Blogjaim, hobbi projektjeim

* The Ubuntu Incident
* Python Adventures
* @GitHub
* heroku
* extra
* haladó Python
* YouTube listák


Debrecen | la France


[ edit ]

Edit - History - Print *** Report - Recent Changes - Search
Page last modified on 2026 April 09, 12:59