IMPLEMENTATION OF CALL BY VALUE
AIM:- To write a C++ program to find the value of a number raised to its power that demonstrates a function using call by value.
1) Start the program.
2) Declare the variables.
3) Get two numbers as input
4) Call the function power to which a copy of the two variables is passed .
5) Inside the function, calculate the value of x raised to power y and store it in p.
6) Return the value of p to the main function.
7) Display the result.
8) Stop the program.
SOURCE CODE:
#include<iostream.h>
#include<conio.h>
void main()
{
int x,y;
double power(int, int);
clrscr();
cout<<"Enter x,y:"<<endl;
cin>>x>>y;
cout<<x<<" to the power "<<y <<" is "<< power(x,y);
getch();
}
double power(int x,int y)
{
double p;
p=1.0;
if(y>=0)
while(y--)
p*=x;
else
while(y++)
p/=x;
return(p);
}
Output:
ENTER X , Y:
2 3
2 TO THE POWER 3 IS 8
RESULT:
- Thus, the given program for implementation of call by value has been written and executed successfully.
0 comments:
Post a Comment