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 already have known functions in Dinfio, like writeln(), sqrt(), and more. They are built-in functions. But you can also create (declare) your own functions to do some processes.

To declare your own function, use the following syntax:

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 do some processes and return a value. 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 but the difference is 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 syntax:

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 is called!")
    stop
endclass

Output:

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

Class Inheritance

A class can inherit to another class. A child class will inherit all attributes and all functions from the parent class. In Dinfio, a class can inherit to multiple classes—like C++. To do class inheritance, use 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, "UI")
    clara = employee("Clara", 23, "Dafturn Technology")
    sarah = freelancer("Sarah", 24, "ITB", "Dafturn Technology")
 
    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 = "UI"
)
employee(
    .name = "Clara"
    .age = 23
    .office = "Dafturn Technology"
)
freelancer(
    .name = "Sarah"
    .age = 24
    .campus = "ITB"
    .office = "Dafturn Technology"
)


← Control Flows Index


Documentation version: 1.0.09. Updated on: 17 April 2021.
Dinfio is designed and written by Muhammad Faruq Nuruddinsyah. Copyright © 2014-2021. All rights reserved.