Functions and Classes

Dinfio Documentation





Functions

A function is a statements block which takes zero, one, or more input parameters and does some processing inside it. You have already known functions in Dinfio, e.g. writeln(), sqrt(), and more. They were built-in functions. However, you can also create (declare) your own functions to do some processes.

To declare your own function, use the following pattern:

function function_name([parameter_1], [parameter_2], ..., [parameter_n])
    ' Statements
stop

Example:

function greeting()
    writeln("Hello, my name is Clara")
stop
 
function another_greeting(name, age)
    writeln("Hello, my name is " & name)
    writeln("And my age is " & age)
stop

To call your functions you already declared, use keyword function_name():

start
    greeting()                        ' Call function
    another_greeting("Aisha", 22)     ' Call function with parameters
stop
 
function greeting()
    writeln("Hello, my name is Clara")
stop
 
function another_greeting(name, age)
    writeln("Hello, my name is " & name)
    writeln("And my age is " & age)
stop

Output:

Hello, my name is Clara
Hello, my name is Aisha
And my age is 22

Your function may return a value after do some processes. To do this, use keyword return inside your function:

start
    a = calculate(10, 20)   ' a equals to 200
stop
 
function calculate(x, y)
    return x * y
stop

In Dinfio, there is one special keyword: yield. This keyword is similar to return, yet it has its own behaviour. The difference is that yield does not stop the execution of a function:

start
    a = do_something(12)   ' a equals to -1
    b = do_something(16)   ' b equals to 1
stop
 
function do_something(x)
    yield -1
 
    if x >= 15
        return 1
    endif
stop

Classes

One of the Dinfio's paradigms is object-oriented programming (OOP). So, a class is an important thing in OOP. You can declare your class by using the following pattern:

class class_name
    field attribute_1
    field attribute_2
    field attribute_n
endclass

Example:

class person
    field name, age
    field nationality
endclass

To create an object of your class, use keyword class_name():

start
    p = person()   ' Create an object from class person
 
    p.name = "Aisha"
    p.age = 22
    p.nationality = "ID"
 
    writeln(p.name)   ' Output: Aisha
stop
 
class person
    field name, age
    field nationality
endclass

Your class may have member functions to do some tasks.

start
    p = person()
 
    p.name = "Aisha"
    p.age = 22
    p.nationality = "ID"
 
    p.change_name()
    p.print()   ' Output: Hello, my name is Clara
stop
 
class person
    field name, age
    field nationality
 
    function change_name()
        this.name = "Clara"   ' You can use keyword "this" to access attributes
    stop
 
    function print()
        writeln("Hello, my name is " & this.name)
    stop
endclass

Class Constructor

A constructor is a function that initialise object's attributes upon object creation. A constructor will be automatically called when object is created. This is an example how to create a constructor:

start
    p = person("Aisha", 22, "ID")
 
    writer(p)
stop
 
class person
    field name, age
    field nationality
 
    function construct(name, age, nat)
        this.name = name
        this.age = age
        this.nationality = nat
 
        writeln("Constructor was called!")
    stop
endclass

Output:

Constructor was called!
person(
    .name = "Aisha"
    .age = 22
    .nationality = "ID"
)

Class Inheritance

A class can inherit another class. A child class will inherit all attributes and all functions from the parent class. In Dinfio, a class can inherit multiple classes—like C++. To do class inheritance, place the colon symbol : next to your class name (class child: parent or class child: parent_1, parent_2, ..., parent_n) and call function extend() in your constructor.

start
    aisha = student("Aisha", 19, "Universitas Indonesia")
    clara = employee("Clara", 23, "Dinfio Foundation, Inc.")
    sarah = freelancer("Sarah", 24, "The University of Edinburgh", "Dinfio Foundation, Inc.")
 
    writer(aisha)
    writer(clara)
    writer(sarah)
stop
 
class person
    field name
    field age
 
    function construct(name, age)
        this.name = name
        this.age = age
    stop
endclass
 
class student: person   ' class student inherits to class person
    field campus
 
    function construct(name, age, campus)
        extend(this, person(name, age))
 
        this.campus = campus
    stop
endclass
 
class employee: person   ' class employee inherits to class person
    field office
 
    function construct(name, age, office)
        extend(this, person(name, age))
 
        this.office = office
    stop
endclass
 
class freelancer: student, employee   ' class freelancer inherits to class student and employee
    function construct(name, age, campus, office)
        extend(this, student(name, age, campus))
        extend(this, employee(name, age, office))
    stop
endclass

Output:

student(
    .name = "Aisha"
    .age = 19
    .campus = "Universitas Indonesia"
)
employee(
    .name = "Clara"
    .age = 23
    .office = "Dinfio Foundation, Inc."
)
freelancer(
    .name = "Sarah"
    .age = 24
    .campus = "The University of Edinburgh"
    .office = "Dinfio Foundation, Inc."
)


← Control Flows Index


Documentation version: 1.0.16. Updated on: 14 July 2025.
Dinfio is designed and written by Muhammad Faruq Nuruddinsyah. Copyright © 2014-2025. All rights reserved.