-->




c++ pointer simple example pass by reference

1 comment
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
pointer argument example pass by reference


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 

1 comment:

  1. This blog awesome and i learn a lot about programming from here.The best thing about this blog is that you doing from beginning to experts level.

    Love from

    ReplyDelete