Methods
In EasyBite’s Object‑Oriented world, methods are the functions that live inside classes and define the behavior of your objects. While attributes hold state, methods perform actions, manipulate that state, and return results. This guide gives you a very detailed, step‑by‑step explanation of everything you need to know about methods in EasyBite—from basic syntax to advanced inheritance techniques—with numerous examples and clear outputs so that complete beginners can follow along.
Table of Contents
- Methods
What Is a Method?
A method is simply a function that is defined inside a class. Methods describe what an object can do or how it behaves. For example, a Car
class might have methods like start()
, drive(distance)
, and stop()
. Methods have access to the object they belong to, so they can read and modify that object’s attributes.
Defining a Method
To define a method in EasyBite, you use the method
keyword, then write its body, and close it with end method
. Methods go inside a class ... end class
block:
class Example
set counter to 0
method increment()
set counter to counter + 1
end method
method reset()
set counter to 0
end method
end class
method increment()
declares a method namedincrement
with no parameters.- The body runs in the context of the current object.
- You must always close a method with
end method
.
No output occurs until you create an object and call its methods.
Method Parameters
Methods can accept parameters, just like global functions. You list them in parentheses after the method name:
class Calculator
method add(a, b)
return a + b
end method
method multiply(x, y)
return x * y
end method
end class
a
andb
are required parameters foradd
.- You must supply values when you call
add()
on an object.
Return Values
If a method computes something you want to use elsewhere, use the return
keyword. The value you return is handed back to the caller:
class Greeter
method getGreeting(name)
return "Hello, " + name + "!"
end method
end class
set g to new Greeter()
set message to g.getGreeting("Emma")
show(message)
Output:
Hello, Emma!
If a method does not include a return
, it implicitly returns nil
(or no value), which is fine for methods that perform actions rather than calculations.
Using this
in Methods
Inside a method, you can refer to the current object explicitly using the this
keyword. This is especially helpful if your method has a parameter or local variable with the same name as an attribute:
class Counter
set count to 0
method init(start)
set this.count to start
end method
method increment()
set this.count to this.count + 1
end method
method getCount()
return this.count
end method
end class
set c to new Counter(10)
show(c.getCount())
// Output:
// 10
c.increment()
show(c.getCount())
// Output:
// 11
Using this.count
makes it crystal‑clear you’re working with the object’s attribute.
Calling Methods
Once you’ve defined a class with methods and created an object using new
, you invoke a method with dot notation:
set car to new Car()
car.start()
car.drive(15)
car.stop()
- The syntax is
object.methodName(args…)
. - Parentheses
()
are always required, even if there are no parameters.
Default Parameters in Methods
EasyBite lets you supply default values for method parameters, so callers can omit them if they’re fine with the standard behavior:
class Logger
method log(message, prefix to "[LOG]")
show(prefix + " " + message)
end method
end class
set logger to new Logger()
logger.log("System started")
// Output:
// [LOG] System started
logger.log("Disk full", "[ERROR]")
// Output:
// [ERROR] Disk full
prefix to "[LOG]"
sets a default.- Omitting
prefix
uses"[LOG]"
; supplying it overrides the default.
Method Overriding and parent
In inheritance, a subclass can override a method from its parent. To call the parent’s version inside the override, use parent.methodName(...)
:
class Animal
method speak()
show("Animal makes a sound.")
end method
end class
class Dog inherit Animal
method speak()
parent.speak() // call Animal’s speak()
show("Dog barks.")
end method
end class
set pet to new Dog()
pet.speak()
Output:
Animal makes a sound.
Dog barks.
This technique lets you extend behavior instead of replacing it entirely.
Naming Conventions for Methods
To keep your code consistent and readable:
- Use all lowercase, with no underscores (e.g.,
startengine
). - Keep names descriptive but concise (e.g.,
calculatearea
rather thanca
). - Match the verb‑object pattern for clarity (e.g.,
setspeed
,getbalance
,printreport
).
Best Practices
- One responsibility per method: keep methods focused on a single task.
- Limit parameters: more than three or four parameters can be confusing.
- Use default parameters for optional behavior.
- Leverage
this
to make attribute access explicit. - Document edge cases: if a method can fail or return
null
, note it in comments.
Detailed Examples with Outputs
Example 1: A Simple Person
Method
class Person
set name to ""
set age to 0
init(n, a)
this.name to n
this.age to a
end init
method birthday()
this.age to this.age + 1
show("Happy birthday, " + this.name + "! You are now " + this.age + ".")
end method
end class
set p to new Person("Sam", 29)
p.birthday()
Output:
Happy birthday, Sam! You are now 30.
Example 2: Calculator with Multiple Methods
class Calculator
method add(a, b)
return a + b
end method
method subtract(a, b)
return a - b
end method
method multiply(a, b)
return a * b
end method
method divide(a, b)
if b = 0
return nil
end if
return a / b
end method
end class
set calc to new Calculator()
show(calc.add(10, 5))
// Output:
// 15
show(calc.divide(10, 0))
// Output:
// null
Conclusion
Methods are the essential building blocks of object behavior in EasyBite. You now know how to:
- Define methods with
method … end method
. - Accept parameters and provide default values.
- Return results to callers.
- Use the
this
keyword for clarity. - Override methods in subclasses and call the parent with
parent
. - Name and organize methods following best practices.
With this knowledge, you can design rich, interactive objects and build scalable, maintainable applications in EasyBite. Happy coding!