Home News DOC Class DL Others Cooker
Documents  >  Tutorial  >  Methods
Method definition

A method is an interface that uses the form of function call. The main purpose is to specify behaviar of the class.


Table of contents:


Format

Access control

method

Return type

Method name

Argument

endmethod

Example

Format

The format of the method definition is:


Access-control method Return-type Method-name(Argument)

...

endmethod


It is a definition that starts with access control, followed by the keyword method, and ends with the keyword endmethod.

You can define methods with different order of arguments even if they have a same name. And, you can place them anywhere in the class definition.

The method definition will generate a scope.

Access control

It specifies the access level of the getter using the keywords open or closed.

If you specify open, the getter can be called from outside of the class definition.

If you specify closed, the getter can only be used inside of the class definition.

method

It is a keyword that indicates that it is a method definition. It must be specified next to access control.

Return type

It specifies the return type after the keyword method. This indicates the class of instances returned by the method.

You should specify void if the method does not return a value.

If the return type is not the void class, it uses the return statement to return a value. The class of this value must match the return type.

The return statement is optional if it does not return a value.

Method name

It specifies the method name after the return type. The naming rules for method name is the same as for identifiers.

Exceptionally, you can use the name of the built-in class as the method name.

Argument

Method arguments are specified next to the Method-name, it must be enclosed by parentheses.

An argument is a pair of class name and argument name.

If there are two or more arguments, separate them with commas. No comma is required when one argument. If no argument, the parentheses are empty.

Since the method are distinguished by the order of the argument classes, you can define methods with different order of arguments even if they have a same name.

endmethod

It is a keyword that indicates the end of the method definition.

You can use the abbreviation "em" instead of "endmethod". Note that "em" is also a keyword.

Example

Let's define methods in the example class.

1:

class example

2:

int x;

3:

int y;

4:

open fitter example() // Default fitter

5:

x = 0;

6:

y = 0;

7:

endfitter

8:

open fitter example(int xx, int yy) // Fitter with two arguments.

9:

x = xx;

10:

y = yy;

11:

ef // Abbreviation.

12:

open setter X(int val)

13:

x = val;

14:

endsetter

15:

open getter int X

16:

return x;

17:

endgetter

18:

open setter Y(int val)

19:

y = val;

20:

es // Abbreviation.

21:

open getter int Y

22:

return y;

23:

eg // Abbreviation.

24:

open method void Clear()

25:

x = 0;

26:

y = 0;

27:

return; // Optional, not required.

28:

endmethod

29:

open method example Add(int vx, int vy)

30:

return new example(x + vx, y + vy);

31:

endmethod

32:

open method example Add(example ve) // Same name, different argument.

33:

return new example(x + ve.x, y + ve.y);

34:

em // Abbreviation.

35:

endclass

It calls the method using the form of a function call.

1:

example ex = new example(10, 10);

2:

ex.Clear();

3:

example ex2 = ex.Add(10, 10);

3:

example ex3 = ex.Add(ex2);

Next
Previous
Copyright © Rice All rights reserved.