Topic :Pointer to Function in C++: Passing by Value: Passing by Pointers:



Pointer To Function:

One of the uses of pointer variables is in passing arguments to function. Pointers allow us to pass the address of a local variable into a function, which can then modify the local variable. To understand why pointer is important for function, we Need to Understand how arguments are passed to a function. Arguments can be passed by two ways:




Passing by Value:

The default parameter passing Mechanism in C++ is classified as Pass by Value, also know 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 an simple example to clear that how Arguments Pass by Value to function. in this example, we will create an function, this function will try to change the value of Actual variable.



/*Call by value: InfoBrother*/
 
#include <iostream>
using namespace std; 
 
//function, used to change the value of actual variable:
void increment(int x) 
{
    ++x; //increment the value.
}
 
main() 
{
	int x=10; //actual variable.
	cout<<" Before increment: "<<x<<endl; 
 
	increment( x ); //function calling. call by value:
	cout<<" After increment: "<<x<<endl;
 
	return 0;
}


Passing 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 means 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 the 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:



/*call by Pointer: InfoBrother*/
 
#include <iostream>
using namespace std; 
 
//function, used to change the value of actual variable:
void increment(int *x) 
{
    ++*x; //increment the value.
}
 
main() 
{
	int x=10; //actual variable.
	int *ptr = &x;
	cout<<" Before increment: "<<x<<endl; 
 
	increment( &x); //function calling. call by pointer:(pass address)
	cout<<" After increment: "<<x<<endl;
 
        return 0;
}


Example:

Let's have an simple example to understand the value of pointer in function. in this example we will create two swap function that attempt to swap the values stored in two variables. first function will took two simple arguments, and in main function we will try to call the function by values, on the other side second function will took two pointers as an arguments, and in main function we will call the function by reference.



/*Pointers to function: InfoBrother*/
 
#include <iostream>
using namespace std; 
 
//swap function without pointer:
void swap1 (int left, int right) 
{
	int temp;
	temp = left;
	left = right;
	right = temp;
}
 
//swap function using pointer.
void swap2 (int *p_left, int *p_right) 
{
	int temp = *p_left;
	*p_left = *p_right;
	*p_right = temp;
}
 
int main ()
{
	int x = 10, y = 20; //two values 
	swap1( x, y ); //calling function by value:
	cout <<"Swap Without Pointer : x = "<< x << " | y = " << y << '\n';
 
	swap2( & x, & y ); //calling function by reference:
	cout <<"Swap with pointer: x = "<< x << " | y = " << y << '\n';
 
	return 0;
}


Take a Minute to see if you can Guess which Function correctly Swaps the two values.


Function Swap1() just switches the values of two variables local to the swap function. it can't touch the values passed into it because they just store copies of the original values (the value stored in variables x and y) passed into them. visually, we can see that the function call copies the values of x and y into the variable left and right:

Function Swap2() more interestingly, takes in the addresses of the local variables x and y. The variables p_left and p_right now point to x and y. the swap2() function has access to the Memory that backs those two variables, so when it performs its switch, it writes into the memory of variables x and y.







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