Difference between revisions of "Functions"

From Mesham
Jump to navigationJump to search
Line 2: Line 2:
 
== Syntax ==
 
== Syntax ==
  
function returntype name[arguments]
+
function returntype name(arguments)
  
 
== Semantics ==
 
== Semantics ==
  
In a function all arguments are pass by reference (even constants). If the type of argument is a type chain (requires ''::'') then it should be declared in the body
+
The type of the variable depends on the pass semantics (by reference or value.) Broadly, all [[:Category:Element Types|element types]] types by themselves are pass by value and [[:Category:Composite Types|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 ==
 
== Example ==
  
  function Int add[var a:Int,var b:Int]
+
  function Int add(var a:Int,var b:Int) {
{
 
 
     return a + b;
 
     return a + b;
 
  };
 
  };
  
 
This function takes two integers and will return their sum.
 
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 ==
 
== The main function ==
  
Returns void, and like C, it 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.
+
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.
  
 
[[Category:Core Mesham]]
 
[[Category:Core Mesham]]

Revision as of 16:11, 12 January 2013

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.