Topic :Passing Arguments to Function: Call by Value | Call by Pointer | Call by Reference

Passing Arguments To Function:

The Main objective of passing Argument to function is Message passing. The message passing is also known as communication between two functions, that is between a caller and called functions. There are three Methods by which we can pass values to the function. These Methods are as follows:





C++ supports all these three types of passing values to the function, whereas C Supports the first two types only. The argument used to send values to function are known as input arguments. The arguments used to return result are known as output Arguments. the Arguments used to send as well as return results are known as input-output arguments. While passing values to the function, the following conditions should be fulfilled.

The data type and a number of actual and formal arguments should be same both in the caller and called a function. Extra arguments are discarded if they are declared. if the formal arguments are more than the actual arguments, then the extra arguments appear as garbage. Any mismatch in the data type will produce the unexpected result.


Passing argument to function



Here we take an example, this example will get two numbers from the user and swap these numbers. we will try to solve this example with all three Methods. (by value, by pointer, by reference).


Call by Value:

The default parameter passing Mechanism in C++ is classified as Pass by Value, also known as call by value. This Means the value of the actual parameter is copied to the formal parameter for the purpose of executing the function's code. Since it is working on a copy of the actual parameter, the function's execution cannot affect the value of the actual parameter owned by the caller.

Let's have a simple example to clarify that how Arguments Pass by Value to function. in this example, we have a function which required two number from a user. when user will enter numbers, this function will try to swap the numbers, and return it. have a deep look in this example:



/*Swapping using Call-by-value - InfoBrother*/
 
#include<iostream>
using namespace std;
 
//function to swap the Numbers: 
void swap(int i , int j) 
{
    int temp=j;
    j=i;
    i=temp;
}
 
main()
{
    int a,b; //variable to store the Numbers:
    cout<<"Enter The value of A : ";
    cin>>a;
    cout<<"\nEnter The value of B : ";
    cin>>b;
 
    cout<<"\n Before swaping the value is : A = "<<a<<" B = "<<b<<endl;
 
    swap(a,b); //function call-by-value.
    cout<<" After swaping the value is A = :"<<a<<" B = "<<b<<endl;     
 
    return 0;
}


In the Above example, did you Notice that the value of A and B is same after and before swapping. Because the value of the actual parameter is copied to the formal parameter for the purpose of executing the function's code. Since it is working on a copy of the actual parameter, the function's execution cannot affect the value of the actual parameter owned by the caller.



Call by Pointer ( * ):

Passing by Pointer also known as call by Pointer Method of passing arguments to a function copies the address of an argument into the formal parameter. Inside the function, the address is used to access the actual argument used in the call. This mean that changes made to the parameter affect the passed argument. To pass the value by pointer, argument pointers are passed to the functions just like any other value. so we Need to declare the function parameters as pointer types, which exchanges the values of the two integer variable pointed to by its arguments.

Let's have an same example that we used for "passing by value", but here we will pass the argument by Pointer. let's have a deep look in this example:



/*Swapping using Call-by-pointer- InfoBrother*/
 
#include<iostream>
using namespace std;
 
//function to swap the Numbers:
void swap(int *i , int *j)  
{
    int temp=*j;
    *j=*i;
    *i=temp;    
}
 
main()
{
    int a,b; //variable to store the Numbers:
    cout<<"Enter The value of A : ";
    cin>>a;
    cout<<"\nEnter The value of B : ";
    cin>>b;
 
    cout<<"\n Before saping the value is : A = "<<a<<" B = "<<b<<endl;
 
    swap(a,b); //function call-by-Pointer.
    cout<<" After swaping the value is A = :"<<a<<" B = "<<b<<endl;     
 
    return 0;
}


In The above example, the value of A and B change indirectly after swapping. because at this time we didn't pass the only value to function, but we pass the address of the variable to function. Function Swap() more interestingly, takes in the addresses of the local variables A and B. The Pointer variables i and j now point to A and B. so function has access to the Memory that backs those two variables, so when it performs its switch, it writes into the memory of variables A and B.



Call by Reference ( & ):

while it is possible to achieve a call-by-reference manually by using the pointer operators, this approach is rather clumsy. First, it compels you to perform all Operations through pointers. second, it requires that you remember to pass the addresses (rather than the values) of the arguments when calling the function.

Fortunately, in C++, it is possible to tell the compiler to automatically use call-by-reference rather than call-by-value for one or more parameters of a particular function. We can accomplish this with a reference parameter. when we use a reference parameter, the address (not the value) of an argument is automatically passed to the function. within the function, operations on the reference parameter are automatically dereferenced, so there is no need to use the pointer operators.

Let's have a simple example to understand, how we can pass arguments by reference to function.



/*Swapping using Call-by-Reference - InfoBrother*/
 
#include<iostream>
using namespace std;
 
//function to swap the Numbers: 
void swap(int &i , int &j) 
{
    int temp=j;
    j=i;
    i=temp;
}
 
main()
{
    int a,b; //variable to store the Numbers:
    cout<<"Enter The value of A : ";
    cin>>a;
    cout<<"\nEnter The value of B : ";
    cin>>b;
 
    cout<<"\n Before swapping the value is : A = "<<a<<" B = "<<b<<endl;
 
    swap(a,b); //function call-by-Reference.
    cout<<" After swapping the value is A = :"<<a<<" B = "<<b<<endl;     
 
    return 0;
}


In The above example, the value of A and B change indirectly after swapping same like Pointers. As apparent from the Name "By Reference" we are not passing the value itself but some form of reference or address. Thus by passing the address of the variable to the called function, we convey to the function that the number we should change is lying inside this passed Memory location, square it and put the result again inside that memory location. When the calling function gets the control back after calling the called function, it gets the changed value back in the same Memory location.



Don't confuse about Pointer and Reference, Pointer is a variable which stores the address of another variable. while Reference is a variable which refers to another variable. Mostly Programmers Use References in Function parameters and return type to define useful and self-documenting interfaces. and Use pointers to implement algorithms and data structures.





I Tried my Best to Provide you complete Information regarding this topic in very easy and conceptual way. but still if you have any Problem to understand this topic, or do you have any Questions, Feel Free to Ask Question. i'll do my best to Provide you what you need.

Sardar Omar.
InfoBrother





WRITE FOR INFOBROTHER

Advertising






Advertisement