Functions

From Mesham
Revision as of 16:11, 12 January 2013 by Polas (talk | contribs)
Jump to navigationJump to search

Syntax

function returntype name(arguments)

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 composite 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.

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.