Example program for pointer to function in C C++

Example program for pointer to function in C C++

Pointer और इसके कार्य करने के तरीके को आधिक जानने के लिए देखें यह पोस्ट—Pointers in C

C Language में Pointer क्या प्रयोग करते हुए किसी Function को कैसे point किया जा सकता है जानने के लिए देखें—Pointer to function

C Language में Array और इसके विभिन्न प्रकारो के बारे में और अधिक जानने के लिए देखें— Arrays in C

Example program for pointer to function in C

#include<stdio.h>
void sum(int x,int y);
int main()
{
    int a=0,b=0;
    void (*ptr) (int,int);
    ptr=sum;
    printf("Enter a b: ");
    scanf("%d%d",&a,&b);
    ptr(a,b);
    return 0;
}
void sum(int x,int y)
{
    printf("a = %d\n",x);
    printf("b = %d\n",y);
    printf("Sum = %d\n",x+y);
}

Example program for pointer to function in C++

#include<iostream>
using namespace std;
void sum(int x,int y);
int main()
{
    int a=0,b=0;
    void (*ptr) (int,int);
    ptr=sum;
    cout<<"Enter a b: ";
    cin>>a>>b;
    ptr(a,b);
    return 0;
}
void sum(int x,int y)
{
    cout<<"a = "<<x<<endl;
    cout<<"b = "<<y<<endl;
    cout<<"Sum = "<<x+y<<endl;
}

Output of Example program for pointer to function in C C++

Example program for pointer to function in C C++

Other important function based programs in C C++

  1. Sum of two numbers using function
  2. Average of three numbers using function
  3. Swapping using function call by value
  4. Swapping using function call by reference
  5. Passing array as function argument
  6. Passing structure as function argument
Share it to:

Published by

admin

I am a computer teacher, programmer and web developer