SlideShare a Scribd company logo

Function Pointer

These slides clears concept of pointer to function or function pointer in C language. examples are also given to explain the concept better

1 of 13
Download to read offline
1
M R S . D I P A L I M E H E R
M O D E R N C O L L E G E O F A R T S , S C I E N C E A N D C O M M E R C E ,
G A N E S H K H I N D , P U N E 4 1 1 0 1 6
POINTER TO FUNCTION/
FUNCTION POINTER
( IN C PROGRAMMING)
POINTER
2
 Pointer is a variable which stores
address of another variable. It is denoted
by * operator.
 Functions are always stored somewhere
in memory. So they also have address.
So we can also define pointer to function
/ function pointer which will store address
of function.
STEPS TO DECLARE FUNCTION POINTER
Declare the function pointer- to declare function pointer by any name
with precedence of * operator. This pointer is written into opening
closing rounded bracket. Return data type if any will be written first
then pointer with rounded brackets then data type of actual
parameters will be written in rounded brackets. Declaring function
pointer does not means that function can be directly called.
Store the address- to call the function address of function must be
stored in function pointer
Call the function using function pointer- instead of using function
name to call it now use function pointer name to call the function (if
arguments are there then pass it i.e. actual parameters)
3
DECLARING A FUNCTION POINTER
Function
return type name_of_the function( data type arguement1, data type argument
2,…);
return type (* pointer name)( data type argument 1, data type argument2,..);
For example
4
Return
type
Name of the
function
Argument (Actual
Parameters)
Explanation
void add (int , int ); Add is such a function which
accepts two integers and prints
addition of them
void (*ptr) (int, int); ptr is a pointer to such function
which accepts two integers.( the
function can do any thing. In this
case it will do addition)
EXAMPLES..
add is a function
which accepts
nothing and prints
addition of them
void add(void); void (*ptr) (void); ptr is pointer to
such function
which does not
accepts anything
add is a function
which accepts two
integers and prints
addition of them
void add(int, int); void (*ptr) ( int,int); ptr is pointer to
such function
which accepts two
intergers
add is a function
which accepts two
integers and
returns addition of
them
int add(int, int); int (*ptr) ( int,int); ptr is pointer to
such function
which accepts two
integers and
returns one integer
5
HOW TO CALL FUNCTION USING FUNCTION POINTER
return type name_of_the function( data type arguement1, data
type argument 2,…)
void add(int , int);// declaring function in main function
int a,b;
add ( a, b)//calling function
return type (* pointer name)( data type argument 1, data type
argument2,..)
void(*ptr) (int, int);// declaring function pointer in main function
int a,b;
ptr=add; // storing address of function pointer
ptr( a, b);// calling function using function pointer
6

Recommended

Function in c program
Function in c programFunction in c program
Function in c programumesh patil
 
RECURSION IN C
RECURSION IN C RECURSION IN C
RECURSION IN C v_jk
 
Enumerated data types in C
Enumerated data types in CEnumerated data types in C
Enumerated data types in CArpana shree
 
Constructor and Types of Constructors
Constructor and Types of ConstructorsConstructor and Types of Constructors
Constructor and Types of ConstructorsDhrumil Panchal
 

More Related Content

What's hot (20)

Functions in C
Functions in CFunctions in C
Functions in C
 
Function in c
Function in cFunction in c
Function in c
 
07. Virtual Functions
07. Virtual Functions07. Virtual Functions
07. Virtual Functions
 
Functions in c
Functions in cFunctions in c
Functions in c
 
Pointers in c++
Pointers in c++Pointers in c++
Pointers in c++
 
Data types in c++
Data types in c++Data types in c++
Data types in c++
 
Constructor and destructor
Constructor  and  destructor Constructor  and  destructor
Constructor and destructor
 
Functions in c language
Functions in c language Functions in c language
Functions in c language
 
Pointers C programming
Pointers  C programmingPointers  C programming
Pointers C programming
 
Constructors and Destructor in C++
Constructors and Destructor in C++Constructors and Destructor in C++
Constructors and Destructor in C++
 
Structure of C++ - R.D.Sivakumar
Structure of C++ - R.D.SivakumarStructure of C++ - R.D.Sivakumar
Structure of C++ - R.D.Sivakumar
 
C Pointers
C PointersC Pointers
C Pointers
 
Types of Constructor in C++
Types of Constructor in C++Types of Constructor in C++
Types of Constructor in C++
 
Function overloading ppt
Function overloading pptFunction overloading ppt
Function overloading ppt
 
Pointers in C Programming
Pointers in C ProgrammingPointers in C Programming
Pointers in C Programming
 
Call by value
Call by valueCall by value
Call by value
 
Inheritance in c++
Inheritance in c++Inheritance in c++
Inheritance in c++
 
Constructors and destructors
Constructors and destructorsConstructors and destructors
Constructors and destructors
 
class and objects
class and objectsclass and objects
class and objects
 
Pointer in c
Pointer in cPointer in c
Pointer in c
 

Similar to Function Pointer (20)

Function pointer
Function pointerFunction pointer
Function pointer
 
Advanced pointers
Advanced pointersAdvanced pointers
Advanced pointers
 
Unit 4 (1)
Unit 4 (1)Unit 4 (1)
Unit 4 (1)
 
Recursion in C
Recursion in CRecursion in C
Recursion in C
 
pointers.pptx
pointers.pptxpointers.pptx
pointers.pptx
 
Dti2143 chapter 5
Dti2143 chapter 5Dti2143 chapter 5
Dti2143 chapter 5
 
Function in c
Function in cFunction in c
Function in c
 
Pointer in C
Pointer in CPointer in C
Pointer in C
 
Pointers
PointersPointers
Pointers
 
Function Pointer in C
Function Pointer in CFunction Pointer in C
Function Pointer in C
 
presentation_pointers_1444076066_140676 (1).ppt
presentation_pointers_1444076066_140676 (1).pptpresentation_pointers_1444076066_140676 (1).ppt
presentation_pointers_1444076066_140676 (1).ppt
 
c program.ppt
c program.pptc program.ppt
c program.ppt
 
User Defined Functions
User Defined FunctionsUser Defined Functions
User Defined Functions
 
Pointer.pptx
Pointer.pptxPointer.pptx
Pointer.pptx
 
Function
FunctionFunction
Function
 
Learn c++ (functions) with nauman ur rehman
Learn  c++ (functions) with nauman ur rehmanLearn  c++ (functions) with nauman ur rehman
Learn c++ (functions) with nauman ur rehman
 
Complicated declarations in c
Complicated declarations in cComplicated declarations in c
Complicated declarations in c
 
Unit 3 (1)
Unit 3 (1)Unit 3 (1)
Unit 3 (1)
 
C function
C functionC function
C function
 
Functions
FunctionsFunctions
Functions
 

More from Dr-Dipali Meher

Database Security Methods, DAC, MAC,View
Database Security Methods, DAC, MAC,ViewDatabase Security Methods, DAC, MAC,View
Database Security Methods, DAC, MAC,ViewDr-Dipali Meher
 
Version Stamps in NOSQL Databases
Version Stamps in NOSQL DatabasesVersion Stamps in NOSQL Databases
Version Stamps in NOSQL DatabasesDr-Dipali Meher
 
Formulation of Research Design
Formulation of Research DesignFormulation of Research Design
Formulation of Research DesignDr-Dipali Meher
 
Research Methodology-Intorduction
Research Methodology-IntorductionResearch Methodology-Intorduction
Research Methodology-IntorductionDr-Dipali Meher
 
Introduction to Research
Introduction to ResearchIntroduction to Research
Introduction to ResearchDr-Dipali Meher
 
Schema migrations in no sql
Schema migrations in no sqlSchema migrations in no sql
Schema migrations in no sqlDr-Dipali Meher
 
Naive bayesian classification
Naive bayesian classificationNaive bayesian classification
Naive bayesian classificationDr-Dipali Meher
 
Data mining an introduction
Data mining an introductionData mining an introduction
Data mining an introductionDr-Dipali Meher
 

More from Dr-Dipali Meher (17)

Database Security Methods, DAC, MAC,View
Database Security Methods, DAC, MAC,ViewDatabase Security Methods, DAC, MAC,View
Database Security Methods, DAC, MAC,View
 
Version Stamps in NOSQL Databases
Version Stamps in NOSQL DatabasesVersion Stamps in NOSQL Databases
Version Stamps in NOSQL Databases
 
DataPreprocessing.pptx
DataPreprocessing.pptxDataPreprocessing.pptx
DataPreprocessing.pptx
 
Literature Review
Literature ReviewLiterature Review
Literature Review
 
Research Problem
Research ProblemResearch Problem
Research Problem
 
Formulation of Research Design
Formulation of Research DesignFormulation of Research Design
Formulation of Research Design
 
Types of Research
Types of ResearchTypes of Research
Types of Research
 
Research Methodology-Intorduction
Research Methodology-IntorductionResearch Methodology-Intorduction
Research Methodology-Intorduction
 
Introduction to Research
Introduction to ResearchIntroduction to Research
Introduction to Research
 
Neo4j session
Neo4j sessionNeo4j session
Neo4j session
 
Introduction to NoSQL
Introduction to NoSQLIntroduction to NoSQL
Introduction to NoSQL
 
Consistency in NoSQL
Consistency in NoSQLConsistency in NoSQL
Consistency in NoSQL
 
Data models in NoSQL
Data models in NoSQLData models in NoSQL
Data models in NoSQL
 
Schema migrations in no sql
Schema migrations in no sqlSchema migrations in no sql
Schema migrations in no sql
 
Polyglot Persistence
Polyglot Persistence Polyglot Persistence
Polyglot Persistence
 
Naive bayesian classification
Naive bayesian classificationNaive bayesian classification
Naive bayesian classification
 
Data mining an introduction
Data mining an introductionData mining an introduction
Data mining an introduction
 

Recently uploaded

Kartik Nair In Media Res Media Component
Kartik Nair In Media Res Media ComponentKartik Nair In Media Res Media Component
Kartik Nair In Media Res Media ComponentInMediaRes1
 
Mycology Update February 2024 Microbes with Morgan
Mycology Update February 2024 Microbes with MorganMycology Update February 2024 Microbes with Morgan
Mycology Update February 2024 Microbes with MorganMargie Morgan
 
BIOCHEMICAL PROPERTIES OF WATER .Raveesh.pptx
BIOCHEMICAL PROPERTIES OF WATER .Raveesh.pptxBIOCHEMICAL PROPERTIES OF WATER .Raveesh.pptx
BIOCHEMICAL PROPERTIES OF WATER .Raveesh.pptxRAVEESHAD
 
SSC Hindu Religion and Moral Education MCQ Solutions 2024.pdf
SSC Hindu Religion and Moral Education MCQ Solutions 2024.pdfSSC Hindu Religion and Moral Education MCQ Solutions 2024.pdf
SSC Hindu Religion and Moral Education MCQ Solutions 2024.pdfMohonDas
 
Add Products From Catalog in Odoo 17 Sales
Add Products From Catalog in Odoo 17 SalesAdd Products From Catalog in Odoo 17 Sales
Add Products From Catalog in Odoo 17 SalesCeline George
 
Plagiarism, Types & Consequences by Dr. Sarita Anand
Plagiarism, Types & Consequences by Dr. Sarita AnandPlagiarism, Types & Consequences by Dr. Sarita Anand
Plagiarism, Types & Consequences by Dr. Sarita AnandDr. Sarita Anand
 
A TEXTBOOK OF INTELLECTUAL ROPERTY RIGHTS
A TEXTBOOK OF INTELLECTUAL ROPERTY RIGHTSA TEXTBOOK OF INTELLECTUAL ROPERTY RIGHTS
A TEXTBOOK OF INTELLECTUAL ROPERTY RIGHTSDr.M.Geethavani
 
Learner Digital Skills Toolkit DRAFT.docx
Learner Digital Skills Toolkit DRAFT.docxLearner Digital Skills Toolkit DRAFT.docx
Learner Digital Skills Toolkit DRAFT.docxGeorgeMilliken2
 
Successful projects and failed programmes – the cost of not designing the who...
Successful projects and failed programmes – the cost of not designing the who...Successful projects and failed programmes – the cost of not designing the who...
Successful projects and failed programmes – the cost of not designing the who...Association for Project Management
 
Google Ad Grants Services at TechSoup.pdf
Google Ad Grants Services at TechSoup.pdfGoogle Ad Grants Services at TechSoup.pdf
Google Ad Grants Services at TechSoup.pdfTechSoup
 
2.27.24 Malcolm X and the Black Freedom Struggle.pptx
2.27.24 Malcolm X and the Black Freedom Struggle.pptx2.27.24 Malcolm X and the Black Freedom Struggle.pptx
2.27.24 Malcolm X and the Black Freedom Struggle.pptxMaryPotorti1
 
Unit 5th topic Drugs used in congestive Heart failure and shock.pdf
Unit 5th topic Drugs used in congestive Heart failure and shock.pdfUnit 5th topic Drugs used in congestive Heart failure and shock.pdf
Unit 5th topic Drugs used in congestive Heart failure and shock.pdfSUMIT TIWARI
 
Dr.M.Florence Dayana-Cloud Computing-unit - 4.pdf
Dr.M.Florence Dayana-Cloud Computing-unit - 4.pdfDr.M.Florence Dayana-Cloud Computing-unit - 4.pdf
Dr.M.Florence Dayana-Cloud Computing-unit - 4.pdfDr.Florence Dayana
 
Managing Choice, Coherence and Specialisation in Upper Secondary Education - ...
Managing Choice, Coherence and Specialisation in Upper Secondary Education - ...Managing Choice, Coherence and Specialisation in Upper Secondary Education - ...
Managing Choice, Coherence and Specialisation in Upper Secondary Education - ...EduSkills OECD
 
EVALUATION POWERPOINT - STRANGER THINGS.pptx
EVALUATION POWERPOINT - STRANGER THINGS.pptxEVALUATION POWERPOINT - STRANGER THINGS.pptx
EVALUATION POWERPOINT - STRANGER THINGS.pptxiammrhaywood
 
Enhancing MRD Testing in Hematologic Malignancies: When Negativity is a Posit...
Enhancing MRD Testing in Hematologic Malignancies: When Negativity is a Posit...Enhancing MRD Testing in Hematologic Malignancies: When Negativity is a Posit...
Enhancing MRD Testing in Hematologic Malignancies: When Negativity is a Posit...i3 Health
 
天橋立 観光メモ 2024-30-302-2-Amanohashidate Sightseeing Memo.pptx
天橋立 観光メモ 2024-30-302-2-Amanohashidate Sightseeing Memo.pptx天橋立 観光メモ 2024-30-302-2-Amanohashidate Sightseeing Memo.pptx
天橋立 観光メモ 2024-30-302-2-Amanohashidate Sightseeing Memo.pptxMasami YASUDA
 
How To Create Record Rules in the Odoo 17
How To Create Record Rules in the Odoo 17How To Create Record Rules in the Odoo 17
How To Create Record Rules in the Odoo 17Celine George
 
CATCH UP FRIDAY LESSON PLAN GRADE 6.docx
CATCH UP FRIDAY LESSON PLAN GRADE 6.docxCATCH UP FRIDAY LESSON PLAN GRADE 6.docx
CATCH UP FRIDAY LESSON PLAN GRADE 6.docxAizaPolinarCruz
 
Understanding Canada's international higher education landscape (2024)
Understanding Canada's international higher education landscape (2024)Understanding Canada's international higher education landscape (2024)
Understanding Canada's international higher education landscape (2024)CaraSkikne1
 

Recently uploaded (20)

Kartik Nair In Media Res Media Component
Kartik Nair In Media Res Media ComponentKartik Nair In Media Res Media Component
Kartik Nair In Media Res Media Component
 
Mycology Update February 2024 Microbes with Morgan
Mycology Update February 2024 Microbes with MorganMycology Update February 2024 Microbes with Morgan
Mycology Update February 2024 Microbes with Morgan
 
BIOCHEMICAL PROPERTIES OF WATER .Raveesh.pptx
BIOCHEMICAL PROPERTIES OF WATER .Raveesh.pptxBIOCHEMICAL PROPERTIES OF WATER .Raveesh.pptx
BIOCHEMICAL PROPERTIES OF WATER .Raveesh.pptx
 
SSC Hindu Religion and Moral Education MCQ Solutions 2024.pdf
SSC Hindu Religion and Moral Education MCQ Solutions 2024.pdfSSC Hindu Religion and Moral Education MCQ Solutions 2024.pdf
SSC Hindu Religion and Moral Education MCQ Solutions 2024.pdf
 
Add Products From Catalog in Odoo 17 Sales
Add Products From Catalog in Odoo 17 SalesAdd Products From Catalog in Odoo 17 Sales
Add Products From Catalog in Odoo 17 Sales
 
Plagiarism, Types & Consequences by Dr. Sarita Anand
Plagiarism, Types & Consequences by Dr. Sarita AnandPlagiarism, Types & Consequences by Dr. Sarita Anand
Plagiarism, Types & Consequences by Dr. Sarita Anand
 
A TEXTBOOK OF INTELLECTUAL ROPERTY RIGHTS
A TEXTBOOK OF INTELLECTUAL ROPERTY RIGHTSA TEXTBOOK OF INTELLECTUAL ROPERTY RIGHTS
A TEXTBOOK OF INTELLECTUAL ROPERTY RIGHTS
 
Learner Digital Skills Toolkit DRAFT.docx
Learner Digital Skills Toolkit DRAFT.docxLearner Digital Skills Toolkit DRAFT.docx
Learner Digital Skills Toolkit DRAFT.docx
 
Successful projects and failed programmes – the cost of not designing the who...
Successful projects and failed programmes – the cost of not designing the who...Successful projects and failed programmes – the cost of not designing the who...
Successful projects and failed programmes – the cost of not designing the who...
 
Google Ad Grants Services at TechSoup.pdf
Google Ad Grants Services at TechSoup.pdfGoogle Ad Grants Services at TechSoup.pdf
Google Ad Grants Services at TechSoup.pdf
 
2.27.24 Malcolm X and the Black Freedom Struggle.pptx
2.27.24 Malcolm X and the Black Freedom Struggle.pptx2.27.24 Malcolm X and the Black Freedom Struggle.pptx
2.27.24 Malcolm X and the Black Freedom Struggle.pptx
 
Unit 5th topic Drugs used in congestive Heart failure and shock.pdf
Unit 5th topic Drugs used in congestive Heart failure and shock.pdfUnit 5th topic Drugs used in congestive Heart failure and shock.pdf
Unit 5th topic Drugs used in congestive Heart failure and shock.pdf
 
Dr.M.Florence Dayana-Cloud Computing-unit - 4.pdf
Dr.M.Florence Dayana-Cloud Computing-unit - 4.pdfDr.M.Florence Dayana-Cloud Computing-unit - 4.pdf
Dr.M.Florence Dayana-Cloud Computing-unit - 4.pdf
 
Managing Choice, Coherence and Specialisation in Upper Secondary Education - ...
Managing Choice, Coherence and Specialisation in Upper Secondary Education - ...Managing Choice, Coherence and Specialisation in Upper Secondary Education - ...
Managing Choice, Coherence and Specialisation in Upper Secondary Education - ...
 
EVALUATION POWERPOINT - STRANGER THINGS.pptx
EVALUATION POWERPOINT - STRANGER THINGS.pptxEVALUATION POWERPOINT - STRANGER THINGS.pptx
EVALUATION POWERPOINT - STRANGER THINGS.pptx
 
Enhancing MRD Testing in Hematologic Malignancies: When Negativity is a Posit...
Enhancing MRD Testing in Hematologic Malignancies: When Negativity is a Posit...Enhancing MRD Testing in Hematologic Malignancies: When Negativity is a Posit...
Enhancing MRD Testing in Hematologic Malignancies: When Negativity is a Posit...
 
天橋立 観光メモ 2024-30-302-2-Amanohashidate Sightseeing Memo.pptx
天橋立 観光メモ 2024-30-302-2-Amanohashidate Sightseeing Memo.pptx天橋立 観光メモ 2024-30-302-2-Amanohashidate Sightseeing Memo.pptx
天橋立 観光メモ 2024-30-302-2-Amanohashidate Sightseeing Memo.pptx
 
How To Create Record Rules in the Odoo 17
How To Create Record Rules in the Odoo 17How To Create Record Rules in the Odoo 17
How To Create Record Rules in the Odoo 17
 
CATCH UP FRIDAY LESSON PLAN GRADE 6.docx
CATCH UP FRIDAY LESSON PLAN GRADE 6.docxCATCH UP FRIDAY LESSON PLAN GRADE 6.docx
CATCH UP FRIDAY LESSON PLAN GRADE 6.docx
 
Understanding Canada's international higher education landscape (2024)
Understanding Canada's international higher education landscape (2024)Understanding Canada's international higher education landscape (2024)
Understanding Canada's international higher education landscape (2024)
 

Function Pointer

  • 1. 1 M R S . D I P A L I M E H E R M O D E R N C O L L E G E O F A R T S , S C I E N C E A N D C O M M E R C E , G A N E S H K H I N D , P U N E 4 1 1 0 1 6 POINTER TO FUNCTION/ FUNCTION POINTER ( IN C PROGRAMMING)
  • 2. POINTER 2  Pointer is a variable which stores address of another variable. It is denoted by * operator.  Functions are always stored somewhere in memory. So they also have address. So we can also define pointer to function / function pointer which will store address of function.
  • 3. STEPS TO DECLARE FUNCTION POINTER Declare the function pointer- to declare function pointer by any name with precedence of * operator. This pointer is written into opening closing rounded bracket. Return data type if any will be written first then pointer with rounded brackets then data type of actual parameters will be written in rounded brackets. Declaring function pointer does not means that function can be directly called. Store the address- to call the function address of function must be stored in function pointer Call the function using function pointer- instead of using function name to call it now use function pointer name to call the function (if arguments are there then pass it i.e. actual parameters) 3
  • 4. DECLARING A FUNCTION POINTER Function return type name_of_the function( data type arguement1, data type argument 2,…); return type (* pointer name)( data type argument 1, data type argument2,..); For example 4 Return type Name of the function Argument (Actual Parameters) Explanation void add (int , int ); Add is such a function which accepts two integers and prints addition of them void (*ptr) (int, int); ptr is a pointer to such function which accepts two integers.( the function can do any thing. In this case it will do addition)
  • 5. EXAMPLES.. add is a function which accepts nothing and prints addition of them void add(void); void (*ptr) (void); ptr is pointer to such function which does not accepts anything add is a function which accepts two integers and prints addition of them void add(int, int); void (*ptr) ( int,int); ptr is pointer to such function which accepts two intergers add is a function which accepts two integers and returns addition of them int add(int, int); int (*ptr) ( int,int); ptr is pointer to such function which accepts two integers and returns one integer 5
  • 6. HOW TO CALL FUNCTION USING FUNCTION POINTER return type name_of_the function( data type arguement1, data type argument 2,…) void add(int , int);// declaring function in main function int a,b; add ( a, b)//calling function return type (* pointer name)( data type argument 1, data type argument2,..) void(*ptr) (int, int);// declaring function pointer in main function int a,b; ptr=add; // storing address of function pointer ptr( a, b);// calling function using function pointer 6
  • 7. EXAMPLE ( ON LINUX PLATFORM WRITING FUNCTION DEFINITION BEFORE MAIN) steps Normal Programme using Function Programme using function pointer steps Declare a function void add( int a, int b) { printf(“nAddition=%d”,a+ b); } void add( int a, int b) { printf(“nAddition=%d”,a+b); } In main call the function int main() {int x,y; printf(“enter two values:”); scanf(“%d%d”,&x,&y); add(x,y); } int main() { void (*p)(int, int); int x,y; printf(“enter two values:”); scanf(“%d%d”,&x,&y); ptr=add; ptr(x,y); } Declare the function pointer Store the address Call the function using function pointer 7
  • 8. EXAMPLE ( ON LINUX PLATFORM WRITING FUNCTION DEFINITION AFTER MAIN) steps Normal Programme using Function Programme using function pointer steps In main call the function int main() {int x,y; void add(int, int); printf(“enter two values:”); scanf(“%d%d”,&x,&y); add(x,y); } int main() { void (*p)(int, int); int x,y; void add( int,int); printf(“enter two values:”); scanf(“%d%d”,&x,&y); ptr=add; ptr(x,y); } Declare the function pointer Store the address Call the function using function pointer Declare a function after main void add( int a, int b) { printf(“nAddition=%d”,a+ b); } void add( int a, int b) { printf(“nAddition=%d”,a+b); } 8
  • 9. CALLING FUNCTION IN MAIN Without function pointer With function pointer #include<stdio.h> #include<stdio.h> void add() { int a,b; printf(“n Enter two values:”); scanf(“%d%d”, &a,&b); printf(“naddition=%d”,a+b); } void add() { int a,b; printf(“n Enter two values:”); scanf(“%d%d”, &a,&b); printf(“naddition=%d”,a+b); } int main() { add();// directly calling the function } int main() { void(*ptr) (void);// declaring function pointer ptr=add;// pointer storing address of function ptr();//calling of function written before main } 9
  • 10. CALLING FUNCTION IN MAIN WITH ARGUMENTS Without function pointer With function pointer #include<stdio.h> #include<stdio.h> void add(int a,int b) { printf(“naddition=%d”,a+b); } void add(int a,int b) { printf(“naddition=%d”,a+b); } int main() { int x,y; printf(“nEnter two numbers:”); scanf(“%d%d”, &x,&y); add(x,y);// calling the function with arguments } int main() { void(*ptr) (int,int);// declaring function pointer int x,y; printf(“nEnter two numbers:”); scanf(“%d%d”,&x,&y); ptr=add;// pointer storing address of function ptr(x,y);//calling of function using function //pointer and function written before main } 10
  • 11. CALLING FUNCTION IN MAIN WITH ARGUMENTS AND RETURNING VALUE Without function pointer With function pointer #include<stdio.h> #include<stdio.h> void add(int a, int b) { return a+b; } void add(int a, int b) { return a+b; } int main() { int x,y,z; printf(“nEnter two numbers:”); scanf(“%d%d”,&x,&y); z=add(x,y);// calling the function with arguments printf(“n addition=%d”, z); } int main() { int (*ptr) (int, int);// declaring function pointer int x,y,z; printf(“nEnter two numbers:”); scanf(“%d%d”, &x,&y); ptr=add;// pointer storing address of function z=ptr(x,y); //calling of function using function pointer and function written before main printf(“n addition=%d”, z); } 11
  • 12. CALLING FUNCTION IN MAIN WITH ARGUMENTS AND RETURNING VALUE Without function pointer With function pointer #include<stdio.h> #include<stdio.h> int add(int x) { int sum=0; for(i=1;i<=x;i++) { sum=sum+i; } return sum; } int add(int x) { int sum=0; for(i=1;i<=x;i++) { sum=sum+i; } return sum; } int main() { int n,z=0; printf(“nEnter n:”); scanf(“%d”, &n); z=add(n); printf(“n sum of first n numbers=%d“,z); } int main() { int (*ptr) (int);// declaring function pointer int n,z=0; printf(“nEnter n:”); scanf(“%d”, &n); ptr=add;// pointer storing address of function z=ptr(n); //calling of function using function pointer and function written before main printf(“nsum of first n numbers=%d”, z); 12
  • 13. 13