examples of C++ : CALL BY REFERENCE ( EXAMPLE) :- इससे पहले के article मे function के type , strurcture और call by reference को discuss किया था | अब इस article मे function के उदाहरन को discuss करेग जिसमे call by reference को use किया जाता है |
उदाहरन -1
इस उदाहरन मे , square function को बनाया जाता है |
Explanation
सबसे पहले यूजर द्वारा data input किया जाता है | इसके बाद value को variable ‘a’ मे assign कर दिया जाता है |
इसके बाद square() मे variable ‘a’ के address को pass किया जाट है | इस function से प्राप्त आउटपुट को variable ‘o’ मे assign कर दिया जाता अहि \
उसके बाद variable ‘o’ की value display किया जाता है |
square () मे ,
square () मे variable ‘a’ के address को एक pointer variable ‘b’ मे assign किया जाता है |
इसके बाद int square = *b * *b से square किया जाता है | लेकिन *b * *b को reader द्वारा समजना बहुत मुश्किल होता है |
इसलिए int square = (*b)* (*b) को use किया जाता है |
उसके बाद variable square की value को return किया जाता है |
source code
#include<iostream.h>
#include<conio.h>
#include<math.h>
void sqaure(int*);
void main()
{
int a ;
cout<<“Enter value : “;
cin>>a;
int output = sqaure(&a );
cout<<” Square output of a :”<<output<<endl;
}
getch();
}
void add(int *b)
{
int square = (*b)* (*b) ;
return square;
}
इसका आउटपुट होता :-
Enter value : 3
Square output of a : 9
उदाहरन -2
इस उदाहरन मे , cube function को बनाया जाता है |
Explanation
सबसे पहले यूजर द्वारा data input किया जाता है | इसके बाद value को variable ‘a’ मे assign कर दिया जाता है |
इसके बाद cube() मे variable ‘a’ के address को pass किया जाट है | इस function से प्राप्त आउटपुट को variable ‘o’ मे assign कर दिया जाता है |
उसके बाद variable ‘o’ की value display किया जाता है |
cube () मे ,
cube () मे variable ‘a’ के address को एक pointer variable ‘b’ मे assign किया जाता है |
इसके बाद int cube = *b * *b * *b से square किया जाता है | लेकिन *b * *b को reader द्वारा समजना बहुत मुश्किल होता है |
इसलिए int cube = (*b)* (*b) * (*b) को use किया जाता है |
उसके बाद variable cube की value को return किया जाता है |
source code
#include<iostream.h>
#include<conio.h>
#include<math.h>
void cube (int*);
void main()
{
int a ;
cout<<“Enter value : “;
cin>>a;
int output = cube (&a );
cout<<” cube output of a :”<<output<<endl;
}
getch();
}
void cube(int *b)
{
int cube = (*b)* (*b) ;
return cube;
}
उदाहरन -3
इस उदाहरन मे , power function को बनाया जाता है |
Explanation
सबसे पहले यूजर द्वारा दो data input किया जाता है | इसके बाद value को variable ‘a’ और ‘b’ मे assign कर दिया जाता है | ‘a’ power function मे base होता है तब variable ‘b’ power function मे power होता है |
इसके बाद cube() मे variable ‘a’ और ‘b’ के address को pass किया जाट है | इस function से प्राप्त आउटपुट को variable ‘o’ मे assign कर दिया जाता है |
उसके बाद variable ‘o’ की value display किया जाता है |
power () मे ,
power () मे variable ‘a’ और ‘b’ के address को एक pointer variable ‘base’ और power मे assign किया जाता है |
इसके बाद loop चल्या जाता है | loop तब तक चलता है जब तक control variable की value power से कम या सामान होती है |
इस loop की body मे , int o = o*base को calculate किया जाता है | ‘o’ की initail value ‘1’ होती है |
बाद मे variable ‘o’ की value को return कर दिया जाता है |
source code
#include<iostream.h>
#include<conio.h>
#include<math.h>
void power (int*, int*);
void main()
{
int a , b;
cout<<“Enter Base : “;
cin>>a;
cout<<“Enter Power : “;
cin>>b;
int output = power (&a ,&b );
cout<<” Power output :”<<output<<endl;
}
getch();
}
void cube(int *base , int *power)
{
int i = 0 ;
int o=1;
for(i=0;i<power ; i++)
{
o=o* base ;
}
return o;
}
इसका आउटपुट होगा :
Enter Base : 3
Enter Power :3
Power output :27
उदाहरन -2
इस उदाहरन मे , even – odd function को बनाया जाता है |
Explanation
सबसे पहले यूजर द्वारा data input किया जाता है | इसके बाद value को variable ‘a’ मे assign कर दिया जाता है |
इसके बाद even () मे variable ‘a’ के address को pass किया जाट है | इस function से प्राप्त आउटपुट को variable ‘o’ मे assign कर दिया जाता है |
उसके बाद variable ‘o’ की value display किया जाता है |
cube () मे ,
cube () मे variable ‘a’ के address को एक pointer variable ‘b’ मे assign किया जाता है |
इसके बाद condition check किया जाता है | अगर *b को 2 से divid किया जाता है अगर इसके remainder ‘0’ होती है तब ‘1’ return होगा |
अन्यथा ‘0’ को return किया जाता है |
source code
#include<iostream.h>
#include<conio.h>
#include<math.h>
void even (int*);
void main()
{
int a ;
cout<<“Enter value : “;
cin>>a;
int count = cube (&a );
if(count == 0)
{
cout<<” Number is even. “<<endl;
}
else
{
cout<<” Number is not even. “<<endl;
}
getch();
}
void even (int *b)
{
if((*b)% 2 == 0 )
{
return ‘0’;
}
else
{
return ‘1’ ;
}
}
इस article मे discuss किये गये उदाहरनो के level बहुत सरल है आगे के article मे और मुश्किल उदाहरनो को discuss करेगे |