Skip to content
NorscodeNorscode

Your first Norscode program: Hei, verden

ArticleBy the Norscode project

Write, check, and run your very first program in Norscode — step by step.

terminal $ nc run hei.no Hei, verden!

There is a tradition in programming: the very first thing you make simply prints "Hei, verden" on the screen. It is small, but it proves that everything fits together — the tool, the code, and your machine. Let's do it in Norscode.

Create the file

Create a file called hei.no and write:

funksjon start() -> heiltall {
    skriv("Hei, verden!")
    returner 0
}

A couple of things to notice:

  • funksjon start() is where the program begins. Every Norscode program starts in the start function.
  • -> heiltall says that the function returns an integer. Here it is 0, which means everything went well.
  • skriv(...) prints the text to the screen.

Check before you run

Before you run anything, you can let the runtime read through the code:

nc check hei.no

nc check goes through the syntax and types without running the program. If there is a typo or a type that doesn't match, you are told now — not in the middle of a run.

Run the program

nc run hei.no

You should see this in the terminal:

Hei, verden!

Congratulations — that is your first Norscode program.

What if something went wrong?

  • "command not found": nc is not installed or not on your path. See the guide to installing Norscode.
  • An error message from nc check: read it calmly — it is in Norwegian and usually points right at the line. Check that every " has a partner, and that the braces { } balance.
  • Nothing happens: remember that execution starts in start. Without a start function, the program doesn't know where to begin.

Next steps

Now that the first program runs, the next natural step is your first loop, or the whole of Getting started.

What each part means

The program is small, but every line has a role. funksjon start() declares the function that execution begins in — every Norscode program starts here, no matter how many other functions you have. The arrow -> heiltall says that the function returns an integer; the number 0 is a convention that means "everything went well", while a different number can signal an error. skriv(...) sends the text to the screen, and returner 0 ends the program with the good status.

Try a few small variations

When the first program works, the best way to learn is to change a little and see what happens. Write another line:

funksjon start() -> heiltall {
    skriv("Hei, verden!")
    skriv("Dette er Norscode.")
    returner 0
}

Or use a variable:

funksjon start() -> heiltall {
    la navn = "Ada"
    skriv("Hei, " + navn + "!")
    returner 0
}

la creates a variable, and + joins text together. Small changes like this quickly build intuition for how the language fits together.

Why start and heiltall?

That execution always begins in start means you are never in doubt about where a program starts. And that start returns an integer follows an old tradition: a program tells the outside world whether it succeeded through a numeric code, where 0 means success. A build script or other automation can read that code without interpreting the text the program printed.

Common beginner mistakes

  • Forgot start: without a start function, the program doesn't know where to begin, and nothing happens.
  • Unbalanced braces or quotation marks: every { needs a }, and every quotation mark a partner. nc check usually points right at the line.
  • Forgot returner: start should return an integer — remember returner 0 at the end.

Once this sticks, you are ready for your first loop or the whole of Getting started.

Related

Back to the overview