How To Write Hello World Program In C++ Programming Language

Image
  How To Write a Hello World Program In C++ Programming Language In this post we will look at how a C++ program works. We will try make the example as simple as possible for the beginners so they can easily understand the basics of C++. We will write a simple C++ hello world program in C++ programming language which is considered as the very first program in almost every programming language. A Brief Introduction to the C++ Programming Language C++ is an object oriented programming language and it is an extension to C programming language. C++ is a general purpose programming language and it is also a case-sensitive language. C++ is very powerful programming language it has features of both low level and high level programming languages, and that is why it is called mid level programming language. Hello World program in C++ programming language Above is an example of a simple Hello World program in C++ programming language. Now let us look at the code, and I will take you...

What is Call by Value and Call by Reference in C Programming


What is Call by Value and Call by Reference in C Programming

We use functions in programming languages like C, C++, C#, etc. Today we will cover some important concepts about functions that are implemented in C programming language. The topic we will cover today is the functions and the use of call by value and call by reference in C programming. This is a very important topic for functions. As we all know when we use functions we can pass values to functions from within the main program, the point where we call a function. There are two ways to pass data to functions in C language i.e.: Call by value and Call by reference.





What is Call by value in C programming




In Call by value, we pass the values of parameters or arguments that are used inside the main program. We only send variables' values to the function parameters. As shown in the above coding example, we cannot modify or change the values of parameters that are used inside the main program.

In the above coding example, we pass the value parameter to the function named CallByValue(value) 

In call by value, different memory is allocated to both the parameters that are used during the function call and the parameters that are used inside function round brackets, during the function definition.

In call by value, the value of the actual variable or argument is not changed. For example in the above code, the value of the value variable before and after the function call will be the same as 50, though we changed the value of the value1 variable inside the function value will not change inside the main program.


What is Call by Reference in C Programming




Now what is call by reference. It is different from call by value in that during the call by reference method we pass the reference or address of the arguments used during the function call.

In the above coding example, we pass the value parameter to the function with the & operator which specifies the address or reference of the argument passed CallByReference(&value).

Another very important point is that in the call-by-reference, the value of the arguments that we used during the function call can be modified by changing the value of the parameters that are used inside the function definition point within the curly round brackets. The main reason behind the value change is that during the function call, we pass the address of the arguments that are used during the function call.

For example, in the above code, the value of the value variable will be changed before and after the function call. First, it is 50, but after the function call is completed, the value of the actual variable is changed to 60.

In call by reference, the same memory location is used for both the arguments that we use during the function call and the function parameters that are used in the function definition point. That is why if we change the value of the parameters in the function definition it is reflected inside the main program variables.


Comparison between call by value and call by reference

In call by value method, the value of the arguments is passed to the function definition, whereas in call by reference the address of the arguments is passed to the function definition.

In call-by-value, changes in the values of parameters in the function definition are not reflected in the main program variable values, whereas in call-by-reference, changes in the values of parameters in the function definition are reflected inside the main program arguments values.

In call by value, both the arguments and the parameters have different memory locations whereas in call by reference both the arguments and the parameters have the same memory location.

Congratulations we have successfully gathered and understood the concept of call by value and call by reference.







Comments

Popular posts from this blog

How if else-if else ladder statement works in C program.

How to add two numbers in C programming language