1.8. Functions

Felix allows you to define functions, although the syntax is different from C. Here is an example:
Start C++ section to tut/examples/tut106.flx[1 /1 ]
     1: include "std";
     2: fun mid(a:int, b:int):int =
     3: {
     4:   val c = (a + b) / 2;
     5:   return c;
     6: }
     7: print (mid(2,4)); print "\n";
End C++ section to tut/examples/tut106.flx[1]
It is clear that mid returns an int, and you might think that 'mid' has two arguments. This is not so. All functions in Felix have exactly one argument. Well, almost all of them :-) I'll explain shortly.

Functions in Felix may not have any side effects, except for diagnostic outputs. Note however that functions may modify their own private data, that is, may contain and mutate local variables.

Whilst in the above example the return type of the function is clearly given, it is not necessary, as illustrated by the next example:

Start C++ section to tut/examples/tut107.flx[1 /1 ]
     1: include "std";
     2: fun mid(a:int, b:int) =
     3: {
     4:   val c = (a + b) / 2;
     5:   return c;
     6: }
     7: print (mid(2,4)); print "\n";
End C++ section to tut/examples/tut107.flx[1]
which is equivalent to the one above. Note however that the types of the arguments must be given.


1.8.1. Preconditions and postconditions