iTranslated by AI

The content below is an AI-generated translation. This is an experimental feature, and may contain errors. View original article

Pass by Value, Pass by Pointer, Pass by Object Sharing, and Pass by Reference

に公開

I have created a comparison table by language for how formal parameters can handle variables passed as actual arguments, along with explanations of related terms.
I hope this helps your understanding of pass-by-value and pass-by-reference.

Comparison Table by Language

\ Passing Method
  \
Language \
Pass by Value
(Value Copy)
 
Pass by Pointer
(Pointer Value Copy
Value Sharing)
Pass by Value of Reference
(Reference Value Copy
Object Sharing)
Pass by Reference
(No Copy
Variable Sharing)
C# Supported
(int arg)
Supported
(int *arg)
Supported
(Object arg)
Supported
(ref int arg)
Swift Supported
(arg: Int)
Supported Supported Supported
(arg: inout Int)
PHP Supported
(int $arg)
Not Supported Supported
(object $arg)
Supported
(int &$arg)
VB.NET Supported
(ByVal arg
As Integer)
Not Supported Supported
(ByVal arg
As Object)
Supported
(ByRef arg
As Integer)
Rust Supported
(arg: i32)
Supported
(arg: *i32)
Not Supported Supported
(arg: &i32)
C++ Supported
(int arg)
Supported
(int *arg)
Not Supported Supported
(int &arg)
C Supported
(int arg)
Supported
(int *arg)
Not Supported Not Supported
Go Supported
(arg int)
Supported
(arg *int)
Supported
(arg []int)
Not Supported
Java
JavaScript
TypeScript
Supported
(int arg)
Not Supported Supported
(Object arg)
Not Supported
Python Not Supported Not Supported Supported
(arg: int)
Not Supported
Ruby Not Supported Not Supported Supported
(arg: Integer)
Not Supported

Variable Types

Variables are broadly classified into two types: "Value-type variables" and "Reference-type variables."

Value-type Variables

  • The variable holds the value itself.
  • The assigned value and the value held by the variable are identical.

Pointer-type Variables (A type of value-type variable)

  • The pointed-to value (pointee) exists somewhere in memory, and the variable (pointer) holds a pointer value (e.g., a memory address) to point to that value.
  • Since you assign a pointer value to the variable, the assigned value and the value held by the variable are identical (value-type variable).
  • Depending on the language, you can increment pointers or access them as arrays to point to different values (Dangerous).

Reference-type Variables (Object-type variables)

  • The referenced object exists somewhere in memory, and the variable holds a reference value to the assigned object to reference it.
    • The language itself determines what the reference value is.
  • The assigned value and the value held by the variable are different.

Which data types are value-type or reference-type variables depends on the language.

Language\Data Type Numeric Array Pointer Struct Object
C, C++ Value type Neither Value type Value type None
C# Value type Reference type Value type Value type Reference type
PHP Value type Value type None None Reference type
Java, JavaScript Value type Reference type None None Reference type
Python, Ruby Reference type Reference type None None Reference type

Types of Function Arguments

Actual Argument (Value or Variable)

  • A value (numerical value, pointer value, object, etc.) or a variable passed by the caller.
function(123);    // Passing a value
int value = 123;
function(value);  // Passing a variable

When passing a variable, there are two methods: "Pass by Value," which passes the value of the variable, and "Pass by Reference," which passes the variable itself (referencing the calling variable).

Formal Parameter (Variable)

  • A variable received by the called function.
  • It is also possible to assign a value to a formal parameter within the function.

Types of Argument Passing Methods

When a variable is specified as an actual argument, it is classified into two types: "Pass by Value" and "Pass by Reference."
Passing the "value" of the variable is "Pass by Value," while passing the "variable" itself (referencing the caller's variable) is "Pass by Reference."
If a value is specified as an actual argument, it is equivalent to pass-by-value, and pass-by-reference will result in a compilation or runtime error.

Pass by Value (call by value, pass by value)

  • Passes the value held by the variable to the formal parameter.
    • Value-type variables pass a copy of the value.
      • Changing the value within the function does not affect the caller.
    • Reference-type variables pass a copy of the reference value and share the value (Pass by Value of Reference).
      • Changing the value within the function affects the caller.
  • Actual arguments and formal parameters are independent variables.
    • Assigning to a formal parameter does not affect the actual argument.

Pass by Pointer (A type of pass-by-value, call by address)

  • Passes a copy of the pointer value (e.g., a memory address) held by the pointer-type variable to the formal parameter.
  • Actual arguments and formal parameters are independent variables.
    • Assigning to a formal parameter does not affect the actual argument.
  • The pointed-to value is shared.
    • Changing the pointed-to value affects the caller.

Pass by Value of Reference (A type of pass-by-value, call by object, call by object-sharing)

  • Passes a copy of the reference value held by the reference-type variable to the formal parameter.
  • Actual arguments and formal parameters are independent variables.
    • Assigning to a formal parameter does not affect the actual argument.
  • The referenced object is shared.
    • Changing the content of the referenced object affects the caller.

Pass by Reference (call by reference, pass by reference)

  • Passes reference information for the actual argument (the caller's variable) and shares the variable with the formal parameter.
    • The language itself determines what reference information is passed and how the variable is shared.
  • Actual arguments and formal parameters are the same variable (alias).
    • Assigning to the formal parameter results in an assignment to the actual argument (the caller's variable).

Verification Programs

Pass-by-Value, Pass-by-Pointer, and Pass-by-Reference for Value-type Variables

C++
#include <stdio.h>

void increment(int w, int *x, int *y, int &z) { // w is pass-by-value, x, y are pass-by-pointer, z is pass-by-reference
    w++;     // Increment the formal parameter (copied value)
    x++;     // Increment the formal parameter (copied memory address value)
    (*y)++;  // Increment the shared value at the pointer destination
    z++;     // Increment the formal parameter (value of the shared variable)
}

int main() {
    int a = 1;
    int b = 1;
    int c = 1;
    int d = 1;
    increment(a, &b, &c, d);  // a, d specify variables; &b, &c specify values (memory address values)
    printf("%d %d %d %d\n", a, b, c, d);  // 1 1 2 2
}

Pass-by-Value and Pass-by-Reference for Pointer-type Variables

C++
#include <stdio.h>

void increment(int *x, int *&y) {  // x is pass-by-value, y is pass-by-reference
    (*x)++;  // Increment the value of the shared int variable
    x++;     // Increment the copied pointer-type variable
    (*y)++;  // Increment the value of the shared int variable
    y++;     // Increment the shared pointer-type variable
}

int main(void) {
    int values[] = {1, 22, 333};
    int *p0 = &values[0];
    int *p1 = &values[1];
    increment(p0, p1);
    printf("%d %d\n", values[0], *p0);  // 2 2
    printf("%d %d\n", values[1], *p1);  // 23 333
}

Pass-by-Value-of-Reference and Pass-by-Reference for Reference-type Variables

PHP
<?php

function increment(object $x, object $y, object &$z) {  // x and y are pass-by-value-of-reference, z is pass-by-reference
    $x->value = $x->value + 1;                // Assignment within the shared object
    $y = (object)['value' => $y->value + 1];  // Assignment to the formal parameter (independent variable)
    $z = (object)['value' => $z->value + 1];  // Assignment to the formal parameter (shared variable)
}

$a = (object)['value' => 1];
$b = (object)['value' => 1];
$c = (object)['value' => 1];
increment($a, $b, $c);
echo "{$a->value} {$b->value} {$c->value}\n";  // 2 1 2

Information Processing Examination Example Questions

In Information Processing Examinations, "call by value" is expressed as "値呼出し" (call by value) and "call by reference" as "参照呼出し" (call by reference).

https://www.ap-siken.com/kakomon/25_haru/q20.html

Result Verification Program

C++
#include <stdio.h>

void add(int X, int& Y) {  // Value-type variable; formal parameter X is call-by-value, Y is call-by-reference
    X = X + Y;
    Y = X + Y;
}

int main(){
    int X, Y;  // Value-type variables

    X = 2;
    Y = 2;
    add(X, Y);
    
    printf("%d %d\n", X, Y);
}
Execution Result
2 6

Summary

There are many blogs and articles that explain passing a reference to an object as "pass by reference," but this explanation differs from the term "pass by reference" as used in Information Processing terminology.

  • "Passing a reference" (pass a reference) to a value (object) results in "sharing the value."
  • "Passing a variable by reference" (pass by reference) results in "sharing the variable."

In Information Processing terminology, "pass by value" and "pass by reference" are terms used when passing variables as function arguments. There are four combinations based on two types of variables and two methods of passing:

  1. Pass-by-value for value-type variables
  2. Pass-by-reference for value-type variables
  3. Pass-by-value for reference-type variables
  4. Pass-by-reference for reference-type variables

Do you correctly understand and can you explain which of these your programming language supports?
I hope you will find this useful as a reference to understand the original usage of the terms and help clarify things when you find yourself out of sync with the person you are talking to.

Discussion