|
void f()
{
printf("Hello\n");
}
void g()
{
printf("Good-bye\n");
}
void run( void h() ) // Function with a function as parameter !
{
h(); // Run h()...
}
int main( int argc, char *argv[] )
{
run( f ); // Call run with parameter f
run( g ); // Call run with parameter g
}
|
Output:
Hello Good-bye |
How to run the program:
|
|
run:
.LFB2:
.cfi_startproc
pushq %rbp
.cfi_def_cfa_offset 16
movq %rsp, %rbp
.cfi_offset 6, -16
.cfi_def_cfa_register 6
subq $16, %rsp
movq %rdi, -8(%rbp)
movq -8(%rbp), %rdx // Put starting address of function in rdx
movl $0, %eax
call *%rdx // Call the function (bsr) !!!
|
|
|
void f( char *s ) // f has a string parameter s
{
printf("%s\n", s);
}
void g( int s ) // g has an int parameter s
{
printf("%d\n", s);
}
int h( char * s ) // h has different return type
{
printf("%s\n", s);
return 0;
}
/* ----------------------------------------------------------------
run has a function parameter of the form:
void h( char * ) --- i.e., a string (char[])
Function f matches this function parameter declaration
Function g does NOT match this function parameter declaration
---------------------------------------------------------------- */
void run( void h( char * ), char *a )
{
h(a);
}
int main( int argc, char *argv[] )
{
run( f, "Hello" );
run( g, "Hello" ); // Error: argument 1 (g) has the wrong type !!!
run( h, "Hello" ); // Error: argument 1 (h) has the wrong type !!!
}
|
Explanation:
|
How to run the program:
|
|
#define epsilon 0.00001
/* ----------------------------
We solve this equation...
---------------------------- */
double f( double x )
{
return x*x*x + 2*x - 5 ; // Solve: x^3 + 2x - 5 = 0
}
int main( int argc, char *argv[] )
{
double a, b, m, y_m, y_a;
a = 0; b = 10; // Start interval [a .. b]
while ( (b-a) > epsilon )
{
m = (a+b)/2; // Mid point of [a .. b]
y_m = f(m); // y_m = f(m)
y_a = f(a); // y_a = f(a)
if ( (y_m > 0 && y_a < 0) || (y_m < 0 && y_a > 0) )
{ // f(a) and f(m) have different signs: move b
b = m;
}
else
{ // f(a) and f(m) have same signs: move a
a = m;
}
printf("New interval: [%lf .. %lf]\n", a, b); // Print progress
}
printf("Approximate solution = %lf\n" , (a+b)/2 );
}
|
How to run the program:
|
#define epsilon 0.00001
double f( double x )
{
return x*x*x + 2*x - 5 ; // Solve: x^3 + 2x - 5 = 0
}
double g( double x )
{
return x*log(x) + 3*sin(x) - 7 ; // Solve: x*ln(x) + 3*sin(x) - 7
}
/* ======================================================
BiSection algorithm: uses a function parameter !!!
====================================================== */
double BiSection( double f(double), double a, double b )
{
double m, y_m, y_a;
while ( fabs(b-a) > epsilon ) // fabs() = floating point (double) absolute
{
m = (a+b)/2; // Mid point
y_m = f(m); // y_m = f(m)
y_a = f(a); // y_a = f(a)
if ( (y_m > 0 && y_a < 0) || (y_m < 0 && y_a > 0) )
{ // f(a) and f(m) have different signs: move b
b = m;
}
else
{ // f(a) and f(m) have same signs: move a
a = m;
}
printf("New interval: [%lf .. %lf]\n", a, b); // Print progress
}
return (a+b)/2 ;
}
int main( int argc, char *argv[] )
{
double r;
r = BiSection( f, 0, 10 );
printf("x^3 + 2x - 5 = 0, x = %lf\n" , r );
r = BiSection( g, 0, 10 );
printf("x*ln(x) + 3*sin(x) - 7, x = %lf\n" , r );
}
|
Output:
x^3 + 2x - 5 = 0, x = 1.328273
x*ln(x) + 3*sin(x) - 7, x = 5.445571
|
How to run the program:
|