Posts for: #C

Pass by Value or Reference

1. Python

def test_function(x, y):
    x += 1
    y.append(4)

def main():
    a = 1
    b = [2, 3]

    test_function(a, b)
    print("a =", a)
    print("b =", b)

main()

Output:

a = 1
b = [2, 3, 4]
  • If you pass a mutable object into a method, the method gets a reference to that same object and you can mutate it to your heart’s delight, but if you rebind the reference in the method, the outer scope will know nothing about it, and after you’re done, the outer reference will still point at the original object.

阅读更多

Data Types in Programming Languages

Data Types in Programming Languages

Programs are collections of instructions that manipulate data to produce a desired result.

1. Javascript

As you have know that javascript is a dynamic language from previous post, which means the values have types, not variables.

Javascript value types can be categorized into two main categories: objects and primitives. Among these types, the objects are mutable, which means their values can be modified or changed after they are created. On the other hand, the primitive types are immutable, meaning their values cannot be changed once they are assigned.

阅读更多

Typing in Programming Language

1. Compiled vs interpreted language

Programming languages are for humans to read and understand. The program (source code) must be translated into machine language so that the computer can execute the program. The time when this translation occurs depends on whether the programming language is a compiled language or an interpreted language. Instead of translating the source code into machine language before the executable file is created, an interpreter converts the source code into machine language at the same time the program runs. So you can’t say a language doesn’t have compilation step, because any language needs to be translated to machine code.

阅读更多

Typing in Programming Language - Example

Previous post: Typing in Programming Language - David’s Blog

1. Javascript

JavaScript is a dynamic language with dynamic types. Variables in JavaScript are not directly associated with any particular value type, and any variable can be assigned (and re-assigned) values of all types:

let foo = 42; // foo is now a number
foo = "bar"; // foo is now a string
foo = true; // foo is now a boolean

This is in dynamic languages (dynamically typing), values have types, not variables.

阅读更多

C Go Java Python内存结构及对比

1. Javascript

Taken from JavaScript | MDN but this applies for all language with GC:

Low-level languages like C, have manual memory management primitives such as malloc() and free(). In contrast, JavaScript automatically allocates memory when objects are created and frees it when they are not used anymore (garbage collection). This automaticity is a potential source of confusion: it can give developers the false impression that they don’t need to worry about memory management.

阅读更多