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
| Keyword | Bengali | English | Description |
|---|---|---|---|
dhoro | ধরো | let | Regular mutable variable |
sthir | স্থির | const | Immutable constant (cannot be changed) |
bishwo | বিশ্ব | global | Global variable (accessible everywhere) |
Regular Variables (dhoro)
Use the dhoro keyword for regular mutable variables:
| 1 | // Basic variable declaration |
| 2 | dhoro naam = "Rahim"; |
| 3 | dhoro boyosh = 25; |
| 4 | dhoro active = sotti; |
| 5 | |
| 6 | // Variables can be reassigned |
| 7 | dhoro message = "Hello"; |
| 8 | message = 42; // Now holds a number |
| 9 | message = [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 |
| 2 | sthir PI = 3.14159; |
| 3 | sthir MAX_SIZE = 100; |
| 4 | sthir APP_NAME = "BanglaCode"; |
| 5 | |
| 6 | // Use constants |
| 7 | dhoro area = PI * 5 * 5; |
| 8 | dekho(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 |
| 2 | bishwo ganok = 0; |
| 3 | |
| 4 | kaj barao() { |
| 5 | // Can access and modify global variable |
| 6 | ganok = ganok + 1; |
| 7 | } |
| 8 | |
| 9 | barao(); |
| 10 | barao(); |
| 11 | barao(); |
| 12 | |
| 13 | dekho(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 |
| 2 | dhoro x = 10; |
| 3 | |
| 4 | kaj test1() { |
| 5 | dhoro x = 20; // New local variable |
| 6 | dekho(x); // 20 |
| 7 | } |
| 8 | |
| 9 | test1(); |
| 10 | dekho(x); // 10 (outer x unchanged) |
| 11 | |
| 12 | // Global variable - shared across all scopes |
| 13 | bishwo counter = 0; |
| 14 | |
| 15 | kaj test2() { |
| 16 | counter = counter + 1; // Modifies global |
| 17 | } |
| 18 | |
| 19 | test2(); |
| 20 | test2(); |
| 21 | dekho(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 (
naamandNaamare different)
| 1 | // Valid variable names |
| 2 | dhoro firstName = "Rahim"; |
| 3 | dhoro _private = "secret"; |
| 4 | dhoro count2 = 100; |
| 5 | sthir MAX_VALUE = 1000; |
| 6 | bishwo 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:
| 1 | dhoro x = 10; |
| 2 | |
| 3 | // Simple assignment |
| 4 | x = 20; |
| 5 | |
| 6 | // Compound assignment operators |
| 7 | x += 5; // x = x + 5 (x is now 25) |
| 8 | x -= 3; // x = x - 3 (x is now 22) |
| 9 | x *= 2; // x = x * 2 (x is now 44) |
| 10 | x /= 4; // x = x / 4 (x is now 11) |
| 11 | |
| 12 | dekho(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:
| 1 | dhoro outer = "I am outer"; |
| 2 | |
| 3 | kaj 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 | |
| 9 | myFunction(); |
| 10 | dekho(outer); // Works |
| 11 | // dekho(inner); // Error! inner is not defined here |
Closures
Functions capture variables from their surrounding scope:
| 1 | kaj makeCounter() { |
| 2 | dhoro count = 0; |
| 3 | |
| 4 | ferao kaj() { |
| 5 | count = count + 1; |
| 6 | ferao count; |
| 7 | }; |
| 8 | } |
| 9 | |
| 10 | dhoro counter = makeCounter(); |
| 11 | dekho(counter()); // 1 |
| 12 | dekho(counter()); // 2 |
| 13 | dekho(counter()); // 3 |
Combining All Three
Here's an example using all three variable types together:
| 1 | // Constants for configuration |
| 2 | sthir PI = 3.14159; |
| 3 | sthir RATE = 0.05; |
| 4 | |
| 5 | // Global for shared state |
| 6 | bishwo total = 0; |
| 7 | |
| 8 | kaj 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 | |
| 18 | dekho(calculateArea(5)); // 78.53975 |
| 19 | dekho(calculateArea(10)); // 314.159 |
| 20 | dekho("Total:", total); // Total: 392.69875 |
Type Checking
Use the dhoron (type) built-in function to check a variable's type:
| 1 | dhoro num = 42; |
| 2 | sthir STR = "hello"; |
| 3 | bishwo flag = sotti; |
| 4 | |
| 5 | dekho(dhoron(num)); // "int" or "float" |
| 6 | dekho(dhoron(STR)); // "string" |
| 7 | dekho(dhoron(flag)); // "boolean" |
Type Conversion
BanglaCode provides built-in functions for type conversion:
| 1 | // Convert to string |
| 2 | dhoro num = 42; |
| 3 | dhoro str = lipi(num); // "42" |
| 4 | |
| 5 | // Convert to number |
| 6 | dhoro text = "123"; |
| 7 | dhoro number = sonkha(text); // 123 |
| 8 | |
| 9 | // Check results |
| 10 | dekho(dhoron(str)); // "string" |
| 11 | dekho(dhoron(number)); // "int" |
Best Practices
- Use
sthirfor values that should never change (configuration, mathematical constants) - Use
bishwosparingly - only when truly needed for shared state - Prefer
dhorofor most variables - Use descriptive names that explain the variable's purpose
- Use UPPERCASE for constants, camelCase or snake_case for others