Tuesday 3 April 2018

Pointers to Functions

1. Cpp allows functions to be referenced by a pointer.
2. A pointer to a function must be declared to be a pointer to the datatype returned by the functions like void , int , float and so on....
3. In addition , the argument type of the function must also be specified when a pointer is declared.
4. The argument declaration is a list of a formal argument separated by commas and enclosed in parenthesis.
5. The general Syntax of a pointer to a function is
    Returntype (*variable) (list of parameters);
   Ex : void (*ptr) (float a, float b, float c);
   In the above declaration , a pointer to a function returns void and takes formal arguments of two float and one int.
 Ex : float (*ptr) (char a, double b, int c, float d)
 In the above declaration , a pointer to a function returns a floating point value and takes the formal argument of char , double , int and float type.
6. After the declaration of pointer to a function , the address of the function must be assigned to a pointer.


WAP to find sum of three numbers and average using pointer to function.

#Include<conio.h>
#include<iostream.h>
void calavg(int a, int b, int c);
void (*ptr)(int a, int b, int c);
void main( )
{
int a1, b1, c1;
clrscr( );
cout<<"\n\tEnter Any three Values";
cin>>a1>>b1>>c1;
ptr=&calavg;
(*ptr)(a1,b1,c1);
getch( );
}
void calavg(int a, int b, int c)
{
int sum;
float avg;
sum=a+b+c;
avg=sum/3.0;
cout<<"\n\n\tSum of three nos is--->"<<sum;
cou<<"\n\n\tAverage is--->"<<avg;
}

Output:-
Enter Any three Values 2.23  45
Sum of three nos is--->80
Average is--->26.66666

WAP to demonstrate how a pointer to a function is declared to perform simple arithmetic operation addition, subtraction, multiplication and division of two nos.

#include<conio.h>
#include<iostream.h>
void calop(int a, int b);
void (*ptr)(int a, int b)
void main( )
{
int a1,b1;
clrscr( );
cout<<"\n\n\tEnter Any two nos";
cin>>a1>>b1;
ptr=&calop;
(*ptr)(a1,b1);
getch( );
}
void calop(int a1,int b1)
{
cout<<"\n\n\tAddition is---><<(a1+b1);
cout<<"\n\n\Subtraction is--->"<<(a1-b1);
cout<<\n\n\tMultiplication is--->"<<(a1*b1);
cout<<"\n\n\tDivision is---><<(a1/b1);
}

Output:-
Enter Any two nos 12  6
Addition is--->18
Subtraction is--->6
Multiplication is--->72
Division is--->2

0 Comments:

Post a Comment

Popular Posts

Categories

AI (27) Android (24) AngularJS (1) Assembly Language (2) aws (17) Azure (7) BI (10) book (4) Books (114) C (77) C# (12) C++ (82) Course (60) Coursera (176) coursewra (1) Cybersecurity (22) data management (11) Data Science (89) Data Strucures (6) Deep Learning (9) Django (6) Downloads (3) edx (2) Engineering (14) Excel (13) Factorial (1) Finance (5) flutter (1) FPL (17) Google (19) Hadoop (3) HTML&CSS (46) IBM (25) IoT (1) IS (25) Java (92) Leet Code (4) Machine Learning (44) Meta (18) MICHIGAN (5) microsoft (3) Pandas (3) PHP (20) Projects (29) Python (741) Python Coding Challenge (191) Questions (2) R (70) React (6) Scripting (1) security (3) Selenium Webdriver (2) Software (17) SQL (40) UX Research (1) web application (8)

Followers

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