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 ]

Procedures and functions

proc

In Nim, a proc can be either a procedure (no return value) or a function (has a return value).

(1) procedure [no return value]
#include <stdio.h>

void hello()
{
    puts("hello world");
}

int main()
{
    hello();

    return 0;
}
proc hello() =
  echo "hello world"

hello()
(2) function [return a value]
#include <stdio.h>

int twice(const int n)
{
    return 2 * n;
}

int main()
{
    int value = twice(8);

    printf("%d\n", value); // 16

    return 0;
}
proc twice(n: int): int =
  return 2 * n

let value = twice(8)

echo value  # 16

In Nim, formal parameters are in read-only mode. So inside twice(), the value of n cannot be changed. This Nim code is equivalent to the C code on the left side.

func

A func is a pure function.

What is a pure function? A function that has no side effect (doesn't change its environment) and for the same input it always returns the same output.

In the example above, twice() is actually a pure function, thus it could also be defined like this:

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

Short form

If the body is just 1 line, you can write the body after the '=' sign:

proc hello() = echo "hello world"

If the subroutine has no input parameters, you can omit the parentheses:

proc hello = echo "hello world"

Note: I don't really like this last short form.

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, 21:18