1. Pointers are very much used in function and declaration.
2. Sometimes with the use of pointer , a complex function can be easily represented and accessed.
3. The use of pointers in a function definition may be classified into two groups.
Call by Value
Call by Reference
Call By Value:-
1.In a call by value , whenever a portion of program invokes a function with a formal arguments , then control is transferred from main function to calling function and value of actual argument is copied to argument of function.
2. While calling a function , when copy of formal argument is sent to arguments of the called function then it is known as call by value.
3. In a call by value, when a control is transferred back from called function to the calling function of the program then altered values are not transferred back.
WAP for implementation of 'call by value' concept.
#include<conio.h>
#Include<iostream.h>
void main( )
{
int x,y;
clrscr( );
x = 12;
y = 13;
cout<<"\n\n\tValue of x--->"<<x<<"\t"<<"Value of y--->"<<y;
funct(x,y);
cout<<"\n\n\tValue of x--->"<<x<<"\t"<<"Value of y--->"<<y;
getch( );
}
void funct(int a, int b)
{
a = a*a;
b = b*b;
}
Output:-
Value of x--->12 Value of y--->13
Value of x--->12 Value of y--->13
Call By Reference:-
1. When a function is called by a portion of a program , the address of actual arguments are copied on to formal arguments though they may be referred by different variable name.
2. While calling a function, when the address or reference of actual arguments are sent to the called function then it is known as call by reference.
3. The content of that address can be accessed freely either within a function or within a calling routine.
4. In a call by reference when the address of actual arguments are sent then these address are stored within the pointer arguments of called function.
5. Thus the use of pointers as function arguments permits the corresponding data item to be altered globally from within the function.
6. Any change that is made to the data item will be recognized in both functions and calling portion of the program.
WAP to implement the concept of call by reference.
#include<conio.h>
#include<iostream.h>
void main( )
{
int x,y;
clrscr( );
x = 12;
y = 13;
cout<<"\n\n\tValue of x--->"<<x<<"\t"<<"Value of y--->"<<y;
funct(&x,&y);
cout<<"\n\n\tValue of x--->"<<x<<"\t"<<"Value of y--->"<<y;
getch( );
}
void funct(int *p1, int *p2)
{
*p1 = *p1 **p1;
*p2 = *p2**p2;
}
Output:-
Value of x is--->12 Value of y is--->13
Value of x is--->144 Value of y is--->169
WAP to exchange the values of two variable using call by value.
#include<conio.h>
#Include<iostream.h>
void exchange(int a, int b)
{
int temp;
temp=a;
a=b;
b=temp;
cout<<"\n\n\tAfter Exchange Value of A is--->"<<a<<"\tValue of B is--->"<<b;
}
void main( )
{
int a=12,b=14;
clrscr( );
cout<<"\n\n\tBefore Exchange Value of A is--->"<<a<<"\tValue of B is--->"<<b;
exchange(a,b);
getch( );
}
Output:-
Before Exchange Value of A is--->12 Value of B is--->14
After Exchange Value of A is--->14 Value of B is--->12
WAP to exchange the value of two variables using call by reference
#include<conio.h>
#include<iostream.h>
void exchange(int *a, int *b)
{
int *temp;
*temp=*a;
*a=*b;
*b=*temp;
}
void main( );
{
int a=12,b=14;
clrscr( );
cout<<"\n\n\tBefore Exchange Value of A is--->"<<a<<"\tValue of B is--->"<<b;
exchange(&a,&b);
cout<<"\n\n\tAfter Exchange Value of A is--->"<<a<<"\tValue of B is--->"<<b;
getch( );
}
Output:-
Before Exchange Value of A is--->12 Value of B is--->14
After Exchange Value of A is--->14 Value of B is--->12
2. Sometimes with the use of pointer , a complex function can be easily represented and accessed.
3. The use of pointers in a function definition may be classified into two groups.
Call by Value
Call by Reference
Call By Value:-
1.In a call by value , whenever a portion of program invokes a function with a formal arguments , then control is transferred from main function to calling function and value of actual argument is copied to argument of function.
2. While calling a function , when copy of formal argument is sent to arguments of the called function then it is known as call by value.
3. In a call by value, when a control is transferred back from called function to the calling function of the program then altered values are not transferred back.
WAP for implementation of 'call by value' concept.
#include<conio.h>
#Include<iostream.h>
void main( )
{
int x,y;
clrscr( );
x = 12;
y = 13;
cout<<"\n\n\tValue of x--->"<<x<<"\t"<<"Value of y--->"<<y;
funct(x,y);
cout<<"\n\n\tValue of x--->"<<x<<"\t"<<"Value of y--->"<<y;
getch( );
}
void funct(int a, int b)
{
a = a*a;
b = b*b;
}
Output:-
Value of x--->12 Value of y--->13
Value of x--->12 Value of y--->13
Call By Reference:-
1. When a function is called by a portion of a program , the address of actual arguments are copied on to formal arguments though they may be referred by different variable name.
2. While calling a function, when the address or reference of actual arguments are sent to the called function then it is known as call by reference.
3. The content of that address can be accessed freely either within a function or within a calling routine.
4. In a call by reference when the address of actual arguments are sent then these address are stored within the pointer arguments of called function.
5. Thus the use of pointers as function arguments permits the corresponding data item to be altered globally from within the function.
6. Any change that is made to the data item will be recognized in both functions and calling portion of the program.
WAP to implement the concept of call by reference.
#include<conio.h>
#include<iostream.h>
void main( )
{
int x,y;
clrscr( );
x = 12;
y = 13;
cout<<"\n\n\tValue of x--->"<<x<<"\t"<<"Value of y--->"<<y;
funct(&x,&y);
cout<<"\n\n\tValue of x--->"<<x<<"\t"<<"Value of y--->"<<y;
getch( );
}
void funct(int *p1, int *p2)
{
*p1 = *p1 **p1;
*p2 = *p2**p2;
}
Output:-
Value of x is--->12 Value of y is--->13
Value of x is--->144 Value of y is--->169
WAP to exchange the values of two variable using call by value.
#include<conio.h>
#Include<iostream.h>
void exchange(int a, int b)
{
int temp;
temp=a;
a=b;
b=temp;
cout<<"\n\n\tAfter Exchange Value of A is--->"<<a<<"\tValue of B is--->"<<b;
}
void main( )
{
int a=12,b=14;
clrscr( );
cout<<"\n\n\tBefore Exchange Value of A is--->"<<a<<"\tValue of B is--->"<<b;
exchange(a,b);
getch( );
}
Output:-
Before Exchange Value of A is--->12 Value of B is--->14
After Exchange Value of A is--->14 Value of B is--->12
WAP to exchange the value of two variables using call by reference
#include<conio.h>
#include<iostream.h>
void exchange(int *a, int *b)
{
int *temp;
*temp=*a;
*a=*b;
*b=*temp;
}
void main( );
{
int a=12,b=14;
clrscr( );
cout<<"\n\n\tBefore Exchange Value of A is--->"<<a<<"\tValue of B is--->"<<b;
exchange(&a,&b);
cout<<"\n\n\tAfter Exchange Value of A is--->"<<a<<"\tValue of B is--->"<<b;
getch( );
}
Output:-
Before Exchange Value of A is--->12 Value of B is--->14
After Exchange Value of A is--->14 Value of B is--->12
0 Comments:
Post a Comment