Language Basics

Variables

BanglaCode provides three ways to declare variables: dhoro (regular), sthir (constant), and bishwo (global). BanglaCode is dynamically typed, so you don't need to specify types.

Variable Declaration Keywords

KeywordBengaliEnglishDescription
dhoroধরোletRegular mutable variable
sthirস্থিরconstImmutable constant (cannot be changed)
bishwoবিশ্বglobalGlobal variable (accessible everywhere)

Regular Variables (dhoro)

Use the dhoro keyword for regular mutable variables:

1// Basic variable declaration
2dhoro naam = "Rahim";
3dhoro boyosh = 25;
4dhoro active = sotti;
5
6// Variables can be reassigned
7dhoro message = "Hello";
8message = 42; // Now holds a number
9message = [1, 2, 3]; // Now holds an array

Constants (sthir)

Use the sthir (স্থির = fixed/constant) keyword for values that should never change. Attempting to reassign a constant will result in an error:

1// Declare constants
2sthir PI = 3.14159;
3sthir MAX_SIZE = 100;
4sthir APP_NAME = "BanglaCode";
5
6// Use constants
7dhoro area = PI * 5 * 5;
8dekho(area); // 78.53975
9
10// This will cause an error!
11// PI = 3.14; // Error: 'PI' ekti sthir (constant), eitake bodlano jabe na

⚠️ Constant Naming Convention

By convention, use UPPERCASE names for constants to make them easily identifiable.

Global Variables (bishwo)

Use the bishwo (বিশ্ব = world/global) keyword for variables that need to be accessible from any scope, including inside functions:

1// Declare a global variable
2bishwo ganok = 0;
3
4kaj barao() {
5 // Can access and modify global variable
6 ganok = ganok + 1;
7}
8
9barao();
10barao();
11barao();
12
13dekho(ganok); // 3

Global vs Regular Variables

The key difference is that regular variables inside functions create new local bindings, while global variables are shared across all scopes:

1// Regular variable - function creates new local binding
2dhoro x = 10;
3
4kaj test1() {
5 dhoro x = 20; // New local variable
6 dekho(x); // 20
7}
8
9test1();
10dekho(x); // 10 (outer x unchanged)
11
12// Global variable - shared across all scopes
13bishwo counter = 0;
14
15kaj test2() {
16 counter = counter + 1; // Modifies global
17}
18
19test2();
20test2();
21dekho(counter); // 2

Naming Rules

Variable names must follow these rules:

  • Must start with a letter (a-z, A-Z) or underscore (_)
  • Can contain letters, digits (0-9), and underscores
  • Cannot be a reserved keyword
  • Are case-sensitive (naam and Naam are different)
1// Valid variable names
2dhoro firstName = "Rahim";
3dhoro _private = "secret";
4dhoro count2 = 100;
5sthir MAX_VALUE = 1000;
6bishwo app_state = "running";
7
8// Invalid variable names (will cause errors)
9// dhoro 2count = 5; // Cannot start with digit
10// dhoro first-name = ""; // Hyphens not allowed
11// dhoro dhoro = 5; // Cannot use reserved keywords

Assignment Operators

BanglaCode supports compound assignment operators for regular variables:

1dhoro x = 10;
2
3// Simple assignment
4x = 20;
5
6// Compound assignment operators
7x += 5; // x = x + 5 (x is now 25)
8x -= 3; // x = x - 3 (x is now 22)
9x *= 2; // x = x * 2 (x is now 44)
10x /= 4; // x = x / 4 (x is now 11)
11
12dekho(x); // Output: 11
13
14// Note: Compound operators don't work on constants
15// sthir Y = 10;
16// Y += 5; // Error!

Variable Scope

Variables have function scope. A variable declared inside a function is only accessible within that function:

1dhoro outer = "I am outer";
2
3kaj myFunction() {
4 dhoro inner = "I am inner";
5 dekho(outer); // Works - can access outer variable
6 dekho(inner); // Works - can access local variable
7}
8
9myFunction();
10dekho(outer); // Works
11// dekho(inner); // Error! inner is not defined here

Closures

Functions capture variables from their surrounding scope:

1kaj makeCounter() {
2 dhoro count = 0;
3
4 ferao kaj() {
5 count = count + 1;
6 ferao count;
7 };
8}
9
10dhoro counter = makeCounter();
11dekho(counter()); // 1
12dekho(counter()); // 2
13dekho(counter()); // 3

Combining All Three

Here's an example using all three variable types together:

1// Constants for configuration
2sthir PI = 3.14159;
3sthir RATE = 0.05;
4
5// Global for shared state
6bishwo total = 0;
7
8kaj calculateArea(radius) {
9 // Local variable for computation
10 dhoro area = PI * radius * radius;
11
12 // Update global total
13 total = total + area;
14
15 ferao area;
16}
17
18dekho(calculateArea(5)); // 78.53975
19dekho(calculateArea(10)); // 314.159
20dekho("Total:", total); // Total: 392.69875

Type Checking

Use the dhoron (type) built-in function to check a variable's type:

1dhoro num = 42;
2sthir STR = "hello";
3bishwo flag = sotti;
4
5dekho(dhoron(num)); // "int" or "float"
6dekho(dhoron(STR)); // "string"
7dekho(dhoron(flag)); // "boolean"

Type Conversion

BanglaCode provides built-in functions for type conversion:

1// Convert to string
2dhoro num = 42;
3dhoro str = lipi(num); // "42"
4
5// Convert to number
6dhoro text = "123";
7dhoro number = sonkha(text); // 123
8
9// Check results
10dekho(dhoron(str)); // "string"
11dekho(dhoron(number)); // "int"

Best Practices

  • Use sthir for values that should never change (configuration, mathematical constants)
  • Use bishwo sparingly - only when truly needed for shared state
  • Prefer dhoro for most variables
  • Use descriptive names that explain the variable's purpose
  • Use UPPERCASE for constants, camelCase or snake_case for others