Method Declaration
All functions in Morph are declared with the method keyword.
Basic Syntax
// Without 'is' — recommended for methods
MethodName method() {
// body
}
// With 'is' — also valid
MethodName is method() {
// body
};
Convention: Omit is for method declarations. Use is for variable assignments.
With Parameters and Return Type
Add method(a as int, b as int) as int {
return a + b;
}
- Parameters:
name as type, separated by commas - Return type:
as typeafter the parameter list - No return type means the method returns nothing (void)
Calling a Method
result is Add(3, 5);
Print(result); // 8
Entry Point
The Init method in src/Main.mx is the program entry point:
Init method() {
Print("Hello Morph!");
}
No return type, no parameters. Automatically invoked by the runtime.
Method Naming Rules
- Method names must start with an uppercase letter (PascalCase)
- Minimum name length: 4 characters (configurable)
CalculateSum method(a as int, b as int) as int { // ✅ correct
return a + b;
}
add method(a as int, b as int) as int { // ❌ must start uppercase
return a + b;
}
Methods in Classes
Inside a class, methods are declared the same way:
Player class {
GetName public method() as string {
return "Player One";
}
}
Next Steps
- Parameters & Return Types — Detailed parameter syntax
- Constructor — Special initialization method
- Access Modifiers — public / private