Norscode Functions

Functions are one of the most important structure tools in Norscode. They make logic easier to name, easier to test and easier to move as a codebase grows. Good function design is one of the clearest differences between code that stays maintainable and code that quickly becomes tiring to change.

Clear names and clear contracts

A useful function name tells the reader what the function is for before they read the body. A useful return type tells the rest of the program what it may rely on.

funksjon hils(navn: tekst) -> tekst {
    returner "Hei, " + navn
}

That pattern stays valuable even in larger systems. If a function validates input, compute something, formats text and writes to storage all at once, the reader has to understand too many responsibilities in one place.

Pure logic versus side effects

Try to separate functions that calculate values from functions that perform effects such as logging, storage or network calls. Pure functions are easier to test and easier to reuse because they only depend on their input and output.

funksjon rabatt(total: heltall, prosent: heltall) -> heltall {
    returner total - (total * prosent / 100)
}

Effectful functions still matter, but they are usually easier to reason about when they sit at the edge of the system instead of being mixed into every piece of business logic.

Composition over long handlers

In practical Norscode projects, one function often coordinates the flow while smaller functions handle details such as validation, transformation or formatting. That makes the top-level flow read more like an explanation of intent than a pile of implementation detail.

As a rule of thumb, split a function when better names, better tests or clearer responsibilities appear. Stop splitting when extra functions only add indirection without improving any of those things.

The best continuation from here is modules if you want file structure next, or testing if you want to make function behaviour safer under change.