POINTERS AND FUNCTIONS IN C ++ PROGRAMMING

POINTERS AND FUNCTIONS IN C ++ PROGRAMMING

Pointer to Function


Pointer is used in Function Declaration. Difficult functions can be easily defined and accessed using Pointer.


The use of Pointer in Function Definition can be divided into 2 groups -


  • Call by value
  • Call by Reference

Before understanding both of the above, let us know what is Actual and Formal Parameters, after which Call by value and Call by Reference will be very easy to understand. We can divide the variable to be sent in the function in two ways which are as follows


  • Variables that are used as Function Argument or Parameter used while calling the actual Parameter-Function are called Actual Parameter.
  • Formal Parameter- Variables that are used as an argument at the time of Function Definition are called Formal Parameters.

Call by Value


When calling a function in a program, when we pass the value of the actual parameter value, the Formal Parameter of the function, then calling such function is called call by value. In the Call by value, the Actual Parameter Value Copy is given to the formal parameter, so if the value of the formal Parameter is changed inside the function body, then it does not have any effect on the value of the Actual Parameter.


#include<iostream.h>

#include<conio.h>


int squ(int a) // formal parameter

{

int t;

t = a * a;

return t;

}


void main()

{

clrscr();

int x;

x = 7;

cout<<squ(x); // Actual Parameter

}


We can explain the call by value by the following example.


int mut(int a)

{

a = a*a;

return(a);

}


void main()

{

clrscr();

int x=3;

int r;  

cout<<“\n value of x before Calling Function =”<<x;

r = mut(x);

cout << “\n Square is =” << x ;

cout << “\n value of x after calling Function =” << x;

}

Output


Value of x before calling Function = 3

Square is = 9

Value of x after Calling Function = 3


In the above example, there is a formal variable whose value is changed inside the function body and x is the actual variable whose value is passed by the Formal variable by value. Here the function is called by value. Hence, the value of x remains the same before and after calling the function, while the value of the formal variable changes.


Call by Reference


When we call a function in the program, when we give the address (reference) of the actual parameter to the Formal Parameter, then such function call is called Call By Reference. In the Call by Reference, the address of the actual parameter is given in the Formal Variable. Therefore, the change inside the function body in the Formal Variable has an effect on the value of the Actual Parameter. Because Formal and Actual Parameters specify the same memory location.


The Pointer data type is used in the Formal Parameter in Call by Reference. And while calling the function, the address of the actual parameter is passed by the & operator.


Example:-


int add (*a);

{

a*a = *a + *a;

return *a;

}


void main ()

{

clrscr();

int x = 3;

int r;

cout<< “\n value of x before calling Function =”<< x;

r = add (&x);

cout<< “\n double=”<<r;

cout<< “\n value of x after calling Function =”<< x;

}


Output


value of x before calling Function = 3

Double = 6

Value of x after calling Function = 6


AKASH TIMES

I AM AN ENGINEER BY PROFESSION BUT BLOGGER IS MY OLD DAYS DREAM TO CREATE MY OWN SITE FOR THOSE WHO ARE CURIOUS ABOUT MY BIRTHPLACE THEN I AM COMING FROM (INDIA). THE CURRENT CITY LIVES IN NAVI MUMBAI, INDIA

Post a Comment

IF YOU HAVE ANY PROBLEM PLEASE FELL FREE TO CONTACT US BY - AKASH TIMES

PREVIOUS POST NEXT POST