Ballerina Basics: 5 Fundamental Programming Concepts

Faria KarimFaria Karim
3 min read

Introduction:

This blog covers five fundamental programming concepts—variables, control structures, functions, data structures, and OOP—illustrated with examples in Ballerina. Mastering these basics is key to building efficient applications.

1. Variables and Data Types:

In any language, variables are used to store data, while data types define the type of data that can be stored in a variable. Ballerina has a set of primitive data types, including int, float, boolean, and string, as well as collection types like array and map. Use final to make a variable’s value unchangeable.

// Declaring variables in Ballerina
int age = 28;
string name = "Ballerina Dev";
boolean isDeveloper = true;
final string language = "Ballerina";
// language = "AnotherLanguage"; // This would raise an error.

2. Control Structures:

Control structures allow programs to make decisions and execute code conditionally. Ballerina supports control structures like if, else, while, and foreach.

int score = 85;

if score >= 90 {
    io:println("Grade: A");
} else if score >= 75 {
    io:println("Grade: B");
} else {
    io:println("Grade: C");
}

For looping through data, Ballerina has foreach, which is handy for processing collections or arrays.

int[] numbers = [1, 2, 3, 4, 5];
foreach int num in numbers {
    io:println("Number: ", num);
}

The while loop runs as long as a specified condition is true. Here, it’s used to print numbers from 1 to 5.

int count = 1;

while count <= 5 {
    io:println("Count: ", count);
    count += 1;
}

3. Functions:

Functions allow you to organize code into reusable blocks, making it modular and manageable. In Ballerina, functions are defined using the function keyword, and they can take parameters and return values.

function greet(string name) returns string {
    return "Hello, " + name + "!";
}

string greeting = greet("Ballerina");
io:println(greeting);

4. Data Structures:

Data structures like arrays, maps, and records are vital for organizing and managing data in programming. Ballerina offers a variety of data structures to handle different types of data.

Arrays are used to store a sequence of elements of the same type:

int[] ages = [20, 25, 30];
io:println(ages[0]); // Accessing the first element

Maps are key-value pairs for storing data where each value is associated with a key:

map<string> user = {
    "name": "Ballerina Dev",
    "role": "Developer"
};
io:println(user["name"]);

Records are more structured, allowing the definition of a custom data format:

import ballerina/io;

type Person record {
    string name;
    int age;
};

// Main function
public function main() {
    Person person = {name: "Ballerina", age: 28};
    io:println("Name: ", person.name);
}

5. Object-Oriented Programming (OOP):

Ballerina is also equipped with Object-Oriented Programming (OOP) features that allow for creating and managing objects. Objects are instances of a class that encapsulate data and behavior.

import ballerina/io;

class Developer {
    string name;
    int experience;

    // Constructor
    function init(string name, int experience) {
        self.name = name;
        self.experience = experience;
    }

    // Method
    function getDetails() {
        io:println("Name: ", self.name, "Experience: ", self.experience, " years");
    }
}

// Main function
public function main() {
    Developer dev = new Developer("Ballerina Dev", 5);
    io:println(dev.getDetails());
}

Conclusion:

The foundational concepts of programming—variables, control structures, functions, data structures, and OOP—are universal across programming languages and can be applied effectively in Ballerina. With these basics, you’ll be well-equipped to build and integrate services, handle data, and make full use of Ballerina’s powerful capabilities.

0
Subscribe to my newsletter

Read articles from Faria Karim directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Faria Karim
Faria Karim

Currently, I am working as an "Associate Software Engineer" at "Kaz Software". I have completed my graduation from North South University in the year 2020 with the highest distinction. I always love to explore new ideas. I am a curious and self-motivated person. So, I always try to keep learning and share my ideas with other people. I enjoy problem-solving a lot. I have solved around 100+ problems in various online judges and I am also a three-star coder at Hackerrank. Moreover, I have participated in the ACM ICPC two times. I have completed the off-sight round of ACM ICPC of 2018. I have some basic knowledge of c/c++, Java, Javascript, Php, and Python. I have done several projects using these programming languages. I have hands-on experience on various backend frameworks like Node.js, Django, Flask, and Laravel. I have also used React.js as the front-end library for a few of my projects. Currently, I am more into React.js. For web-based projects, I have worked on both MySQL and NoSQL (MongoDB) as the database system. Machine learning is my another matter of interest. I have research experience in the fuzzy system. I have done a few projects using Tensorflow, Keras, CNN, and LSTM. Recently I have grown my interest in game development. I have built two games. One is using construct 2 and the another one is in Unity 3D. Side by side, I am also trying to figure out the things on ionic. In my free time, I mostly like to surf the internet, watch movies, and also love to do animations and illustrations using Powerpoint.