Versus
    32-bit vs 64-bit
    Annotations vs Decorators
    BigQuery vs Bigtable
    Block Storage vs File Storage vs Object Storage
    C vs C++
    Canvas vs SVG
    Constructor vs Init() vs Factory
    Containers vs Virtual Machines (VMs)
    DOM vs Virtual DOM vs Shadow DOM
    DQL vs DDL vs DCL vs DML
    Dagger vs Guice
    Data Mining vs Machine Learning vs Artificial Intelligence vs Data Science
    Flux vs Redux
    GCP API Gateway vs Cloud Endpoint
    GCP Cloud Run vs Cloud Functions vs App Engine
    GCP DataFlow vs Dataproc
    Google Analytics 4 vs Universal Analytics
    Google Internal vs Open Source
    HEIC vs HEIF vs HEVC vs JPEG
    Java vs C++
    Jetty vs Netty
    Kotlin vs Java
    LLVM vs JVM
    Linux vs BSD
    Microcontroller vs Microprocessor vs Computer
    Node.js vs Erlang
    POSIX vs SUS vs LSB
    Pass-by-value vs Pass-by-reference
    Proto2 vs Proto3
    PubSub vs Message Queue
    REST vs SOAP
    React vs Flutter vs Angular
    Rust vs C++
    SLI vs SLO vs SLA
    SRAM vs DRAM
    SSD vs HDD
    Software Engineer vs Site Reliability Engineer
    Spanner vs Bigtable
    Stack based VM vs Register based VM
    Stateless vs Stateful
    Static Site Generation vs Server-side Rendering vs Client-side Rendering
    Strong Consistency vs Eventual Consistency
    Subroutines vs Coroutines vs Generators
    Symlinks vs Hard Links
    TCP vs UDP
    Tensorflow vs PyTorch
    Terminal vs Shell
    Vi vs Vim vs gVim vs Neovim
    WAL vs rollback journal
    gtag vs Tag Manager
    stubs vs mocks vs fakes

Pass-by-value vs Pass-by-reference

Updated: 2022-04-25
  • pass-by-value: passing a copy of the variable to a function.
  • pass-by-reference: passing an alias of the variable to a function.
  • "pass-by-pointer": actually pass-by-value, it is copying the value of the pointer (the address), into the function, and then dereferencing it to get the value.

Languages with both pass-by-value and pass-by-reference

C++:

Declaration Invocation
pass-by-value void f(int x) f(x)
pass-by-reference void f(int& x) f(x)
pass-by-pointer void f(int* x) f(&x)
pass-by-pointer (array) void f(int* arr) f(arr)

Languages with pass-by-value only

Java, JavaScript, C, Rust and most other languages do not support any form of pass-by-reference.

In C++ a reference is an alias for another variable. C doesn’t have this concept (so essentially everything in C is pass-by-value).

Similarly Java is always pass-by-value, or "Object references are passed by value".

Pass by Value

The argument’s value is copied into the value of the corresponding function parameter.

void foo(int x) {
  // ...
}

int main() {
  int x = 1;
  foo(x);
}

Pass by Reference

A reference is same object, just with a different name and reference must refer to an object.

void swap(int& x, int& y) {
  int tmp = x;
  x = y;
  y = tmp;
}

int main() {
  int a = 10, 20;
  swap(a, b);
}

Pass by Pointer

void swap(int* x, int* y) {
  int tmp = *x;
  *x = *y;
  *y = tmp;
}

int main() {
  int a = 10, 20;
  swap(&a, &b);
}

Array:

// Definition
double getAverage(int *arr, int size) {}

// call the function
int a[5] = {1000, 2, 3, 17, 50};
avg = getAverage(a, 5);