Software Development Exam  >  Software Development Notes  >  Basics of C++  >  Function Pointer in C++

Function Pointer in C++ | Basics of C++ - Software Development PDF Download

Introduction

Function pointers are a powerful feature of C++ programming language. They allow us to store the address of a function in a pointer variable and then use that pointer to call the function. This article aims to provide an easy-to-understand explanation of function pointers in C++.

What is a Function Pointer?

A function pointer is a variable that stores the address of a function. In C++, functions are first-class citizens and can be treated like any other data type. Therefore, a function pointer is just like a regular pointer, but instead of pointing to an object, it points to a function.

Declaring a Function Pointer

To declare a function pointer, we use the following syntax:

return_type (*pointer_name)(argument_type1, argument_type2, ..., argument_typeN);

Here is an example of declaring a function pointer:

int (*p)(int, int);

This declares a function pointer named 'p' that can point to a function that takes two 'int' arguments and returns an 'int'.

Assigning a Function to a Function Pointer

Once we have declared a function pointer, we can assign a function to it. Here is an example of assigning a function to a function pointer:

int add(int a, int b) {

    return a + b;

}


int (*p)(int, int) = add;

In this example, we have assigned the 'add' function to the 'p' pointer. Now we can call the 'add' function using the 'p' pointer like this:

int result = p(3, 4);

This will call the 'add' function with the arguments '3' and '4', and store the 'result' in the result variable.

Using Function Pointers

We can use function pointers in many ways. Here are some examples:
1. Passing Function Pointers as Arguments:
We can pass a function pointer as an argument to another function. Here is an example:

void print_result(int (*operation)(int, int), int a, int b) {

    int result = operation(a, b);

    std::cout << "Result: " << result << std::endl;

}


int add(int a, int b) {

    return a + b;

}


int multiply(int a, int b) {

    return a * b;

}


int main() {

    int (*p)(int, int) = add;

    print_result(p, 3, 4);


    p = multiply;

    print_result(p, 3, 4);


    return 0;

}

In this example, we have defined a function 'print_result' that takes a function pointer as its first argument. We then pass the 'add' function and the 'multiply' function as arguments to 'print_result' using the 'p' pointer.

2. Storing Function Pointers in Arrays:
We can store function pointers in arrays. Here is an example:

int add(int a, int b) {

    return a + b;

}


int multiply(int a, int b) {

    return a * b;

}


int main() {

    int (*operations[])(int, int) = {add, multiply};

    int result = operations[0](3, 4);

    std::cout << "Result: " << result << std::endl;


    result = operations[1](3, 4);

    std::cout << "Result: " << result << std::endl;


    return 0;

}

In this example, we have defined an array 'operations' that stores two function pointers. We then use the function pointers to call the 'add' function and the 'multiply' function.

Sample Problems

1. Write a program that uses a function pointer to find the maximum of two integers.

#include <iostream>


int max(int a, int b) {

    return a > b ? a : b;

}


int main() {

    int (*p)(int, int) = max;

    int result = p(3, 4);

    std::cout << "Result: " << result << std::endl;

    return 0;

}

Output

Result: 4

2. Write a program that uses a function pointer to calculate the sum of an array of integers.

#include <iostream>


int sum(int* arr, int size) {

    int result = 0;

    for (int i = 0; i < size; i++) {

        result += arr[i];

    }

    return result;

}


int main() {

    int arr[] = {1, 2, 3, 4, 5};

    int size = sizeof(arr) / sizeof(arr[0]);


    int (*p)(int*, int) = sum;

    int result = p(arr, size);

    std::cout << "Result: " << result << std::endl;

    return 0;

}

Output

Result: 15

Conclusion

Function pointers are a powerful feature of C++ that allows us to store the address of a function in a pointer variable and then use that pointer to call the function. We can use function pointers in many ways, such as passing them as arguments to other functions, storing them in arrays, and more. With these examples, you should be able to start using function pointers in your own programs.

The document Function Pointer in C++ | Basics of C++ - Software Development is a part of the Software Development Course Basics of C++.
All you need of Software Development at this link: Software Development
70 videos|44 docs|15 tests
70 videos|44 docs|15 tests
Download as PDF
Explore Courses for Software Development exam
Signup for Free!
Signup to see your scores go up within 7 days! Learn & Practice with 1000+ FREE Notes, Videos & Tests.
10M+ students study on EduRev
Download the FREE EduRev App
Track your progress, build streaks, highlight & save important lessons and more!
Related Searches

past year papers

,

Summary

,

Exam

,

Important questions

,

Function Pointer in C++ | Basics of C++ - Software Development

,

Previous Year Questions with Solutions

,

Free

,

practice quizzes

,

Sample Paper

,

MCQs

,

Objective type Questions

,

Viva Questions

,

Extra Questions

,

mock tests for examination

,

video lectures

,

pdf

,

Semester Notes

,

Function Pointer in C++ | Basics of C++ - Software Development

,

ppt

,

Function Pointer in C++ | Basics of C++ - Software Development

,

shortcuts and tricks

,

study material

;