Tuesday, 3 April 2018

Pointers and Functions

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


0 Comments:

Post a Comment

Popular Posts

Categories

AI (32) Android (24) AngularJS (1) Assembly Language (2) aws (17) Azure (7) BI (10) book (4) Books (146) C (77) C# (12) C++ (82) Course (67) Coursera (198) Cybersecurity (24) data management (11) Data Science (106) Data Strucures (8) Deep Learning (13) Django (14) Downloads (3) edx (2) Engineering (14) Excel (13) Factorial (1) Finance (6) flask (3) flutter (1) FPL (17) Google (21) Hadoop (3) HTML&CSS (47) IBM (25) IoT (1) IS (25) Java (93) Leet Code (4) Machine Learning (46) Meta (18) MICHIGAN (5) microsoft (4) Nvidia (1) Pandas (3) PHP (20) Projects (29) Python (888) Python Coding Challenge (285) Questions (2) R (70) React (6) Scripting (1) security (3) Selenium Webdriver (2) Software (17) SQL (42) UX Research (1) web application (8)

Followers

Person climbing a staircase. Learn Data Science from Scratch: online program with 21 courses