Function Reference in C++ Programming

What is a Function Reference in C++ ?

A function reference in C++ refers to passing the address (reference) of a function to another function, typically to allow dynamic function calls or callbacks.

  • Structure: A function pointer is used to hold the reference to a function. It can then be invoked like any regular function.
  • Benefits: Function references enable dynamic function calls, which is useful in scenarios such as event handling, callbacks, and implementing polymorphism in C.
  • Common Usage: Often used in implementing callbacks, handling events, or managing function pointers in data structures like linked lists.

Algorithm :-
  • Step 1: Start
  • Step 2: Include the required header file #include <iostream>.
  • Step 3: Define a function helloWorld() that prints "Hello, World!".
  • Step 4: In the main() function, declare a function pointer functionPointer and assign it the address of helloWorld.
  • Step 5: Call the function helloWorld() using the pointer functionPointer.
  • Step 6: Return 0 to indicate successful program execution.
  • Step 7: Stop

  • Example 1: Simple Function Reference

    Definition: This example demonstrates the Basic usage of a function pointer in C++, where a function pointer is used to call a function.

    Explanation: The function pointer stores the address of the function and calls it through the pointer.

    Expected Output: The output will display "Hello, World!" from the function call.

    #include <iostream>
    
    void helloWorld() {
    std::cout << "Hello, World!\n";
    }
    
    int main() {
    void (*functionPointer)() = helloWorld;
    functionPointer();  // Calling the function using the pointer
    return 0;
    }
    

    Algorithm :-
  • Step 1: Start
  • Step 2: Include the required header file #include <iostream>.
  • Step 3: Define a function sum(int a, int b) that returns the sum of a and b.
  • Step 4: In the main() function, declare a function pointer functionPointer and assign it the address of sum.
  • Step 5: Call the function sum(3, 4) using the pointer functionPointer and store the result in result.
  • Step 6: Print the value of result using the cout function.
  • Step 7: Return 0 to indicate successful program execution.
  • Step 8: Stop

  • Example 2: Function Reference with Arguments

    Definition: This example demonstrates how to use a function pointer to reference a function that accepts arguments.

    Explanation: The function pointer is used to call a function that takes two integer arguments and returns their sum.

    Expected Output: With a = 3 and b = 4, the output will be "Sum: 7".

    #include <iostream>
    
    int sum(int a, int b) {
    return a + b;
    }
    
    int main() {
    int (*functionPointer)(int, int) = sum;
    int result = functionPointer(3, 4);  // Calling the function using the pointer
    std::cout << "Sum: " << result << "\n";
    return 0;
    }
    

    Algorithm :-
  • Step 1: Start
  • Step 2: Include the required header file #include <iostream>.
  • Step 3: Define two functions functionOne() and functionTwo() that print different messages ("Function One" and "Function Two").
  • Step 4: In the main() function, define an array of function pointers functionArray[] and assign the addresses of functionOne and functionTwo.
  • Step 5: Call functionOne() using the function pointer array functionArray[0].
  • Step 6: Call functionTwo() using the function pointer array functionArray[1].
  • Step 7: Return 0 to indicate successful program execution.
  • Step 8: Stop

  • Example 3: Array of Function Pointers

    Definition: This example shows how to use an array of function pointers to reference multiple functions and call them dynamically.

    Explanation: The program uses an array of function pointers to reference two different functions and calls each function by index.

    Expected Output: The output will display results from both function calls.

    #include <iostream>
    
    void functionOne() {
    std::cout << "Function One\n";
    }
    
    void functionTwo() {
    std::cout << "Function Two\n";
    }
    
    int main() {
    void (*functionArray[])() = {functionOne, functionTwo};
    functionArray[0]();  // Calling function one
    functionArray[1]();  // Calling function two
    return 0;
    }
    

    Algorithm :-
  • Step 1: Start
  • Step 2: Include the required header file #include <iostream>.
  • Step 3: Define two functions optionA() and optionB() that print "Option A Selected" and "Option B Selected" respectively.
  • Step 4: Define a function chooseOption(void (*callback)()) that takes a function pointer as an argument and calls it.
  • Step 5: In the main() function, declare a function pointer functionPointer.
  • Step 6: Assign the function pointer functionPointer to optionA if the choice is 1, otherwise assign it to optionB.
  • Step 7: Call chooseOption() with the function pointer functionPointer as an argument.
  • Step 8: Return 0 to indicate successful program execution.
  • Step 9: Stop

  • Example 4: Function Pointer as Callback

    Definition: This example demonstrates using function pointers as callbacks to execute different functions based on user input.

    Explanation: The function pointer is passed to another function, which then calls it based on some condition or event.

    Expected Output: Depending on the choice, either "Option A Selected" or "Option B Selected" will be printed.

    #include <iostream>
    
    void optionA() {
    std::cout << "Option A Selected\n";
    }
    
    void optionB() {
    std::cout << "Option B Selected\n";
    }
    
    void chooseOption(void (*callback)()) {
    callback();  // Calling the callback function
    }
    
    int main() {
    void (*functionPointer)();
    int choice = 1;  // 1 for Option A, 2 for Option B
    
    if (choice == 1)
    functionPointer = optionA;
    else
    functionPointer = optionB;
    
    chooseOption(functionPointer);
    return 0;
    }