Skip to content

Procedures

Michael Wang edited this page Aug 8, 2022 · 7 revisions

Cish procedures can be thought of as glorified function pointers. They can be passed as values, arguments, return types, etc... because Cish embraces first class and higher order functions. A procedure literal declarations is composed of the proc keyword, followed by arguments encapsulated in parenthesis, followed by the return keyword, and finally followed by a type specifying the return type of the procedure. The example below best demonstrates the previous statement:

global readonly auto myProc = proc (int a, int b) return int {
  return a + b;
}

However there are other ways to declare a procedure that are often times less verbose:

  • For regular, named functions

proc helloThere(array<char> msg) {
  println(msg);
}

The above is the equivalent to:

global readonly auto helloThere = proc(array<char> msg) return nothing {
  println(msg);
}

and if it's declared inside another function (or any non-global context), helloThere is translated to {

readonly auto helloThere = proc(array<char> msg) return nothing {
  println(msg);
}
  • For "lambdas"

proc myLambda(int a, int b) => a + b;

or

sort(myNums, proc(int a, int b) => a - b); 

In addition, you actually don't have to specify a return type (ie return nothing, return int) so long as Cish can properly infer the type. In addition you don't need curly brackets so long the procedure only has one statement - this also apply's to all conditionals as well, include if, else, while, and for.

Recursive Calls

For every function the variable thisproc is a local, read-only variable that represents the current function. For example:

proc fib(int n) {
  if(n < 2)
    return n;
  return thisproc(n - 1) + thisproc(n - 2);
}

where thisproc points to the same function address as fib. This is particularly useful for recursive anonymous functions that are not tied to any specific variable.

Further reading

Read more about variable scoping, and how it applies to procedures here.

Read more about type arguments, and how they apply to procedures here.

Clone this wiki locally