c++ pointer simple example pass by reference

Write a program which passes a reference to a function and function has a pointer as argument. Keep the return type void and display result in main body. Apply any maths operation find square of a number
Use any c++ compiler: Codeblocks recommended.

If you have any confusion read this difference b/w pass by value and pass by reference 


#include<iostream>
using namespace std;

void
pass_by_reference(int *);
int
main()
{

int
number;

cout<<"Enter Number to Calculate Square: ";cin>>number;
cout<<"\n\n\n";

pass_by_reference(&number);
cout<<"\t\tResult: "<<number<<endl;

return
0;

}


void
pass_by_reference(int *iPtr)
{
*
iPtr=*iPtr * *iPtr;
}




Sample input Output of program


In the above example there is no need to return from function because changes will be made permanently on reference of number.
Find more examples here: C++ Simple Examples 


Previous Post Next Post