Pass by value — explained

A simple example to show how Kotlin and Java pass arguments by value to functions and not by reference.

Photo by Lalaine Macababbad on Unsplash
Temp ref = new Temp();
nuller(ref);
System.out.println(ref); // Is not null
void nuller(Temp value) {
value = null;
}
void swap(int& x, int& y) 
{
int x1 = x;
x = y;
y = x1;
}

int main()
{
int a = 9, b = 1;
cout << "a is " << a; // outputs: a is 9
swap(a, b);
cout << "a is " << a; // outputs: a is 1
}

Lead Developer at Qvik, Coach, Agile Thinker, GDG Lead.

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store