2.8. Functions

Felix allows you to define functions, although the syntax is different from C. Here is an example:
Start felix section to tut/tutorial/tut-1.08-0.flx[1 /1 ]
     1: #line 365 "./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/tutorial/tut-1.08-0.flx[1]
Start data section to tut/tutorial/tut-1.08-0.expect[1 /1 ]
     1: 3
End data section to tut/tutorial/tut-1.08-0.expect[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/tutorial/tut-1.08-1.flx[1 /1 ]
     1: #line 394 "./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/tutorial/tut-1.08-1.flx[1]
Start data section to tut/tutorial/tut-1.08-1.expect[1 /1 ]
     1: 3
End data section to tut/tutorial/tut-1.08-1.expect[1]
which is equivalent to the one above. Note, however, that the types of the arguments must be given.


2.8.1. Preconditions and postconditions