Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Please explain why the answer is what is is thoroughly. I am trying to study for

ID: 3827665 • Letter: P

Question

Please explain why the answer is what is is thoroughly. I am trying to study for my test. I will make sure to thumbs up! thank you very much for your time

double derive(double (*f)(double), double x) {

double stepSize = 0.01;

double x1 = x – stepSize;
double x2 = x + stepSize;
return f(x2)- f(x1)/ (2 *stepSize);

}

19. Write a function that finds the derivative of a function f at a value x. It takes a function pointer f as argument that represents the function for computing the derivative. The derivative of a function can be calculated by f(x stepsize)-f(x stepsize) 2 stepsize Use 0.01 for stepsize

Explanation / Answer

Please let me know if still you have doubt.

About Function Pointer:

In C, like normal data pointers (int *, char *, etc), we can have pointers to functions. Following is a simple example that shows declaration and function call using function pointer.

#include <stdio.h>
// A normal function with an int parameter
// and void return type
void fun(int a)
{
printf("Value of a is %d ", a);
}

int main()
{
// fun_ptr is a pointer to function fun()
void (*fun_ptr)(int) = &fun;

/* The above line is equivalent of following two
void (*fun_ptr)(int);
fun_ptr = &fun;
*/

// Invoking fun() using fun_ptr
(*fun_ptr)(10);

return 0;
}

Why do we need an extra bracket around function pointers like fun_ptr in above example?
If we remove bracket, then the expression "void (*fun_ptr)(int)" becomes “void *fun_ptr(int)” which is declaration of a function that returns void pointer.

Following are some interesting facts about function pointers.


1) Unlike normal pointers, a function pointer points to code, not data. Typically a function pointer stores the start of executable code.


2) Unlike normal pointers, we do not allocate de-allocate memory using function pointers.

3) Like normal data pointers, a function pointer can be passed as an argument and can also be returned from a function.
For example, consider the following C program where wrapper() receives a void fun() as parameter and calls the passed function.

// A simple C program to show function pointers as parameter
#include <stdio.h>

// Two simple functions
void fun1() { printf("Fun1 "); }
void fun2() { printf("Fun2 "); }

// A function that receives a simple function
// as parameter and calls the function
void wrapper(void (*fun)())
{
fun();
}

int main()
{
wrapper(fun1);
wrapper(fun2);
return 0;
}


How to declare a pointer to a function?
return_data_type (*function_name)(paramter list);
Ex: int (*foo)(int);

Comining to Question:


double derive(double (*f)(double), double x) { // it takes a function pointer and a double value as parameter
   double stepSize = 0.01;
   double x1 = x – stepSize;
   double x2 = x + stepSize;
   return f(x2)- f(x1)/ (2 *stepSize); // usign function f
}