Whats the difference between pass by reference and pass by value?

One of my favorite topics in programming is understanding pass-by-value and pass-by-reference. It is a concept that came up during one of my technical interviews and is one that you should know if you don’t already.

The difference between pass-by-reference and pass-by-value is, pass-by-value creates a new space in memory and makes a copy of a value, whereas pass-by-reference does not. Instead of making a copy, pass-by-reference does exactly what it sounds like; a value stored in memory gets referenced.

Short Way To Remember: Complex values are pass-by-reference and Primitive values are pass-by-value.

Pass-By-Reference:

Objects

To get started, I am going to start by making an object literal and assign it to the variable john. The object will contain simple properties such as name, age, and gender. Let’s create another variable called newJohn and set it equal to the john object.

At this point, you are probably thinking that these are now two separate objects for which you can now modify in any way you want.

NOOOO!!!!!

Remember when I said that instead of making a copy, a value in memory will be referenced?

The newJohn object is NOT a copy of the object stored inside of the john variable. Instead, the variable newJohn is referencing the original john object.

That means if you decide to edit thenewJohn object ( e.g changing the name property of newJohn on line 3 to newJohn0), you are in fact modifying the john object!

As you can see in lines 16 and 19, both objects’ name property has a value of newJohn0.

Arrays

Arrays work the same way as objects.

We will get the same result as we did with the object example.

We will create a variable called john and initialize it to an array. We will then create a new variable called newJohn and assign it the value of john.

However, when we try to add a new item to the array stored in thenewJohn variable, it will also affect the john array. Again, this is because we are only dealing with one array and NOT two separate copies of an array. The variable newJohn is referencing the same array that was initialized in the variable john.

This is not the case when dealing with values that are pass-by-value.

Pass-by Value

Unlike objects and arrays, primitive values such as numbers or strings will actually create a copy.

Here, we create a variable john1 and assign it the value of john2 . We will also create a function called john3 that takes an argument and adds 2 to that argument. The parameter that we will pass in is the john4 variable.

As we saw with pass-by-reference, when we tried to mutate the object in newJohn, we were essentially referencing the object stored in the variablejohn and changing the object. With pass-by-value, when we pass in the variable john4 to the function and return the value of john8 , WE ARE NOT CHANGING the original value of the variablejohn4.

This is because when we passjohn4 into the function, it creates a copy and it is given its own space in memory.

When we newJohn1 on line 14, we still have our original john2.

The value newJohn3 is not changed!

Conclusion

Understanding PBV vs PBR is helpful because if you are writing code and you want to set one variable equal to another variable, or in the example above, pass a variable into a function as an argument, you need to know whether the value will be pass-by-value or pass-by-reference. This will help you avoid errors when working with your data.

Computer programming is a process where professionals write codes to instruct a computer or an application to perform specific tasks. Each programming language offers functions that are a set of commands or statements that execute the task when call.

These sets of commands take inputs or some arguments to give return outputs. These arguments can either be passed by value or reference. Understanding the distinction between pass-by value and reference is crucial element in programming.

So, what is the difference between pass by value and pass by reference? Pass by value is when the parameter value copies to another variable. On the other hand, pass by reference is where the actual parameter passes to the function.

These programming statements allow professionals to manage and maintain applications or software programs. The table below displays the difference between pass by value and pass by reference in C#.

Difference between Pass by Value and Pass by Reference with Table

ParametersPass By ValuePass By ReferenceDefinitionIt is the process of copying the function parameter value to another.It is the process of passing the actual parameter to the function.Changes EffectAny tweaks made inside the function are not reflected in the original value.Changes made inside the function are reflected in the original value.Actual ParameterMakes a copy of the actual parameter.The address of the actual parameter passes to the function.Association with FunctionFunction gets a copy of the actual content.Function access the original variable content.Memory RequirementRequires more memoryRequires less memoryTime RequirementNeeds more time since it involves copying values.Requires less time since there is no copying values.ApplicationBuilding a multi-threaded application.Passing objects of large structs or classes.

What Is Pass by Value?

Pass by value is a mechanism of copying function parameter value to another memory location. If you are tweaking a variable within the function, it accesses only the copy.

These changes or modifications will not have any effect on the original value. The value copied to a new memory location is called newValue. The changes are said to be made on the newValue and not the original value.

Below is an illustration of pass by value:

let a = 100;let b = a;a = 90;

console.log(a); // prints 90

console.log(b); // prints 100

Changing the value of “a” does not affect the value of “b”, since different memory is allocated to it on assignment.

What Is Pass by Reference?

Pass by reference is a mechanism where the memory address is passed to the function. The function will get access to the original variable.

When the memory address passes to the function, it alters with the actual variable. The process requires less memory and time since there is no copying issue.

Below is an illustration of pass by reference:

let a = {‘name’ : ‘Rohan’, ‘age’ : 30};

let b = a;

a.name = ‘Sam’;

console.log(a); // prints {‘name’ : ‘Sam’, ‘age’ : 30}

console.log(b); // prints {‘name’ : ‘Sam’, ‘age’ : 30}

Both “a” and “b” variables point to the same object, hence any changes in the value of “a” will update “b” value and vice versa.

Main Difference between Pass by Value and Reference

  1. Pass by value involves copying the function parameter value to another variable. On the other hand, pass by reference involves passing the actual parameters to the function.
  2. Pass by value changes made inside the function are not reflected in the original value. Changes made inside the function of pass by reference are reflected in the original value.
  3. Pass by value makes a copy of the actual parameter, whereas pass by reference passes the address of actual parameter to the function.
  4. Pass by value function gets a copy of the actual content, while pass by reference function access the original variable content.
  5. Pass by value requires more memory since it involves copying values. On the other hand, pass by reference requires less memory since there is no copying.
  6. Pass by value needs more time due to copying of values, whereas pass by reference requires less time.

Similarities between Pass by Value and Reference

  1. Both are involved in programming languages.
  2. Both are ideal in writing efficient and effective programs.
  3. Both make the programs more manageable and easy to maintain.
  4. Both are two methods of calling a function.

Conclusion

So, what is the difference between pass by value and pass by reference in C? The former is a coping mechanism of the function parameter value to another variable, while the latter is the mechanism of passing the actual parameter to the function.

Pass by value changes inside the function are not reflected in the actual value. This happens since the mechanism will make an original parameter copy and require more memory and time.

Pass by reference changes inside the function are reflected in the original value. This happens since the address of the actual parameter passes to the function and will require less memory and time.

Understanding the difference between pass by value and pass by reference with example is crucial.  It will help you overcome challenges associated with pass by value and pass by reference in python.

What is pass by value and pass by reference with example?

"Passing by value" means that you pass the actual value of the variable into the function. So, in your example, it would pass the value 9. "Passing by reference" means that you pass the variable itself into the function (not just the value). So, in your example, it would pass an integer object with the value of 9.

What is the difference between pass by reference and pass by address?

Pass by reference (also called pass by address) means to pass the reference of an argument in the calling function to the corresponding formal parameter of the called function so that a copy of the address of the actual parameter is made in memory, i.e. the caller and the callee use the same variable for the parameter.