Functions

From Mesham
Revision as of 18:50, 13 January 2013 by Polas (talk | contribs)
Jump to navigationJump to search

Syntax

function returntype name(arguments)

Where returntype is a type chain or void.

Semantics

The type of the variable depends on the pass semantics (by reference or value.) Broadly, all element types types by themselves are pass by value and compound types are pass by reference; although this behaviour can be overridden by additional type information. Memory allocated onto the heap is pass by reference, static or stack frame memory is pass by value.

Example

function Int add(var a:Int,var b:Int) {
   return a + b;
};

This function takes two integers and will return their sum.

function void modify(var a:Int::heap) {
   a:=88;
}

In this code example, the modify function will accept an integer variable but this is allocated on the heap (pass by reference.) The assignment will modify the value of the variable being passed in and will still be accessible once the function has terminated.

Function prototypes

Instead of specifying the entire function, the programmer may just provide the prototype (no body) of the function and resolution will be deferred until link time. This mechanism is most popular for using functions written in other languages, however you must use the native modifier with native function prototypes.

Native function example

function native void myNativeFunction(var a:Int);

The main function

Returns void and can have either 0 arguments or 2. If present, the first argument is number of command line interface parameters passed in, 2nd argument is a String array containing these. Location 0 of string array is the program name. The main function is the program entry point, it is fine for this not to be present in a Mesham code as it is then just assumed that that code is a library and only accessed via linkage.

Since: Version 0.41b