The procedure is similar to functions in the Nim programming language.

Sometimes, you have a repeated code that needs to execute multiple times, move the code into a separate block called procedure.

The procedure involves two steps.

  • Declaration of a procedure
  • Calling procedure

How to declare and call a procedure in Nim

Procedure declared with the proc keyword in the declaration It contains procedure name, colon(:), followed by return type, equal(=), and sequence of code block statements indented by space.

Return_Type is a datatype that procedure returns of a value type. Syntax

proc procedurename(): Return_Type =
    //code blocks

A procedure is called with the name of a procedure.

procedurename();

Here is a simple example

proc hello() =
    echo "Hello World!"
hello()

Output:

Hello World!

Declare a procedure with arguments and return type

the procedure accepts arguments. and return type Inside the body, use the return statement to return the values.

The below example takes two arguments of type int and return int type.

proc add(first: int, second: int): int =
  return first+second
echo add(10, 30)

Output:

40

if proc declares multiple arguments of the same type, you can ignore the type except for the last argument.

proc add(first, second: int): int =
  return first+second
echo add(10, 30)

The return keyword is optional and the last calculated value is returned implicitly without using the return keyword.

proc add(first, second: int): int =
  first+second
echo add(10, 30)

Nim varargs example

Sometimes, you have multiple arguments of the same type, Then declare multiple fixed arguments.

Declared three arguments for a procedure.

proc add(a,b,c: int): int  =
a+b+c
echo(add(4,5,6));

What if you have 4 arguments, You have to redeclare it with a new function name of 4 arguments.

To allow multiple dynamic arguments, You can use varargs to accept multiple arguments.

Here is an example

proc add(values: varargs[int]): int  =
    for value in items(values):
      total += value

echo(add(4,5,6));

Output:

15

How to return an array or sequence from a procedure in Nim

Nim Procedure returns a sequence of types.

The procedure contains declaration seq[type] in the declaration

Here is an example

proc convertToSequence(values: varargs[int]): seq[int]=
  var sequence : seq[int]
  for value in items(values):
    sequence.add(value);
  return sequence

echo(convertToSequence(4,5,6));

Output:

@[4, 5, 6]

Nim Procedure overloading

Overloading is an OOPS programming feature, that contains multiple procedures with the same name and different arguments.

The below example contains two procedures with the same name and different arguments. The correct method to call is decided based on the number and type of arguments at runtime.

procedure overloading example

proc printMessage(msg: string) = echo "One"
proc printMessage(first, second: int) = echo "two"
printMessage("t")
printMessage(1, 2)

NIm Procedure default arguments

Nim allows you to declare a default argument value if the caller is not called with arguments.

In the below example,

  • Procedure accepts two arguments with default values.
  • First call not passed arguments, considered default values, and return the result
  • Second call not passed the second argument, considered second default argument values and return the result
  • the third call passed two arguments, ignored default values
proc add(first: int = 0, second = 1): int =
    first + second
echo add()
echo add(12)
echo add(10,12)

Output:

1
13
22

Procedure Named arguments

Name arguments allow the caller to pass argument names with their values.

It gives flexibility to pass argument names and simplifies when there are more arguments.

proc add(first: int = 0, second = 1): int =
    first + second
echo add()
echo add(second=12)

Procedure discard statement

Sometimes, We have a procedure that returns the result.

If the caller wants to ignore the result of a return value, a discard statement is used.

proc add(first,second: int): int =
    first + second
add(1,5)

It throws a below error Error: expression ‘add(1, 5)’ is of type ‘int’ and has to be used (or discarded) exit status 1

To avoid this error, We have to use either

  • assign the result of the procedure to a variable such as var result=add(1,5)
  • use discard statement such as discard add(1,5)
proc add(first,second: int): int =
    first + second
discard add(1,5)

Nim Methods

methods are similar to the procedure in NIm language, can be used in oops programming Syntax

method methodname(arguments: datatype): returntype = body