Difference between revisions of "Functions"

From Mesham
Jump to navigationJump to search
m (moved Procedures to Functions)
m (7 revisions imported)
 
(4 intermediate revisions by the same user not shown)
Line 1: Line 1:
 +
<metadesc>Mesham is a type oriented programming language allowing the writing of high performance parallel codes which are efficient yet simple to write and maintain</metadesc>
 
== Syntax ==
 
== Syntax ==
  
function returntype name[arguments]
+
function returntype name(arguments)
 +
 
 +
Where ''returntype'' is a type chain or ''void''.
  
 
== 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: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 ==
  
  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.
 +
 +
== 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 ==
 
== 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.
 +
 
 +
''Since: Version 0.41b''
  
 
[[Category:Core Mesham]]
 
[[Category:Core Mesham]]

Latest revision as of 15:44, 15 April 2019

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