Skip to content

Functions

LucasMW edited this page Jan 19, 2017 · 2 revisions

Functions

Functions Have a three things:

  • name
  • return type
  • parameters

And are declared like this:

type name (parameters)

examples:

void func(); float sum(float[] array); int opposite(int a);

void means no type, and in this case, means the function func has no return.

functions need to be associated to a block of code, i.e. its definition

In monga functions are defined like this:

 void hello() {
  @"Hello!\n";
 }
 
 int opposite(int a) {
  return -a;
 }
 
 float sum(float[] a,int size) {
  int i;
  float s;
  s = 0;
  i=0; 
  while(i<size) {
   s = a[i] + s;
   i = i+1;
  } 
  return s;
  }

Note that the void function has no return, like its supposed.

The return must contain the expression that contains the result of its execution.

##Calling functions

name(arguments);

it will result in an expression which contains the result of the function

result = arraySum(array,limit);

@"The opposite of "; @a; " is "; @opposite(a);

Functions can be declared one file and defined in another file.

Monga can use C defined functions, but there are some limitations.

Clone this wiki locally