Difference between revisions of "Functions"

From Mesham
Jump to navigationJump to search
m
Line 6: Line 6:
 
== Semantics ==
 
== Semantics ==
  
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.
+
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:Compound Types|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 ==
 
== Example ==
Line 25: Line 25:
  
 
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.
 
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''
  
 
[[Category:Core Mesham]]
 
[[Category:Core Mesham]]

Revision as of 18:13, 13 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 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.

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