Passing Parameters to a Function in C++

In this article, we are going to learn what are the different ways to pass parameters to function in c++

We can pass parameters to a function in three ways.

  • Pass  by value
  • Pass by reference
  • Pass by pointer

Pass by value:

In this method, the copies of values of actual parameters are passed to formal parameters. Due to this, actual parameters and formal parameters are stored at different locations, providing another copy of values. Thus, changes made to the formal parameters inside the function don’t reflect outside the function.

Let’s look at a sample program implementing parameters passing by the value method.

The output of the program is.

As you can see in the above program, two values have been assigned to variables a and b initially. These are the actual parameters. Then after swapping in the swap function, the values of a and b got swapped.

However, the same copy of values has been assigned to the formal parameters and the main method, the values of a and b in the main method are the initial values. So, outside the function, if we try to print the values, they were not reflected in the main method. This is known as passing by value.

This method is the basic method that is used, but due to the above drawback, we can use this function only in the cases we don’t require the reflection of the formal parameters outside the function.

Pass by reference

In this method, instead of passing copies to the actual parameters, we pass the reference to the actual parameters. So formal parameters and actual parameters are stored in the same location, having a single copy for both formal and actual parameters.

See also  UNORDERED MAP in Cpp

Let’s look at a sample program implementing pass-by-reference.

The output of the program is.

As you can see in the above program,  we passed the actual parameters as it is. But we used a ‘ & ’ symbol in the formal parameters, resembling that we passed the reference of the particular parameter. So, the values of the function are reflected outside the function also, as there is only one copy of the values shared between the formal parameters and the actual parameters.

Pass by pointer

Passing parameters by pointers is a method which provides the same functionality as passing parameters by reference does. This means that both actual parameters and formal parameters share the same memory location and the changes made inside the function reflect outside of the function too. But in this method, we pass parameters to the function by using pointer variables.

Let’s look at a sample program implementing the passing of parameters by pointers.

The output of the program is.

as we know, there is no difference in the output which is the same as the passing by reference. The way of passing parameters is the only difference between the two examples.

That’s it for this article, In the next article we will learn about the types of functions with examples.

Leave a Comment

Your email address will not be published. Required fields are marked *

Ads Blocker Image Powered by Code Help Pro

Ads Blocker Detected!!!

we provide projects, courses, and other stuff for free. in order for running we use Google ads to make revenue. please disable adblocker to support us.