C++

Assignment by Value C++

The assignment operation in any programming language is the most crucial one since it is the most commonly used operation. There are certain ways of performing this operation and one such way is the “assignment by value”. This method holds prime importance in C++ and to understand that we will have to read the following sections of this article. Our main motive is to teach you how the concept of “assignment by value” works in C++ in Ubuntu 20.04.

Assignment by Value in C++ in Ubuntu 20.04

The concept of “assignment by value” simply means to assign the value of a constant or a variable to another variable. Contrary to this concept, there is another concept which is known as “assignment by reference”. In the latter type, we pass the reference of a variable (address or pointer to a variable) for the assignment operation to take place. Both these types of assignments have different purposes to serve, however, since this article is mainly focused on the “assignment by value” concept, that is why we will just be discussing those examples below that will make this concept clearer for you.

For that, we will start with a very basic example of simple assignment operations followed by slightly uncommon types of assignments. Then finally, we will discuss the “assignment by value” concept with respect to the functions in C++. However, the methods discussed in all of these examples fall under the category of “assignment by value”.

Example 1: Using the Simple Variable and Constant Assignments in C++

In this example, we just want to discuss how the assignment operator in C++ works. For that, you will have to look through the following C++ script.

In this C++ script, we just have one function i.e. our “main()” function. Inside this function, the first statement is “int x=2”. It is an assignment statement and this type of assignment is known as the “constant assignment”. The value of a constant is being assigned to a variable which in our case is “2”. Then, the second statement is “int y=x”. It is also an assignment statement but this type of assignment is known as the “variable assignment”. The value of a variable is being assigned to another variable. After that, we wanted to print the values of both “x” and “y” on the terminal to find out if the assignments have taken place successfully or not.

Upon compiling and executing this C++ script, we figured out that both of our assignments i.e. variable assignment and constant assignment have taken place successfully since the values of both the variables “x” and “y” were “2” as shown in the image below.

Example 2: Using the Uniform Initialization and Constructor Initialization Assignments in C++

In this example, we just want to explore two other types of assignments i.e. uniform initialization assignment and constructor initialization assignment in C++. In the former type of initialization, we do not use the “=” operator for assigning a value to the desired variable rather the value is assigned while enclosing it within curly brackets. However, in the latter type, we do not use the “=” operator and assign the value by enclosing it within round brackets instead. For that, you will have to look through the following C++ script.

In this C++ script, we just have one function i.e. our “main()” function. Inside this function, the first statement is “int x{200}”. It is an assignment statement and this type of assignment is known as the “uniform initialization assignment”. The value of a constant is being assigned to a variable while using the curly brackets. Then, the second statement is “int y(100)”. It is also an assignment statement but this type of assignment is known as the “constructor initialization assignment”. The value of a constant is being assigned to a variable while using the round brackets. After that, we wanted to print the values of both “x” and “y” on the terminal to find out if the assignments had taken place successfully or not.

Upon compiling and executing this C++ script, we figured out that both of our assignments i.e. the uniform initialization assignment and the constructor initialization assignment have taken place successfully since the values of the variables “x” and “y” were “200” and “100” respectively as shown in the image below:

Example 3: Using the Concept of Assignment by Value with the Functions in C++

Now, we want to extend the concept of assignment by value to the functions in C++. For that, you will have to look through the following C++ script.

In this C++ script, we have first defined a function named “Addition” with “void” return type whose sole purpose is to add the two given numbers and then print their sum on the terminal. This function accepts two parameters i.e. number1 and number2 of integer data types. Then, within the body of this function, we have defined a variable “sum” of integer data type and have assigned to it the value of “number1+number2” i.e. the sum of the two passed numbers. Then, we wanted to print the value of the “sum” variable on the terminal.

Note: You can also define the “Addition” function after the “main()” function but for doing so, you will have to provide the prototype of this function before the “main()” function.

After that, within our “main()” function, we have declared two integers “x” and “y”. Then, we have taken the values of these two variables as inputs from the user at the run time by displaying relevant messages. After that, we have called the “Addition” function with the variables “x” and “y”. In other words, we can say that we have used the concept of “pass by value” over here since the actual values of the variables taken as input from the user were passed to the “Addition” function instead of their references.

When we compiled and executed this code, the output that was generated is shown in the image below:

Upon executing this code, we were first asked to enter the value of the variable “x”. We entered the value “102”. After that, we were asked to enter the value of the variable “y”. This time, we entered the value “278”. Then, the “Addition” function was being called with these two values as a result of which the sum i.e. 380 was printed on the terminal as revealed in the image above.

Conclusion

In this article, we wished to discuss the concept of “assignment by value” in C++ in Ubuntu 20.04. We first introduced you to this concept while comparing it with the concept of “assignment by reference”. Then, we walked you through three different examples that involved the usage of “assignment by value” in C++. The first two examples focused on one-liner assignments whereas the third one made use of the functions in C++ to elaborate this concept in a better way. By looking at all three of these examples, you will instantly learn the concept of “assignment by value” in C++ in Ubuntu 20.04.

About the author

Aqsa Yasin

I am a self-motivated information technology professional with a passion for writing. I am a technical writer and love to write for all Linux flavors and Windows.