Course Content
C++ Introduction
0/1
C++ Variables & Constants
0/1
C++Scope of Variable
0/1
C++ Keywords & Identifiers
0/1
C++ Data Types
0/1
C++ Basic I/O
0/1
C++ Type Conversion
0/1
C++ Operators
0/1
C++ Comments
0/1
C++ If-else
0/1
C++ Ternary Operator
0/1
C++ for Loop
0/1
C++ Ranged for Loop
0/1
C++ while/do-while Loop
0/1
C++ break Statement
0/1
C++ Continue Statement
0/1
C++ switch Statement
0/1
C++ goto Statement
0/1
C++ Functions
0/1
C++ User-defined Functions
0/1
C++ Default Arguments
0/1
C++ Storage Class
0/1
C++ Recursion
0/1
C++ Return by Reference
0/1
C++ Arrays
0/1
C++ Multi-dimentional Arrays
0/1
C++ Arrays & Function
0/1
C++ String
0/1
C++ Structure
0/1
C++ Structure & Functions
0/1
C++ Pointers to Structure
0/1
C++ Pointers
0/1
C++ Void Pointers
0/1
C++ Pointers & Arrays
0/1
C++ Pointers & Functions
0/1
C++ Dynamic Memory Allocation
0/1
C++ OOPs Concepts
0/1
C++ Objects and Class
0/1
C++ Constructors
0/1
C++ Destructors
0/1
C++ Constructor Overloading
0/1
C++ Objects & Function
0/1
C++ Enumeration
0/1
C++ Inheritance
0/1
C++ Inheritance Access Control
0/1
C++ Inheritance Types
0/1
C++ Polymorphism
0/1
C++ Function Overloading
0/1
C++ Function Overriding
0/1
C++ Operator Overloading
0/1
C++ Friend Function
0/1
C++ Virtual Function
0/1
C++ Abstract Class & Pure Virtual Function
0/1
C++ Encapsulation
0/1
C++ Abstraction
0/1
C++ Templates
0/1
C++ Exception Handling
0/1
C++ Multithreading
0/1
C++ Standard Library
0/1
C++ Programming Tutorials
About Lesson

C++ Pointer and Functions

In this tutorial, we will learn about C++ Passing Pointers to Functions, C++ allows you to pass a pointer to a function with the help of examples.

Function Pointer

  • A function pointer is a variable that stores the address of a function that can later be called through that function pointer.
  • In the C++ Functions tutorial, we learned about passing arguments to a function. This method used is called passing by value because the actual value is passed.
  • As we know that Pointers are used to point some variables. Similarly the function pointer is a pointer used to point functions.
  • However, there is another way of passing arguments to a function where the actual values of arguments are not passed. Instead, the reference to values is passed.

Syntax

The following is the syntax for the declaration of a function pointer:

int *funcPtr(int, int)
  • The above syntax is the function declaration. As functions are not simple as variables, but C++ is a type safe, so function pointers have return type and parameter list.
  • In the above syntax, we first supply the return type, and then the name of the pointer, i.e., FuncPtr which is surrounded by the brackets and preceded by the pointer symbol, i.e., (*).
  • After this, we have supplied the parameter list (int,int). The above function pointer can point to any function which takes two integer parameters and returns integer type value.

Address of a Function

We can get the address of a function very easily. We just need to mention the name of the function, we don’t need to call the function.

For example,

#include <iostream>
using namespace std;
int main() {

cout << "The Address of a main() function is: " << &main << endl;
return 0;
}

working:

  • In the above program, we are displaying the address of a main() function.
  • To print the address of a main() function, we have just mentioned the name of the function, there is no bracket not parameters.
  • Therefore, the name of the function by itself without any brackets or parameters means the address of a function.
  • We can use the alternate way to print the address of a function, i.e., &main.

Calling a Function

We can call the function with the help of a function pointer by simply using the name of the function pointer.

The syntax of calling the function through the function pointer would be similar as we do the calling of the function normally.


Example 1: C++ Program to print sum using function pointer

#include <iostream>
using namespace std;
int sum(int a, int b) {

    return a + b;
}
int main() {

    int(*funcPtr)(int,int); // declaring a function pointer
    funcPtr = sum; // funcPtr is pointing to the sum function
    int sum = funcPtr (4, 6);
    cout << "The Value of sum is: " << sum << endl;
    return 0;
}

Output

The Value of sum is: 10

Working

  • In the above program, we declare the function pointer, i.e., int (*funcptr)(int,int) and then we store the address of add() function in funcptr.
  • This implies that funcptr contains the address of add() function.
  • Now, we can call the add() function by using funcptr. The statement funcptr(5,5) calls the add() function, and the result of add() function gets stored in sum variable.

Example 2: C++ Program to print Name using function pointer

To print the content of a void pointer, we use the static_cast operator. It converts the pointer from void* type to the respective data type of the address the pointer is storing:

#include <iostream>
using namespace std;
void displayName(char *name) {

    cout << "What is your Name: " << &name << endl;
}
int main() {

    char s[20]; // declaring a function pointer.
    void (*ptr) char*;  // function pointer declaration.
    ptr = displayName; // storing the address of displayName in ptr.
    cout << "Enter the name of the person: " << endl;
    cin << s;
    cout << s;
    ptr(s); // calling displayName() fumction.
    return 0;
}

Output

Enter the name of the person: 
Joe Root
Name is : Joe Root

Working

  • In the above program, we define the function printname() which contains the char pointer as a parameter.
  • We declare the function pointer, i.e., void (*ptr)(char*). The statement ptr=displayName means that we are assigning the address of displayName() function to ptr.
  • Now, we can call the displayName() function by using the statement ptr(s).

Passing a function pointer as a parameter

The function pointer can be passed as a parameter to another function.


Example 3: Passing by reference without pointers

#include <iostream>
using namespace std;
void fun1() {

    cout << "First Function is called.";
}
void fun2(void (*funPtr)() ) {
    funPtr();
}
int main() {

    fun2(fun1);
    return 0;
}

Output

First Function is called.

Working

  • In the above code, the fun2() function takes the function pointer as a parameter.
  • The main() method calls the fun2() function in which the address of fun1() is passed.
  • In this way, the fun2() function is calling the fun1() indirectly.

Call by Reference: Using pointers

Example 4: Passing by reference without pointers

#include <iostream>
using namespace std;
// function definition to swap values
void swapping(int &s1, int &s2) {
    int var;
    var = s1;
    s1 = s2;
    s2 = var;
}
int main(){

    // initialize variables
    int a = 1, b = 4;

    cout << "Before swapping" << endl;
    cout << "a = " << a << endl;
    cout << "b = " << b << endl;

    // call function to swapping numbers
    swapping(a, b);

    cout << "\nAfter swapping" << endl;
    cout << "a = " << a << endl;
    cout << "b = " << b << endl;

    return 0;
}

Output

Before swapping
a = 2
b = 4

After swapping
a = 4
b = 2

In this program, we passed the variables a and b to the swapping() function. Notice the function definition,

void swapping(int &s1, int &s2)

Here, we are using & to denote that the function will accept addresses as its parameters.

  • Hence, the compiler can identify that instead of actual values, the reference of the variables is passed to function parameters.
  • In the swapping() function, the function parameters s1 and s2 are pointing to the same value as the variables a and b respectively.
  • Hence the swapping takes place on actual value.The same task can be done using the pointers. To learn about pointers, visit C++ Pointers.

Example 5: Passing by reference using pointers

#include <iostream>
using namespace std;

// function prototype with pointer as parameters
void swapping(int*, int*);
int main(){

    // initialize variables
    int a = 2, b = 4;

    cout << "Before swapping the value" << endl;
    cout << "a = " << a << endl;
    cout << "b = " << b << endl;

    // call function by passing variable addresses
    swapping(&a, &b);

    cout << "\nAfter swapping the value" << endl;
    cout << "a = " << a << endl;
    cout << "b = " << b << endl;
    return 0;
}

// function definition to swapping numbers
void swapping(int* s1, int* s2) {
    int var;
    var = *s1;
    *s1 = *s2;
    *s2 = var;
}

Output

Before swapping the value
a = 2
b = 4

After swapping the value
a = 4
b = 2

Here, we can see the output is the same as the previous example. Notice the line,

// &a is address of a
// &b is address of b
swapping(&a, &b);

Here, the address of the variable is passed during the function call rather than the variable.

Since the address is passed instead of value, a dereference operator * must be used to access the value stored in that address.

var = *s1;
*s1 = *s2;
*s2 = var;

*s1 and *s2 gives the value stored at address s1 and s2 respectively.

Since s1 and s2 contain the addresses of a and b, anything is done to *s1 and *s2 will change the actual values of a and b.

Hence, when we print the values of a and b in the main() function, the values are changed.

Exercise Files
No Attachment Found
No Attachment Found
error: Content is protected !!