The Hyper Programming Language

Procedures

General

A procedure is the same as a 'member function' in C++ or a 'method' in Java. You can place procedures only inside a class.

Syntax

A procedure consists of a header, a body and an end. The header is in its simplest form nothing more than just:

procedure name()

The body can contain zero or more statements. The end is flexible; the longest version is:

end procedure name

Both the keyword procedure and the name are optional, so a simple end is sufficient.

Return type

A procedure which has a simple header as described above does not return a result. To have a procedure that returns a result you put a colon and the return type behind the header:

procedure name() : return-type

To actually return the result you use a return statement.

Parameters

A simple input parameter declaration looks like this:

name : type

Parameter declarations are placed in the procedure header between the round brackets, and multiple parameters are separated by commas. For more information about parameters see this page.

`Const' procedures

As in C++ you can declare a procedure to be const. This means that the procedure will not change the class' internal state, represented by the fields of the class. Procedures that are not const will not be allowed to be called for a class instance that is not changeable (so is declared const).

`Static' procedures

Again as in C++ you can declare a procedure to be static. This means that the procedure cannot (or does not need to) access the internal state, represented by the fields, of the class. `Static' procedures have in common with `const' procedures that they can be called for a class instance that is not changeable. Unlike non-`static' procedures, they can be called even without a class instance. You can call them by just using the name of the class they are a member of.

Procedures inside a static class are also static by default.

`Abstract' procedures

An abstract procedure is what in C++ a pure virtual function is, or in Java an `abstract' method. An abstract procedure is only allowed in an abstract class. Such a procedure is required to be overridden in a derived class.

`Sealed' procedures

This is a concept that you don't have in C++, and that in Java is called a `final' method. A sealed procedure cannot be placed inside an abstract class or a static class. You cannot override a sealed procedure in a derived class.

Combinations

You can make the following combinations:

In a combination const must come right before procedure.

Examples

First of all there is the Hello World example.

A procedure without parameters, and that returns nothing:

procedure sayHello()
  system.Out.line("Hello!")
end

A `static' procedure with one parameter, and that returns the double of the parameter:

static procedure twiceMyNumber(number : int) : int
  return number * 2
end

A procedure with 2 parameters, and that returns the sum of the parameters:

procedure sum(a : int, b : int) : int
  return a + b
end

The same procedure again, but with the short notation for parameters of the same type:

procedure sum(a & b: int) : int
  return a + b
end

A procedure that has one variable parameter:

procedure buildCommaList(inout list : string, stringToAdd : string)
  if !list.isEmpty then
    list += ", "
  end
  list += stringToAdd
end

See also


Valid HTML 4.01 Strict Valid CSS!