Skip to content

Functions

Lucas Menezes edited this page Sep 22, 2020 · 4 revisions

Functions

Functions have three things:

  • name
  • return type
  • parameters

And are declared like this:

type name (parameters)

examples:

void func();
short pow(short x, short y);
byte opposite(byte 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 Headache functions are defined like this:

 void hello() {
  @"Hello!\n";
 }
 
 byte opposite(byte a) {
  return -a;
 }
 
 short pow(short x, short y){
    short r,z;
    r=1;
    z = y;
    while(z){
        r = r*x;
        z--;
    }
    return r;
 }

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

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

Headache requires that a function that has return must have its return as the last command of the function

Calling functions

name(arguments);

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

result = pow(base,exponent);

@"The opposite of "; @a; " is "; @opposite(a);
Clone this wiki locally