Scientific Programming and Computer Architecture 23
Scientific Programming and Computer Architecture 23
}
has a totally different effect. When the compiler encounters a function definition, it generates machine
instructions for the body of the function (which is omitted here) and figures out where to place these instructions in
memory. After that, aitken corresponds to the chunk of memory that contains machine instructions that implement
the body of the function. Just as with variables, defining a function amounts to setting aside memory for it during
compilation.
Here we take a look at the mechanism of function calls and the compilation process. Much of the discussion is
centered around the file leibniz.c, which uses the functions defined by aitken.c to extrapolate the partial sums
of the Leibniz series and produces data corresponding to table 1.1↑.
The source file leibniz.c begins with two directives:
#include "aitken.h"
#include <stdio.h>
The first directive includes aitken.h to interface to functions defined in aitken.c. The second directive
includes stdio.h to interface to printing functions defined in the stdio (standard input/output) library.
The leibniz() function, which generates partial sums of the Leibniz series, is defined below without
comment.
//partial sums of 4(1-1/3+1/5-1/7+1/9-...)
void leibniz(double* seq, int len){
int i;
for(i=0; i < len; i++)
if(i==0)
seq[i] = 4.0;
else if(i%2==1)
seq[i] = seq[i-1] - 4.0/(2.0*i+1);
else
seq[i] = seq[i-1] + 4.0/(2.0*i+1);
}
The printseq() function prints a sequence using the printf() function defined in the stdio library.
https://divakarvi.github.io/bk-spca/spca.html[20-1-2019 23:44:49]