ProgrammingLanguages.md

Meqt

I have learned several programming languages like C++(C11), Python3, Swift(5.8), and just started learning Rust. I have had a course on Java primarily for it’s Object-Oriented Programming but not write much code in Java. I also had a glimpse of JavaScript, write some shell script, I don’t remember if I have learned matlab, but I don’t like it and use python when ever possible to replace. (maybe Swift instead of Python months after, my work on MEQT)

In this Article, I want to talked about my understandings and experiences on Compiled and Interpreted Language, Static-Typed and Dynamically-Typed Language, and Script Language.

I also want to compared languages on performance, safety, user-friendly, how hard to start and how hard to learn, you know this could be two thing, capability, usage, programming paradims, syntax, maintainance, popularity, periphery tools like package manager, and environment manager.

The last thing I want to talk about how editor tools shift programming language’s syntax. I don’t know much about LLVM, but it’s greatly related to thest topics and brought a great change to all this thing, besides, LLVM has been awarded the 2012 ACM Software System Award!. And Chris Lattner is co-founder of LLVM, Clang compiler, MLIR compiler infrastructure and the Swift programming language.

Programming languages

Introduction:
Welcome to today’s video, where we’ll explore the essential programming languages for Computer Science (CS) or Electrical Engineering and Computer Science (EECS) majors. As a CS/EECS major, having a diverse knowledge of programming languages is crucial for tackling various projects and solving complex problems. In this video, we’ll cover the features, usage, and history of important languages, including C/C++, Python, Java, JavaScript, Rust, Go, TypeScript, Swift, Kotlin, Dart, HTML, CSS, Verilog, MATLAB, Assembly, SQL, LaTeX, Markdown, and Shell Script. Let’s dive in!

C/C++:

C and C++ are foundational languages widely used in systems programming, embedded systems, and game development. They offer low-level control, high performance, and efficient memory management.

Features:

  • Pointers for direct memory manipulation
  • Static typing for strong type checking
  • Compiled language for efficient execution

Code Snippet:

1
2
3
4
5
6
7
8
9
10
11
#include <iostream>

int main() {
int num = 10;
int* ptr = &num; // Pointer usage
std::cout << "The value of num: " << *ptr << std::endl; // Dereferencing the pointer
static int count = 0; // Static typed variable
count++;
std::cout << "Count: " << count << std::endl;
return 0;
}

Java:

Java is a versatile, object-oriented language known for its platform independence, robustness, and large ecosystem of libraries and frameworks. It is used for enterprise-level applications, Android development, and building large-scale systems.

Features:

  • Object-oriented programming paradigm
  • Interfaces for achieving abstraction and multiple inheritance
  • Inheritance for code reuse and polymorphism

Code Snippet:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
interface Animal {
void sound();
}

class Dog implements Animal {
public void sound() {
System.out.println("Woof!");
}
}

public class Main {
public static void main(String[] args) {
Animal myDog = new Dog();
myDog.sound();
}
}

Python:

Python is a versatile language known for its simplicity, readability, and extensive library support. It is used in web development, data analysis, machine learning, and scientific computing.

Features:

  • Numpy library for numerical computations and array manipulation
  • TensorFlow library for machine learning and deep learning

Code Snippet:

1
2
3
4
5
6
7
8
9
10
11
12
import numpy as np
import tensorflow as tf

# Numpy usage
arr = np.array([1, 2, 3, 4, 5])
print("Array:", arr)

# TensorFlow usage
x = tf.constant(5)
y = tf.constant(3)
z = tf.add(x, y)
print("Result:", z)

Some Newer Languages

Rust:

Rust is a systems programming language that emphasizes safety, performance, and concurrency. It provides memory safety without sacrificing low-level control, making it suitable for developing secure and efficient software.

Features:

  • Ownership system for managing memory and preventing data races
  • References for borrowing data without transferring ownership

Code Snippet:

1
2
3
4
5
6
7
8
9
fn main() {
let mut num = 10;
let ref_num = &num; // Reference
let mut mutable_ref = &mut num; // Mutable reference

*mutable_ref += 5; // Modify value through mutable reference

println!("Value of num: {}", *ref_num);
}

Go:

Go, also known as Golang, is designed for simplicity, scalability, and concurrency. It excels in concurrent programming and is widely used in network programming, distributed systems, and cloud computing.

Features:

  • Goroutines for lightweight concurrent execution
  • Channels for communication and synchronization between goroutines

Code Snippet:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
package main

import (
"fmt"
"time"
)

func printMessage(msg string) {
for i := 1; i <= 5; i++ {
fmt.Println(msg)
time.Sleep(time.Millisecond * 500)
}
}

func main() {
go printMessage("Hello")
go printMessage("World")

time.Sleep(time.Second * 3)
}

TypeScript:

TypeScript is a superset of JavaScript that adds static typing and additional features for large-scale applications. It helps catch errors during development and enables better tooling and code organization.

Features:

  • Emphasis on strong typing and type annotations
  • Enhanced IDE support and code refactoring
  • Compatibility with existing JavaScript codebases

Code Snippet:

1
2
3
4
5
6
function addNumbers(a: number, b: number): number {
return a + b;
}

const result: number = addNumbers(5, 10);
console.log("Result:", result);

UI Languages

Swift:

Swift is a modern programming language developed by Apple for iOS, macOS, watchOS, and tvOS app development. It combines powerful features with a clean syntax and provides frameworks like SwiftUI for building intuitive user interfaces.

Features:

  • SwiftUI for declarative UI development
  • Strong type inference and safety
  • Automatic memory management

Code Snippet:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
import SwiftUI

struct ContentView: View {
var body: some View {
Text("Hello, SwiftUI!")
.font(.largeTitle)
.foregroundColor(.blue)
}
}

struct ContentView_Previews: PreviewProvider {
static var previews: some View {
ContentView()
}
}

Kotlin:

Kotlin is a statically-typed language that runs on the Java Virtual Machine (JVM). It is officially supported for Android development and offers modern features, concise syntax, and seamless interoperability with existing Java code.

Features:

  • Seamless integration with Java codebase
  • Android development support
  • Null safety and concise syntax

Code Snippet:

1
2
3
4
5
6
7
8
9
import android.os.Bundle
import androidx.appcompat.app.AppCompatActivity

class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
}
}

Dart:

Dart is a language developed by Google, commonly used for building mobile, web, and desktop applications. It is the primary language for developing Flutter applications, which allows for cross-platform development.

Features:

  • Flutter framework for building cross-platform apps
  • Hot reload for fast development
  • Asynchronous programming support

Code Snippet:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
import 'package:flutter/material.dart';

void main() {
runApp(MyApp());
}

class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
appBar: AppBar(
title: Text('Flutter App'),
),
body: Center(
child: Text('Hello, Flutter!'),
),
),
);
}
}

HTML:

HTML is the standard markup language for creating web pages. It provides the structure and elements necessary for presenting content on the internet.

Code Snippet:

1
2
3
4
5
6
<!DOCTYPE html>
<html>
<body>
<h1>Hello, World!</h1>
</body>
</html>

CSS:

CSS is a style sheet language used for describing the presentation and formatting of HTML documents. It allows you to style and design web pages, adding visual appeal and responsiveness.

Code Snippet:

1
2
3
4
h1 {
color: blue;
font-size: 20px;
}

Verilog:

Verilog is a hardware description language used for designing and simulating digital circuits. It is commonly used in digital design, FPGA programming, and ASIC development.

Code Snippet:

1
2
3
module AndGate(input a, b, output y);
assign y = a & b;
endmodule

MATLAB:

MATLAB is a high-level programming language used for numerical computing, data analysis, and visualization. It provides a rich set of tools and libraries for mathematical operations and scientific research.

Code Snippet:

1
2
3
4
A = [1 2 3; 4 5 6; 7 8 9];
B = [9 8 7; 6 5 4; 3 2 1];
C = A * B;
disp(C);

Assembly:

Assembly language is a low-level programming language that provides a one-to-one correspondence between instructions and machine code. It is used for low-level programming, embedded systems, and optimizing critical code sections.

Code Snippet (x86 Assembly):

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
section .text
global _start

_start:
mov eax, 4
mov ebx, 1
mov ecx, message
mov edx, 13
int 0x80

mov eax, 1
xor ebx, ebx
int 0x80

section .data
message db 'Hello, Assembly!', 0x0a

SQL:

SQL (Structured Query Language) is used for managing relational databases. It allows the manipulation and retrieval of data, creation of tables, and performing various database operations.

Code Snippet:

1
2
3
4
5
6
7
8
9
10
11
CREATE TABLE employees (
id INT PRIMARY KEY,
name VARCHAR(50),
age INT,
department VARCHAR(50)
);

INSERT INTO employees (id, name, age, department)
VALUES (1, 'John Doe', 30, 'IT');

SELECT * FROM employees WHERE department = 'IT';

LaTeX:

LaTeX is a typesetting system used for document preparation. It is widely used in academia, research, and scientific publications for producing professional-looking documents with complex mathematical formulas and structures.

Code Snippet:

1
2
3
4
5
6
7
8
9
\documentclass{article}
\begin{document}
\section{Introduction}
Hello, LaTeX!

\section{Equations}
The quadratic formula is given by:
\[ x = \frac{-b \pm \sqrt{b^2-4ac}}{2a} \]
\end{document}

Markdown:

Markdown is a lightweight markup language used for creating formatted documents with minimal effort. It is commonly used for writing documentation, readme files, and online content.

Code Snippet:

1
2
3
4
5
6
7
8
# Heading 1
## Heading 2

- List item 1
- List item 2

**Bold text**
*Italic text*

Shell Script:

Shell scripting allows automating tasks and running commands in a Unix/Linux shell environment. It is used for scripting repetitive tasks, managing files, and creating command-line tools.

Code Snippet:

1
2
3
4
5
6
#!/bin/bash
echo "Hello, Shell Scripting!"

for i in {1..5}; do
echo "Iteration: $i"
done

Conclusion:
In this video, we covered the fundamental programming languages that every CS/EECS major should be familiar with. Each language has its unique features, use cases, and historical significance. By expanding your knowledge and proficiency in these languages, you’ll gain the skills necessary to excel in various domains of computer science and engineering.

Remember, the best way to learn a programming language is through practice and real-world applications

. So, pick a language that aligns with your interests and start exploring the vast possibilities it offers. Happy coding!

Compiled and Interpreted Languages, Static-Typed and Dynamically-Typed Languages, and Scripting Languages

In this article, I will share my insights and experiences regarding three important aspects of programming languages: compiled vs. interpreted, static-typed vs. dynamically-typed, and scripting languages. Understanding these concepts is essential for every aspiring programmer and can greatly influence the development process and overall code execution.

  1. Compiled vs. Interpreted Languages:
    Compiled Languages:
    Compiled languages, such as C/C++ and Rust, require a separate compilation step before execution. The source code is translated into machine code specific to the target platform using a compiler. The resulting compiled code is then executed directly by the computer’s processor.

Key characteristics:

  • Performance: Compiled languages often deliver faster performance due to direct execution of machine code.
  • Portability: Compiled code needs to be recompiled for different platforms.
  • Build process: Compilation involves translating the entire source code into an executable file.

Interpreted Languages:
Interpreted languages, like Python and JavaScript, do not require a separate compilation step. The code is executed line by line by an interpreter at runtime. The interpreter translates and executes each statement on the fly.

Key characteristics:

  • Portability: Interpreted languages are typically more portable as the interpreter can be run on different platforms without modification.
  • Development speed: Interpreted languages usually have faster development cycles as there is no need for compilation.
  • Performance: Interpreted languages may have slightly slower performance due to the interpreter’s overhead.
  1. Static-Typed vs. Dynamically-Typed Languages:
    Static-Typed Languages:
    Static-typed languages, such as Java and C#, require explicit declaration of variable types at compile-time. Once a variable is assigned a specific type, it cannot be changed. The compiler enforces type checking during the compilation process.

Key characteristics:

  • Early error detection: Static typing helps catch type-related errors at compile-time, reducing runtime errors.
  • Readability and documentation: Type annotations provide clarity and make code more self-documenting.
  • Performance optimization: Static typing allows for certain performance optimizations by the compiler.

Dynamically-Typed Languages:
Dynamically-typed languages, like Python and JavaScript, do not require explicit type declarations. Variables are bound to values at runtime, and their types can change throughout the program’s execution.

Key characteristics:

  • Flexibility: Dynamically-typed languages allow for more flexibility in terms of variable usage and type conversions.
  • Rapid prototyping: Dynamic typing enables quick prototyping and experimentation due to reduced overhead in type declarations.
  • Late error detection: Type-related errors may surface during runtime, which requires thorough testing and careful error handling.
  1. Scripting Languages:
    Scripting languages, such as Perl and Ruby, are designed primarily for scripting tasks and automation. They often have simpler syntax, higher-level abstractions, and focus on ease of use and productivity.

Key characteristics:

  • Rapid development: Scripting languages prioritize fast development cycles and provide convenient features for automating tasks.
  • High-level abstractions: Scripting languages often offer built-in functions and libraries for common operations, reducing the need for low-level coding.
  • Interoperability: Many scripting languages are easily integratable with other systems and programming languages, allowing for seamless collaboration.

Conclusion:
Understanding the differences between compiled and interpreted languages, static-typed and dynamically-typed languages, and the role of scripting languages is crucial for choosing the right tool for the task at hand. Each type has its own advantages and considerations regarding performance, development speed, and error detection. By expanding your knowledge in these areas, you’ll be better equipped to make informed decisions and write efficient, reliable code in your programming journey.

Remember, the choice of language ultimately depends on the project requirements, team preferences, and the problem you aim to solve. Experiment with different languages, explore their ecosystems, and keep learning to become a versatile and skilled programmer. Happy coding!

Influence of IDE on languages

The influence of Integrated Development Environments (IDEs) on programming languages, especially regarding static-typed and dynamically-typed languages, is significant. IDEs provide developers with a comprehensive set of tools and features that enhance productivity, code quality, and development experience. Let’s explore how IDEs impact these two types of programming languages:

  1. Static-Typed Languages:
    IDEs play a crucial role in static-typed languages by leveraging their strong type systems and providing advanced features for type checking, code navigation, and refactoring.

a. Type Checking:
IDEs for static-typed languages, such as Java or C#, often include sophisticated type checkers that analyze code and detect type-related errors during development. These type checkers highlight potential issues, such as incompatible assignments or incorrect method invocations, helping developers catch errors early on and maintain code integrity.

b. Code Navigation and Auto-completion:
IDEs assist developers in navigating through large codebases by offering features like code auto-completion and intelligent code suggestions. In static-typed languages, IDEs leverage type information to suggest available methods, properties, and variables, improving code efficiency and reducing the likelihood of syntax errors.

c. Refactoring:
Static-typed languages allow for extensive refactoring capabilities, thanks to their strong type systems. IDEs provide powerful refactoring tools that can rename variables, extract methods, rearrange code structures, and more. These tools ensure that the necessary changes are applied consistently throughout the codebase, reducing human error and improving maintainability.

  1. Dynamically-Typed Languages:
    IDEs for dynamically-typed languages, like Python or JavaScript, have also made significant progress in recent years, providing valuable features that assist developers in working with these languages effectively.

a. Type Inference and Documentation:
Although dynamically-typed languages lack explicit type declarations, modern IDEs use advanced type inference algorithms to determine variable types. This helps provide accurate code suggestions and improves code navigation. Additionally, IDEs often integrate with type hinting systems (e.g., Python’s type hints) and offer inline documentation, enhancing code understanding and developer productivity.

b. Code Analysis and Debugging:
IDEs for dynamically-typed languages often include powerful code analysis and debugging tools. They can detect potential runtime errors, highlight common coding mistakes, and provide insights into the behavior of dynamic code. This helps developers identify issues early on and ensure robustness in their applications.

c. Test Framework Integration:
IDEs facilitate the integration of testing frameworks, allowing developers to write and execute tests directly within the IDE. This streamlined workflow improves the efficiency of testing dynamic code, enabling developers to iterate quickly and maintain code quality.

Overall, IDEs have a significant impact on both static-typed and dynamically-typed languages. They enhance the development experience, improve code quality, and enable developers to leverage the features and strengths of each language type effectively. Regardless of the language’s typing system, using a feature-rich IDE empowers developers to write cleaner, more maintainable code, and increases productivity throughout the development process.

let vs. Type

Modern programming languages often adopt the syntax of using type annotations, such as let value: Double = 1.0, instead of the traditional approach of double v = 3.0 for several reasons:

  1. Readability and Clarity:
    Using type annotations with variable declarations enhances code readability and clarity. By explicitly stating the variable type, it becomes easier for developers, including oneself and others, to understand the purpose and expected data type of a variable. This can greatly improve code comprehension and reduce ambiguity.

  2. Self-Documenting Code:
    Type annotations serve as a form of self-documentation within the code. By including the type directly in the variable declaration, it eliminates the need to search for variable type definitions elsewhere in the codebase. This can be especially helpful when working with larger projects or collaborating with other developers, as it provides immediate context and understanding of the variable’s intended type.

  3. Early Error Detection:
    Type annotations facilitate early error detection during compilation. When a variable is explicitly annotated with a specific type, the compiler can perform type checking and flag potential type-related errors before the code is executed. This helps catch issues early in the development process, reducing the likelihood of runtime errors and improving overall code robustness.

  4. Tooling Support:
    Modern IDEs and code editors often provide powerful code analysis and autocomplete features that rely on type annotations. With type annotations, the tooling can offer more accurate and context-aware suggestions, reducing the chances of introducing bugs or relying on incorrect assumptions about variable types.

  5. Improved Maintainability:
    Type annotations contribute to code maintainability by explicitly specifying the expected types. This can help in code maintenance and refactoring tasks, as changes to variable types can be accurately tracked and updated throughout the codebase. It also enables developers to identify potential type-related issues when modifying or extending existing code.

  6. Language Flexibility:
    By using type annotations, programming languages gain flexibility in terms of supporting different type systems, including static typing, type inference, and gradual typing. Type annotations allow languages to accommodate a wide range of coding styles and preferences while still providing the benefits of explicit type information.

Overall, the adoption of type annotations in modern programming languages aims to improve code readability, catch errors early, enhance tooling support, and promote code maintainability. By providing explicit type information, developers can write more robust and self-explanatory code, leading to more efficient development and fewer unexpected issues down the line.

Type

Let’s compare the usage of type annotations and the concept of constant or immutability in various programming languages, including C/C++, Java, Python, JavaScript, TypeScript, Go, Rust, Swift, Kotlin, and Dart.

  1. C/C++:
    Without explicit type:
    1
    2
    int num = 5;
    float pi = 3.14;

With explicit type:

1
2
int num = 5;
float pi = 3.14;

C and C++ are statically-typed languages, where the type of a variable is explicitly declared. The language does not have built-in support for type annotations, and the programmer is responsible for explicitly specifying the types. C++ introduces the auto keyword and decltype specifier for type inference.

  1. Java:
    Without explicit type:
    1
    2
    int num = 5;
    float pi = 3.14;

With explicit type:

1
2
int num = 5;
float pi = 3.14;

Java is also a statically-typed language, and variable types must be explicitly declared. Java does not support type inference or type annotations in the same way as newer languages.

  1. Python:
    Without type hinting:
    1
    2
    num = 5
    pi = 3.14

With type hinting:

1
2
num: int = 5
pi: float = 3.14

Python is a dynamically-typed language that traditionally does not require explicit type annotations. However, starting from Python 3.5, it introduced optional type hinting through type annotations using the : syntax. Type hinting helps with code readability and static analysis but is not enforced at runtime.

  1. JavaScript:
    Without explicit type:
    1
    2
    let num = 5;
    let pi = 3.14;

With explicit type (TypeScript):

1
2
let num: number = 5;
let pi: number = 3.14;

JavaScript is also a dynamically-typed language, and variable types are not explicitly declared. However, with the introduction of TypeScript, a statically-typed superset of JavaScript, explicit type annotations can be used to provide type information for variables.

  1. TypeScript:
    Without explicit type:
    1
    2
    let num = 5;
    let pi = 3.14;

With explicit type:

1
2
let num: number = 5;
let pi: number = 3.14;

TypeScript is a statically-typed language and requires explicit type annotations for variables. It extends JavaScript by adding optional static typing, allowing developers to catch type-related errors during development.

  1. Go:
    Without explicit type:
    1
    2
    num := 5
    pi := 3.14

With explicit type:

1
2
var num int = 5
var pi float64 = 3.14

Go supports type inference, allowing variables to be declared without explicitly mentioning the type. The type is inferred based on the assigned value. However, explicit type declarations can be used for clarity or when type inference is not desired.

  1. Rust:
    Without explicit type:
    1
    2
    let num = 5;
    let pi = 3.14;

With explicit type:

1
2
let num: i32 = 5;
let pi: f64 = 3.14;

Rust supports type inference similar to Go, where variables can be declared without explicitly mentioning the type. However, explicit type annotations can be used when desired.

  1. Swift:
    Without explicit type:
    1
    2
    let num = 5
    let pi = 3.14

With explicit type:

1
2
3
4
let num: Int = 5


let pi: Double = 3.14

Swift supports type inference, and variables can be declared without explicitly mentioning the type. However, explicit type annotations can be used for clarity or when type inference is not desired.

  1. Kotlin:
    Without explicit type:
    1
    2
    val num = 5
    val pi = 3.14

With explicit type:

1
2
val num: Int = 5
val pi: Double = 3.14

Kotlin, like Swift, supports type inference and allows variables to be declared without explicitly mentioning the type. However, explicit type annotations can be used when needed.

  1. Dart:
    Without explicit type:
    1
    2
    var num = 5;
    var pi = 3.14;

With explicit type:

1
2
int num = 5;
double pi = 3.14;

Dart supports type inference, and variables can be declared without explicitly mentioning the type. However, explicit type annotations can be used for clarity or when type inference is not desired.

Regarding the concept of constant or immutability, each language has its own way of defining and enforcing immutability, such as using const or val keywords. These keywords indicate that the value of a variable cannot be changed once assigned. The usage and syntax for constants or immutability may vary between languages, but the concept generally serves to ensure data integrity and improve code reliability by preventing unintentional modifications to variables.

Mutability and Immutability

Certainly! Let’s explore examples of mutability and immutability in variables and function parameters in various programming languages:

  1. C/C++:
    Variable mutability:

    1
    2
    int mutableVariable = 5; // Mutable variable
    const int immutableVariable = 10; // Immutable variable

    Function parameter mutability:

    1
    2
    3
    4
    5
    6
    7
    void mutableFunction(int mutableParam) { // Mutable parameter
    // Code here
    }

    void immutableFunction(const int immutableParam) { // Immutable parameter
    // Code here
    }
  2. Java:
    Variable mutability:

    1
    2
    int mutableVariable = 5; // Mutable variable
    final int immutableVariable = 10; // Immutable variable

    Function parameter mutability:

    1
    2
    3
    4
    5
    6
    7
    void mutableFunction(int mutableParam) { // Mutable parameter
    // Code here
    }

    void immutableFunction(final int immutableParam) { // Immutable parameter
    // Code here
    }
  3. Python:
    Variable mutability:

    1
    mutableVariable = 5  # Mutable variable

    Function parameter mutability:

    1
    2
    def mutable_function(mutable_param):  # Mutable parameter
    # Code here
  4. JavaScript:
    Variable mutability:

    1
    2
    let mutableVariable = 5; // Mutable variable
    const immutableVariable = 10; // Immutable variable

    Function parameter mutability:

    1
    2
    3
    function mutableFunction(mutableParam) { // Mutable parameter
    // Code here
    }
  5. TypeScript:
    Variable mutability:

    1
    2
    let mutableVariable: number = 5; // Mutable variable
    const immutableVariable: number = 10; // Immutable variable

    Function parameter mutability:

    1
    2
    3
    function mutableFunction(mutableParam: number) { // Mutable parameter
    // Code here
    }
  6. Go:
    Variable mutability:

    1
    2
    var mutableVariable int = 5 // Mutable variable
    const immutableVariable int = 10 // Immutable variable

    Function parameter mutability:

    1
    2
    3
    func mutableFunction(mutableParam int) { // Mutable parameter
    // Code here
    }
  7. Rust:
    Variable mutability:

    1
    2
    let mutable_variable = 5; // Mutable variable
    let immutable_variable = 10; // Immutable variable

    Function parameter mutability:

    1
    2
    3
    4
    5
    6
    7
    fn mutable_function(mutable_param: mut i32) { // Mutable parameter
    // Code here
    }

    fn immutable_function(immutable_param: i32) { // Immutable parameter
    // Code here
    }
  8. Kotlin:
    Variable mutability:

    1
    2
    var mutableVariable = 5 // Mutable variable
    val immutableVariable = 10 // Immutable variable

    Function parameter mutability:

    1
    2
    3
    fun mutableFunction(mutableParam: Int) { // Mutable parameter
    // Code here
    }
  9. Swift:
    Variable mutability:

    1
    2
    var mutableVariable = 5 // Mutable variable
    let immutableVariable = 10 // Immutable variable

    Function parameter mutability:

    1
    2
    3
    4
    5
    6
    7
    8
    9
    func mutableFunction(mutableParam: Int) { // Mutable parameter
    //

    Code here
    }

    func immutableFunction(immutableParam: inout Int) { // Immutable parameter
    // Code here
    }
  10. Dart:
    Variable mutability:

    1
    2
    var mutableVariable = 5; // Mutable variable
    final immutableVariable = 10; // Immutable variable

    Function parameter mutability:

    1
    2
    3
    4
    5
    6
    7
    void mutableFunction(var mutableParam) { // Mutable parameter
    // Code here
    }

    void immutableFunction(final immutableParam) { // Immutable parameter
    // Code here
    }

In each of these languages, mutable variables can be reassigned or modified, whereas immutable variables cannot be changed after their initial assignment. Similarly, mutable function parameters can be modified within the function, while immutable function parameters are read-only and cannot be modified within the function body. The syntax and keywords used to denote immutability may vary between languages, but the concept remains consistent.

  • Post title:ProgrammingLanguages.md
  • Post author:Meqt
  • Create time:2023-06-13 10:52:50
  • Post link:https://meqtmac.github.io/2023/06/13/ProgrammingLanguages-md/
  • Copyright Notice:All articles in this blog are licensed under BY-NC-SA unless stating additionally.