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 ]

How to return a value from a function

There is the classical way:

proc twice(n: int): int =
  return 2 * n

You can omit the return statement. In this case the value of the last expression is returned:

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

Since the body is very short here, you can do it in just 1 line (short form):

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

When you have a function, a variable called result is implicitly declared for you. The type of this variable will be the return type of your function. If you use this variable inside your function, then the value of result will be returned at the end of the function implicitly. That is, it's not necessary to write "return result" at the end of the function:

proc twice(n: int): int =
  # var result: int    # you get this implicitly
  result = 2 * n

Another example:

proc my_counter(): int =
  result = 0    # optional, result is implicitly initialized with 0
  for i in 1..5:
    result += 1

let value = my_counter()

echo value  # 5

Here, the variable result is implicitly created. And Nim initializes every variable. An int is initialized with 0. However, when I use a variable as a counter, I prefer initializing it explicitly. At the end of my_counter() the value of result is returned implicitly.

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 04, 09:58