The Hidden Truth About Function Arguments in Programming Languages: Pass by Value vs Reference vs Assignment
Programming languages have different ways of handling function arguments, and understanding these differences is crucial for writing efficient and bug-free code.
Function arguments are the values that are passed to a function when it is called. These values can be used by the function to perform some operation and return a result. The way that function arguments are handled by a programming language can have a significant impact on the performance and behavior of the program.
fun name(argument1, argument2) { }
Passing arguments by value
In some programming languages, function arguments are passed by value. This means that the function receives a copy of the original variable, rather than a reference to it. Any changes made to the variable within the function will not be reflected in the original variable. In most cases those are primitives such as integer, double, float.
Python
str = "hello"
def process(str):
str = "world"
print(string) # world
process(str)
print(str) # hello
C++
void process(int x) {
x++; // this will change only local variable
}
int main() {
int y = 10;
process(y); // passed by value
cout << y << endl; // output: 10 and not 11
}
C#
static void Main(string[] args) {
int x = 5;
Console.WriteLine(x); // output: 5
process(x);
Console.WriteLine(x); // output: 5
}
static void process(int x) {
x = 10;
Console.WriteLine(x); // output: 10
}
Java
public class Person {
int age = 18;
void process (int age) {
age = a + 10;
}
public static void main (String[] args) {
var person = new Person();
System.out.println(person.age); // output: 10
eg.call (20);
System.out.println(person. Age); // output: 10
}
}
JavaScript
function process(num) {
num += 1;
}
let myNumber = 3;
process(myNumber);
console.log(myNumber); // Output: 3
Go Lang
func main() {
myNumber := 3
process(myNumber)
fmt.Println(myNumber) // Output: 3
}
func process(num int) {
num += 1
}
Passing arguments by reference
In other programming languages, function arguments are passed by reference. This means that the function receives a reference to the original variable, rather than a copy of its value. Any changes made to the variable within the function will be reflected in the original variable. In most cases those are non-primitives constructs such as array, objects
Python
def process(list):
list.append(4)
my_list = [1, 2, 3]
process(my_list)
print(my_list) # Output: [1, 2, 3, 4]
C++
void swap(int &x, int &y) {
int temp = x;
x = y;
y = temp;
}
int main() {
int a = 5;
int b = 10;
swap(a, b); // passes both variables by reference
cout << a << " " << b << endl; // output: 10 5
}
C#
static void Main(string[] args) {
int x = 5;
Console.WriteLine(x); // output: 5
process(ref x);
Console.WriteLine(x); // output: 10
}
static void process(ref int x) {
x = 10;
Console.WriteLine(x); // output: 10
}
Java
public static void main(String[] args) {
int[] myArray = {1, 2, 3};
process(myArray);
System.out.println(Arrays.toString(myArray)); // Output: [1, 2, 3, 4]
}
public static void process(int[] arr) {
arr[arr.length - 1] = 4;
}
JavaScript
let obj = {a: 10};
function process(x) {
x.a = 20;
}
console.log(obj); // output: {a: 10}
process(obj);
console.log(obj); // output: {a: 20}
Go Lang
func process(x *int) {
*x = 10
fmt.Println(*x) // output: 10
}
func main() {
x := 5
fmt.Println(x) // output: 5
process(&x)
fmt.Println(x) // output: 10
}
Passing arguments by assignment
When you pass an argument by assignment, the function receives an assignment of the argument value. This means that both the function parameter and the original variable in the calling code refer to the same object in memory. However, if either of them is reassigned to a different object, they become independent of each other.
Passing by assignment is useful when you want to share the same object between the function and the calling code, and when you want to support mutable and immutable types. It also simplifies the syntax and semantics of the language, as there is no need to distinguish between different modes of passing arguments.
However, passing by assignment can also be confusing and error-prone, as it can be hard to predict whether the function will modify the original object or create a new one. It also makes it difficult to implement some features such as copy constructors or move semantics, which rely on passing by value or by reference.
Python
def append(list):
list.append(4) # modifies both list and x
list = [1, 2] # only reassigns list
x = [3]
append(x) # passes x by assignment
print(x) # output: [3, 4], not [1, 2]
Conclusion
Passing arguments by value, by reference, or by assignment is an important concept in programming languages. It is important to understand the differences between methods in order to write efficient and effective code. When passing arguments by value, the original value of the argument is not affected by any changes made within the function. When passing arguments by reference, the original value of the argument is affected by any changes made within the function. The choice of passing mechanism depends on the requirements of the program and the data types being used.