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 :-
#include <iostream>
.helloWorld()
that prints "Hello, World!".main()
function, declare a function pointer functionPointer
and assign it the address of helloWorld
.helloWorld()
using the pointer functionPointer
.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 :-
#include <iostream>
.sum(int a, int b)
that returns the sum of a
and b
.main()
function, declare a function pointer functionPointer
and assign it the address of sum
.sum(3, 4)
using the pointer functionPointer
and store the result in result
.result
using the cout
function.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 :-
#include <iostream>
.functionOne()
and functionTwo()
that print different messages ("Function One" and "Function Two").main()
function, define an array of function pointers functionArray[]
and assign the addresses of functionOne
and functionTwo
.functionOne()
using the function pointer array functionArray[0]
.functionTwo()
using the function pointer array functionArray[1]
.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 :-
#include <iostream>
.optionA()
and optionB()
that print "Option A Selected" and "Option B Selected" respectively.chooseOption(void (*callback)())
that takes a function pointer as an argument and calls it.main()
function, declare a function pointer functionPointer
.functionPointer
to optionA
if the choice is 1, otherwise assign it to optionB
.chooseOption()
with the function pointer functionPointer
as an argument.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; }