1.8. Functions

Felix allows you to define functions, although the syntax is different from C. Here is an example:
Start felix section to tut/examples/tut_beg106.flx[1 /1 ]
     1: #line 268 "./lpsrc/flx_tutorial.pak"
     2: #import <flx.flxh>
     3: fun mid(a:int, b:int):int =
     4: {
     5:   val c = (a + b) / 2;
     6:   return c;
     7: }
     8: print (mid(2,4)); print "\n";
End felix section to tut/examples/tut_beg106.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 felix section to tut/examples/tut_beg107.flx[1 /1 ]
     1: #line 291 "./lpsrc/flx_tutorial.pak"
     2: #import <flx.flxh>
     3: fun mid(a:int, b:int) =
     4: {
     5:   val c = (a + b) / 2;
     6:   return c;
     7: }
     8: print (mid(2,4)); print "\n";
End felix section to tut/examples/tut_beg107.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