FUNCTION
AS ARGUMENTS:
A
function pointer (or subroutine pointer or procedure pointer) is a type of
pointer supported by third-generation programming languages Instead of
referring to data values, a function pointer points to executable code within
memory. When dereferenced, a function pointer can be used to invoke the
function it points to and pass it arguments just like a normal function call.
Such an invocation is also known as an "indirect" call, because the
function is being invoked indirectly through a variable instead of directly
through a fixed name or address. Function pointers can be used to simplify code
by providing a simple way to select a function to execute based on run-time
values.
#include
<iostream.h>
int
add(int first, int second)
{
return
first + second;
}
int
subtract(int first, int second)
{
return
first - second;
}
int
operation(int first, int second, int (*functocall)(int, int))
{
return
(*functocall)(first, second);
}
void
main()
{
int a, b;
int (*plus)(int, int) = add;
int (*minus)(int, int) = subtract;
a = operation(7, 5, plus);
b =
operation(20, a, minus);
cout
<< "a = " << a << " and b = " << b
<< endl;
}
Related Topics
Privacy Policy, Terms and Conditions, DMCA Policy and Compliant
Copyright © 2018-2023 BrainKart.com; All Rights Reserved. Developed by Therithal info, Chennai.